二叉查找树

   二叉查找树(BinarySearch Tree,也叫二叉搜索树,或称二叉排序树Binary Sort Tree)或者是一棵空树,或者是具有下列性质的二叉树:

    (1)、若它的左子树不为空,则左子树上所有结点的值均小于它的根结点的值;

    (2)、若它的右子树不为空,则右子树上所有结点的值均大于它的根结点的值;

    (3)、它的左、右子树也分别为二叉查找树。

#include 
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
const int maxn=100;//假设最多有100个元素
int data[maxn];
int n;
using namespace std;
struct node
{
    int data;  //结点的值
    struct node *lchild,*rchild;//指向左、右孩子的指针
};
struct node * Insert_tree(struct node *root,int data)
{
    if(root==NULL)
    {
        root=(struct node *)malloc(sizeof(struct node));//为data申请一个新结点
        root->data=data;
        root->lchild=root->rchild=NULL;//该结点为叶子结点
        return root;
    }
    if(data<=root->data)//插在root的左子树
    {
        root->lchild=Insert_tree(root->lchild,data);
    }
    if(data>root->data)//插在root的右子树
    {
        root->rchild=Insert_tree(root->rchild,data);
    }
    return root;
}
struct node * Create_tree()//将无序序列data[]建立二叉查找树
{
    struct node * T=NULL;
    for(int i=0;i
      
      
       
       data==k)//查找成功
        return root;
    else if(root->data>k)
        return Search(root->lchild,k);//查找左子树
    else return Search(root->rchild,k);//查找右子树
 }
int main()
{
     while(cin>>n)
     {
         for(int i=0;i
       
       
         >data[i]; int k; cin>>k;//要查找的数 if(Search(Create_tree(),k)==NULL) cout<<"0"< 
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值