Binary sorting tree (BST): creates, searches, inserts, and deletes binary sorting trees.
Binary sorting tree (BST): Create, search, insert, and delete
Delete node operations (left and right subtree are non-empty methods to delete nodes ):
Algorithm analysis:
The following example describes operations related to the creation, search, insertion, and deletion of the binary sorting tree;
For example, enter a keyword sequence (,), and then perform corresponding operations on it. The procedure is as follows:
# Include <iostream> # include <stdio. h> # include <stdlib. h> using namespace std; typedef struct BiTNode {int value; struct BiTNode * lchild, * rchild;} * BiTree; bool LT (int a, int B) // LessThan is less than {if (a <B) return true; elsereturn false ;} /* Recursively search for the data element whose keyword is equal to data in the binary sorting tree pointed by the root pointer root. if the search is successful, the pointer p points to the data element node, returns true. Otherwise, the pointer p points to the last node accessed in the search path and returns the false pointer. the pointer f points to the root parent, its initial call value is NULL */bool SearchBST (BiTree root, int data, BiTree f, BiTree & p) {If (! Root) {p = f; return false;} else if (data = root-> value) // query successful {p = root; return true ;} else if (data <root-> value) // search for return SearchBST (root-> lchild, data, root, p) in the left subtree ); else if (data> root-> value) // search for return SearchBST (root-> rchild, data, root, p) in the right subtree );} // insert datainline void InsertBST (BiTree & root, int data) When no data element with the keyword equal to data exists in the binary sorting tree root. // The root is the reference pointer {BiTree p, s; if (! SearchBST (root, data, NULL, p) // query failed {s = (struct BiTNode *) malloc (sizeof (BiTNode); s-> value = data; s-> lchild = s-> rchild = NULL; if (p = NULL) // when the binary sorting tree is empty, the inserted node * s is the new root node root = s; else if (LT (data, p-> value )) // The inserted node * s is left child p-> lchild = s; else // The inserted node * s is right child p-> rchild = s;} return ;} void PreOrderTraverse (BiTree root) // first traverse {if (root) {printf ("% d", root-> value); PreOrderTraverse (root-> lchild ); preOrderTraverse (root-> rchild) ;}} void InOrderTraverse (BiTree root) // In-order traversal {if (root) {InOrderTraverse (root-> lchild); printf ("% d", root-> value ); inOrderTraverse (root-> rchild) ;}} void PostOrderTraverse (BiTree root) // post-order traversal {if (root) {PostOrderTraverse (root-> lchild ); postOrderTraverse (root-> rchild); printf ("% d", root-> value) ;}} int Delete (BiTree & p) {BiTree q, s; if (! P-> rchild) {// if the right subtree is empty, you only need to re-connect its left q = p; p = p-> lchild; free (q);} else if (! P-> lchild) {// If the left subtree is empty, just re-connect it to the right subtree q = p; p = p-> rchild; free (q );} else {// left and right subtree are not empty q = p; s = p-> lchild; while (s-> rchild) {// turn left. Then, go to the right to end q = s; s = s-> rchild;} p-> value = s-> value; // s to the front line of the deleted node if (q! = P) {q-> rchild = s-> lchild;} // re-connect * The right subtree else of q {q-> lchild = s-> lchild ;} // re-connect the left subtree of * q delete s;} return true;} int DeleteBST (BiTree & T, int key) {if (! T) return false; else {if (key = T-> value) return Delete (T ); // locate the data element else if (key <T-> value) return DeleteBST (T-> lchild, key) with the key Keyword, that is, in the left subtree else return DeleteBST (T-> rchild, key); // If the keyword is greater than the node, that is, in the right subtree} int main (void) {int I, a [101], n, data, findkey, menu, insertkey; BiTree root, pp; printf ("binary tree operation: \ n"); printf ("1: create a binary sorting tree! \ N "); printf (" 2: Search for keywords! \ N "); printf (" 3: Insert a keyword! \ N "); printf (" 4: delete keywords! \ N "); printf (" 0: Quit! \ N "); // number of input nodes printf (" Enter the corresponding operation: "); scanf (" % d ", & menu); while (! (Menu> = 0 & menu <= 4) {printf ("if you have a problem with your input, enter:"); scanf ("% d ", & menu) ;}while (menu! = 0) {// switch (menu) {// case 1: {// printf ("Enter the number of initialization nodes:"); scanf ("% d ", & n); root = NULL; printf ("input node values:"); for (I = 1; I <= n; I ++) {scanf ("% d", & a [I]); InsertBST (root, a [I]);} printf ("output first-order traversal result :"); preOrderTraverse (root); printf ("\ n"); printf ("output ordinal traversal result:"); InOrderTraverse (root); printf ("\ n "); printf ("output post-order traversal result:"); PostOrderTraverse (root); printf ("\ n" ); Break;} // casecase 2: {printf ("Enter the keyword you want to search for:"); scanf ("% d", & findkey); if (SearchBST (root, findkey, NULL, pp )) {printf ("search successful! \ N ");} else {printf (" failed to search! \ N ") ;}break;} case 3: {printf (" Enter the keyword you inserted: "); scanf (" % d ", & insertkey ); insertBST (root, insertkey); printf ("\ n"); printf ("output sequential traversal result:"); InOrderTraverse (root); printf ("\ n "); break;} case 4: {printf ("Enter the keyword you want to delete:"); scanf ("% d", & data); DeleteBST (root, data ); printf ("\ n"); printf ("output sequential traversal result:"); InOrderTraverse (root); printf ("\ n"); break ;} /* while (scanf ("% d", & n )! = EOF) {root = NULL; printf ("input node values:"); for (I = 1; I <= n; I ++) {scanf ("% d", & a [I]); InsertBST (root, a [I]);} printf ("output first-order traversal result :"); preOrderTraverse (root); printf ("\ n"); printf ("output ordinal traversal result:"); InOrderTraverse (root); printf ("\ n "); printf ("output post-order traversal result:"); PostOrderTraverse (root); printf ("\ n"); printf ("Enter the keyword you want to search :"); scanf ("% d", & findkey); if (SearchBST (root, findkey, NULL, pp) {printf ("search successful! \ N ");} else {printf (" failed to search! \ N ") ;}printf (" Enter the keyword you want to delete: "); scanf (" % d ", & data); DeleteBST (root, data ); printf ("\ n"); printf ("output sequential traversal result:"); InOrderTraverse (root );} * //} // switch printf ("Please enter the corresponding operation:"); scanf ("% d", & menu) ;}// whilereturn 0 ;}
The operation result is as follows:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.