二叉搜索树(排序树,检索树)的建树,查找;

#include<cstdio>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
using namespace std;
struct node
{  int val;
  node *left;
  node *right;
  node()
  {}
  node(int xx)
 {
   val=xx;left=right=NULL;
  }
  
}Tree;
void Build(node *tree,int x)//建树的过程就是二分查找的过程
{
	
	
	  if(tree->val>x)
	   {       
		   if(tree->left==NULL)
		        tree->left=new node(x);
		   else
	          Build(tree->left,x);
	   
	    }
	  else if(tree->val<=x)
	  {  
		  if(tree->right==NULL)
		    tree->right=new node(x);
		  else
		    Build(tree->right,x);
		
	   }
}
void InOrder(node *tree)
{
	if(tree!=NULL)
	{
		InOrder(tree->left);
		printf("%d ",tree->val);
		InOrder(tree->right);
		//printf("%d ",tree->val);
	}
}
//void PostOrder(node* tree)
//{
//	if(tree!=NULL)
//	{
//		PostOrder(tree->left);
//		
//		PostOrder(tree->right);
//		delete tree;
//	}
//	
//}
int Bsearch(node *tree,int key)//此查找就是二分查找的过程
{
  if(tree==NULL)
	  return -1;//表示没有此数据;查找失败
  else
  {
	  if(tree->val==key)//查找成功
		  return key;
	  else if(tree->val>key)
	  {
	    Bsearch(tree->left,key);
	  }
	  else if(tree->val<key)
	  {
		  Bsearch(tree->right,key);
	  }
  }
}
int main()
{
	int x;
	int n;
	cin>>n;
	cin>>x;
	Tree.val=x;
	n--;
	while(n--)
	{
	 cin>>x;
	Build(&Tree,x);
	}
	InOrder(&Tree);//已经是排过序的;
	printf("\n");
	//PostOrder(&Tree);//释放内存
	printf("\n");
	cout<<"hello word"<<endl;
 return 0;
}

 

二叉排序树,二叉检索树,二叉查找树指的都是同一一个东西,以前自己都被这些概念搞迷了;现在终于明白了,线段树也是二叉排序树的一种变形;所以说掌握二叉排序树是基本的要求,以后还有好多神奇的数据结构都是二叉排序树,任重道远;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值