16. Binary sorting tree, 16 binary sorting

Source: Internet
Author: User

16. Binary sorting tree, 16 binary sorting
Reprinted Please table name Source: http://blog.csdn.net/u0126375011. Binary sorting treeIf the dataset to be searched is an ordered linear table and is stored in sequence, you can use search algorithms such as folding, interpolation, and Fibonacci to find the dataset. Then, due to order, it takes a lot of time to insert and delete data. The binary sorting tree to be learned below is an algorithm that not only improves insertion and deletion efficiency, but also achieves search efficiency. Therefore, the purpose of constructing a binary sorting tree is not to sort, but to provide the speed of searching and inserting and deleting keywords.1. Binary sorting tree conceptBinary Sort Tree, also known as Binary search Tree, is an empty Tree or a Binary Tree of the following nature. ◆ If its left subtree is not empty, the values of all nodes on the left subtree are smaller than the value of its root structure (parent node); ◆ if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node (parent node); ◆ its left and right subtree is also a binary sorting tree.2. Binary Tree node Structure

/* Tree Binary Tree binary linked list node structure definition */typedef struct BiTNode // node Structure {int data; // node data Struct BiTNode * lchild, * rchild; // left and right child pointer} BiTNode, * BiTree;
Ii. Binary sorting tree operation Algorithm 1. query operations on the binary sorting tree
/* Recursively search for keys in the binary sorting tree T. * if the pointer f points to the parent of T, its initial call value is NULL * If the query is successful, the pointer p points to the data element node and returns TRUE; otherwise, the pointer p points to the last node accessed in the search path and returns FALSE */Status SearchBST (BiTree T, int key, biTree f, BiTree * p) {if (! T) // query failed (empty tree) {* p = f; return FALSE;} else if (key = T-> data) // query successful {* p = T; return TRUE;} else if (key <T-> data) return SearchBST (T-> lchild, key, T, p ); // continue searching for else return SearchBST (T-> rchild, key, T, p) in the left subtree; // continue searching in the right subtree}
Instance: Suppose there is a data set = {, 88,}, and the Search Keyword key is 93. Follow these steps to use the binary sorting tree: ① construct the data set into a binary sorting tree (in-order traversal) according to the binary sorting tree definition );
② Call the binary sorting tree query algorithm SearchBST (T, 93, NULL, P) to query keywords. Among them, the SearchBST function is a recursive function, the parameter T is a binary tree linked list, the key represents the keyword to be queried, And the binary tree f points to the parent tree of T. When T points to the root node, the initial value of f is NULL, which is useful in recursion. The final parameter p is used to find the node location that can be found after the search is successful. ③ If (! T) {...} statement. It is used to determine whether the current binary tree is directed to the leaf node. At this time, T points to the root node 62. Because T is not empty, this statement segment is not executed. ④ Esle if (key = T-> data) statement. That is to say, the matching keyword execution statement is found, obviously 93! = 62, so the statement segment is not executed. ⑤ Else if (key <T-> data) statement. That is, when the keyword to be searched is smaller than the current knot, the statement segment is not executed because it is 93> 62. 6. else {...} statement. That is, when the keyword to be searched is greater than the current knot, the execution statement is 93> 62. Therefore, SearchBST (T-> rchild, key, T, p) is called recursively ). At this time, T points to 62 right children 88, f points to 88's parent node, that is, 62.
7. At this time, the second layer of SearchBST is larger than 88. Therefore, execute the else {...} statement and call SearchBST recursively again (T-> rchild, key, T, p ). T points to 99, the right child of 88.
Because 93 is smaller than 99, the third-layer SearchBST executes the else if (key <T-> data) Statement and recursively calls SearchBST (T-> lchild, key, T, p ). T points to 93 of the 99 left child.
The fourth-layer SearchBST of the pipeline. Because the key is equal to T-> data, the execution speed is 10th ~ Row 11. At this time, the pointer p points to the node where 93 is located and returns True to the Layer 3, Layer 2, and Layer 1, and returns the function True. 2. insert operation of the binary sorting treeThe so-called binary sorting Tree insertion, that is, placing keywords in a suitable position in the tree. (1) binary sorting Tree Insertion Algorithm
/* If the binary sorting tree T does not contain data elements with a keyword equal to the key, * inserts the key and returns TRUE; otherwise, FALSE */Status InsertBST (BiTree * T, int key) is returned) {BiTree p, s;/* Call The lookup function to find whether the keyword exists * //. if the search fails, if (! SearchBST (* T, key, NULL, & p) {s = (BiTree) malloc (sizeof (BiTNode )); // create a memory space for node s-> data = key; // store the keywords in the data domain s pointing to the node s-> lchid = s-> rchild = NULL; // initialize the Left and Right pointer fields of node s if (! P) * T = s; // insert s into the new root node else if (key <p-> data) // if the keyword is less than the p node data value, insert the left child p-> lchild = s; else // If the keyword is greater than the data value of the p node, insert s to the right child of node p-> rchild = s;}/* the node with the same keywords is no longer inserted */else {return FALSE ;}}
For example, if the call function is "InsertBST (T, 93);", the result is FALSE. If the call function is "InsertBST (T, 95 );", then, add a right child 95 at the 93 node and return TRUE. Note that the insert algorithm calls the SearchBST (* T, key, NULL, & p) Search Algorithm in advance and uses the ordinal traversal of the binary tree, in the end, we can see that the pointer p points to 93. 3. Build a binary sorting Tree Algorithm
 
