int main() { BST_t *pStrTree = newBST( (CmpFunc_t*)strcmp, NULL ); int n; while ( fgets( buffer, LEN_MAX, stdin ) != NULL ) // Read each line. { size_t len = strlen( buffer ); // Length incl. // newline character. if ( !BST_insert( pStrTree, buffer, len+1 )) // Insert the line in break; // the tree. } if ( !feof(stdin) ) { // If unable to read the entire text: fprintf( stderr, "sortlines: " "Error reading or storing text input.\n" ); exit( EXIT_FAILURE ); } n = BST_inorder( pStrTree, printStr ); // Print each line, in sorted order. fprintf( stderr, "\nsortlines: Printed %d lines.\n", n ); BST_clear( pStrTree ); // Discard all nodes. return 0; }
struct node* BST_insert(struct node* node,int key) { if(node==NULL) { //Inserting a Node in BST return(new_node(key)); } // Find Right Place (subtree) to insert node. if((node->data)>key) { node->left=BST_insert(node->left,key); } else{ node->right=BST_insert(node->right,key); } return node; }
void BST_delete(BST **tarp,int val){ BST *target; if(tarp==NULL) return ; target=*tarp; if(target->val==val){ if(target->count>0){ target->count--; target->w--; } } else if(target->val>val){ BST_insert(&(target->left),val); } else{ BST_insert(&(target->right),val); } BST_update(tarp); }
void BST_test(void) { int i = 10; BST_t *new_bst = BST_create(1000); srand(time(NULL)); for (i = 1; i <= 10; i++) BST_insert(new_bst, rand()%10000); }
void test_binary_tree_search() { // 二叉查找树要求记录的关键字唯一,所以不能有相同的记录 const int length = 11; int array[length] = {65, 32, 49, 10, 8, 72, 27, 42, 18, 58, 91}; int key1 = 72; int key2 = 55; print_array(array, length, " data: "); BSTree tree = NULL; BSTNode* node = NULL; // 创建二叉树 BST_create(&tree, array, length); if (!tree) { printf("Failed to create binary search tree!\n"); return; } // 查找 node = BST_search(tree, key1); printf(" %s %d in binary search tree.\n", (NULL == node) ? "Could not find" : "Yeah! Found", key1); node = BST_search(tree, key2); printf(" %s %d in binary search tree.\n", (NULL == node) ? "Could not find" : "Found", key2); // 插入节点 printf(" Insert %d to binary search tree.\n", key2); BST_insert(&tree, key2); node = BST_search(tree, key2); printf(" %s %d in binary search tree.\n", (NULL == node) ? "Could not find" : "Yeah! Found", key2); // 删除节点 key2 = 27; printf(" Remove %d from binary search tree.\n", key2); BST_remove(&tree, key2); node = BST_search(tree, key2); printf(" %s %d in binary search tree.\n", (NULL == node) ? "Could not find" : "Yeah! Found", key2); // 销毁二叉树 BST_destory(&tree); assert(NULL == tree); }
void BST_insert(BST **tarp,int val){ BST *target=*tarp; if(target==NULL){ target=(BST *)malloc(sizeof(BST)); target->val=val; target->h=0; target->left=NULL;target->right=NULL; target->count=1; target->w=1; *tarp=target; return ; } if(target->val==val){ target->count++; target->w++; } else if(target->val>val){ BST_insert(&(target->left),val); } else{ BST_insert(&(target->right),val); } BST_update(tarp); }
int main() { struct node* root=NULL; root = BST_insert(root,5); BST_insert(root,10); BST_insert(root,24); BST_insert(root,4); BST_insert(root,3); BST_insert(root,99); BST_insert(root,1); inorder(root); printf("%d is the min node of BST",min_node_in_bst(root)); return 0; }