二叉排序樹BST(未完、佔坑)
阿新 • • 發佈:2019-01-07
#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 ); }