/* Assume there is a dataset = {,} * to construct a binary sorting tree */int I; int a [0] =, 35,73, 51,99, 37,93}; BiTree T = NULL; for (I = 0; I <10; I ++) {InsertBST (& T, a [I]);}
4. Binary sorting tree deletion algorithm
(1) use recursion to search for the key in the binary sorting tree T, and then call the Delete function to Delete the node. * If the binary sorting tree T contains a data element whose key is equal to the key, delete the data element node * and return TRUE; otherwise, return FALSE */
Status DeleteBST (BiTree * T, int key) {if (! * T) // No data element whose key keyword is equal to return FALSE; else {if (key = (* T)-> data) // find the data element return Delete (T) whose key is equal to the key; // call the Delete function to Delete the else if (key <(* T)-> data) of the node) return DeleteBST (& (* T)-> lchild, key); else return DeleteBST (& (* T)-> rchild, key );}}
(2) Delete An Algorithm
/* Delete the node p from the binary sorting tree and reconnect it to the left or right subtree */Status Delete (BiTree * p) {BiTree q, s;/* Scenario 2: delete the right subtree of node p or left subtree is empty */if (* p)-> lchild = NULL) //. if the right subtree is empty, you only need to re-connect its left subtree {q = * p; * p = (* p)-> lchild; free (q );} else if (* p)-> rchild = NULL) // B. just re-connect the right subtree {q = * p; * p = (* p)-> rchild; free (q ); // point p to the node} // Case 3: left and right subtree are not empty else {q = * p; s = (* p)-> lchild; while (s-> rchild) // turn left, and then to the right to the end (find the precursor of the node to be deleted) {q = s; s = s-> rchild ;} (* p)-> data = s-> data; // s direct forward to the deleted node if ( Q! = * P) q-> rchild = s-> lchild; // re-connect the right subtree of q else q-> lchild = s-> lchild; // re-connect the left subtree of q free (s);} return TRUE ;}
Source code analysis: q = * p; * p = (* p)-> rchild; free (q); function: Assign the pointer p to the new node q, and the p Pointer Points to the left node, that is, the right subtree is re-connected, and then the node q is released.
Iii. Binary sorting tree SummaryThe binary sorting tree is stored as a link, keeping the link storage structure free of moving elements when performing the insert or delete operation. After finding the proper insert and delete locations, you only need to modify the link pointer. The insertion and deletion time has good performance. For the binary sorting tree, the path from the root node to the node to be searched is used, the number of comparisons is equal to the number of nodes in the binary sorting tree. In extreme cases, it is at least once, that is, the heel node is the node to be searched, and it does not exceed the depth of the tree at most. That is, the query performance of the binary sorting tree depends on the shape of the binary sorting tree.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.