二叉排序树

#include <stdio.h>
#include <stdlib.h>
typedef struct BSTNode
{
    int data;
    struct BSTNode *lchild,*rchild;
}BSTNode,*PNode;

int count = 0;
bool SearchBST(PNode T,int data)
{
    count++;
    if(T == NULL)
    {
        return false;
    } 
    else if(data < T->data)
    {
        return SearchBST(T->lchild,data);
    }
    else if(data > T->data)
    {
        return SearchBST(T->rchild,data);
    }
    else 
    {
        return true;    
    }

//向树中插入节点,若此节点在不在树中则插入,已在树中则不插入 
PNode insertBST(int data,PNode root)
{
    if(root == NULL)
    {
        root = (PNode)malloc(sizeof(BSTNode));
        root->data = data;
        root->lchild = NULL;
        root->rchild = NULL;
        return root;
    }
    if(data < root->data)
    {
        root->lchild = insertBST(data,root->lchild);
    } 
    else if(data > root->data)
    {
        root->rchild = insertBST(data,root->rchild);
    }   
    return root;
}


int main(){     
    int r[10000];
    int i,j;
    PNode root; 
    PNode current; 
    int search;
    while(true){      
        for(i=0;i<10000;i++){  
            scanf("%d",&r[i]);
            if(getchar()=='\n')
            {
                break; 
            }   
        }     
        scanf("%d",&search);
        root = NULL;
        current= NULL;
        count = 0;
        for(j=0;j<i;j++){  
            current = insertBST(r[j],current);
            if(j == 0)
            {
                root = current;
            }
        }
        SearchBST(root,search);
        printf("%d",count);

        if (getchar()=='\n')
        {
            printf("\n");
            continue;       
        }
        else
        {
            break;    
        }  
    }  
    return 0; 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值