二叉排序树BST(未完、占坑)

#include <stdio.h>
#include <stdlib.h>

typedef struct BSTNode{
    int  data;
    struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;

int BST_Insert( BSTree T , int key ){
    if( T == NULL ){
        BSTree T = (BSTree )malloc(sizeof(BSTNode));
        T -> lchild  = T -> rchild = NULL;
        T -> data = key;
        return 1;
    }
    else if( key == T->data )
        return 0;
    else if( key < T->data)
        return BST_Insert( T->lchild , key );
    else
        return BST_Insert( T->rchild , key );
}

void CreateBSTree(BSTree T , int data[] ,int n){
    T = NULL ;
    int i = 0 ;
    while ( i<n ){
        scanf("%d",&data[i]);
        BST_Insert( T , data[i] ) ;
        i++;
    }
}

void BST_Search( BSTree T , int key ){
    if( key==T->data ) 
        printf("找到\n");
    while(T)
        {
        if( key < T->data ) T = T->lchild ;
        else T = T->rchild ;
    }
    return T ;
}

int main(){
    int data[50] , n  , key ;
    printf("输入节点个数:\n");
    scanf("%d",&n);
    BSTree T ;
    CreateBSTree( T , data , n );
    printf("查找关键字:\n");
    scanf("%d" , &key);
    BST_Search( T , key );
}

猜你喜欢

转载自blog.csdn.net/RRWJ__/article/details/83119638