Basic operations of the binary sorting tree (Binary Search Tree)

Source: Internet
Author: User

Basic operations of the binary sorting tree (Binary Search Tree)

The search of the binary sorting tree belongs to the category of dynamic search. Based on whether the table is modified during the search process, the search can be divided into static search and dynamic search. Dynamic table search features that the table structure is dynamically generated during the search process. For a given key value, if the table contains a record whose keyword is key, the query is successful and the result is returned. Otherwise, a record with a keyword of the key is inserted.

The binary sorting tree is either an empty tree or a binary tree of the following nature:

(1) if its left subtree is not empty, the value of all nodes on the left subtree is smaller than the value of the root node;

(2) if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of the root node;

(3) Its left and right Subtrees are also binary sorting trees;


1. Data Structure of the binary sorting tree

typedef struct node{short key;struct node *rchild,*lchild;} BSTNode,*BSTree;

2. Insert and generate a binary sorting tree

If a node s whose key value is known is inserted into the binary sorting tree, you only need to ensure that the insertion still complies with the definition of the binary sorting tree. You can use the following method to insert it.

(1) If the binary tree is empty, the key is called the root of the binary tree;
(2) If the binary sorting tree is not empty, the key is compared with the root of the binary sorting tree. If the key value is equal to the root node, the insertion is stopped; if the key value is smaller than the value of the root node, the key is inserted into the left subtree. If the key value is greater than the value of the root node, the key is inserted into the right subtree;

The Binary Tree Insertion Algorithm is as follows:

Void InsertBST (BSTree * bst, short key) {BSTree s; if (* bst = NULL) // recursive end condition; create a node when the node is NULL; {s = (BSTree) malloc (sizeof (BSTNode); s-> key = key; s-> lchild = NULL; s-> rchild = NULL; * bst = s;} else if (key <(* bst)-> key) {InsertBST (& (* bst)-> lchild), key );} else if (key> (* bst)-> key) {InsertBST (& (* bst)-> rchild), key );}} /***************** a binary sorting tree can be created based on the Insertion Algorithm of the binary sorting tree; * **************************/void CreateBST (BSTree * bs T) {char key; * bst = NULL; scanf ("% c", & key); while (key! = '? ') // The End Of The input character is '? '; {InsertBST (bst, key); scanf ("% c", & key );}}

3. Delete the binary sorting tree

It is more difficult to delete a node in the binary sorting tree than to insert a node. Unless it is a leaf node, the interconnection of some links must be considered, to ensure that after deleting a node, the remaining node is still a binary sorting tree.

Assume that the node to be deleted is P, and its parent node is F, which is general, and the node P is the left child of node F (the right child is similar ). As shown in: (Note: As of data structure ).

The following three cases are discussed:

(1) If P is a leaf node, you only need to modify the pointer of the parent node after deleting P;
(2) If the left subtree of P is empty or the right subtree of P is empty, you only need to make the left and right subtree PL and PR become the left and right subtree of its parent node F;

(3) If neither left nor right subtree of P is empty, as shown in 8.7 (a), there are two processing methods. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + records/1TveG146OsyOfNvDguN6OoYqOpy/nKvqOsyLu6872rULXE1/records + cq + records + CjxwPiC3vbeoMqO6ytfPyNXS Release/release + release/nKvqGjPC9wPgo8cD6 + 38zly + O3qMPoyvbI58/Co7o8L3A + release "brush: java;"> void DelBST (BSTree * t, Short key) {BSTNode * p, * f, * s, * q; p = * t; f = NULL; while (p) // query the nodes to be deleted with the key keyword. {if (p-> key = key) {break;} f = p; // f points to the parent node of the p node; if (p-> key) {p = p-> lchild;} else {p = p-> rchild;} if (p = NULL) // if no direct return is found; {return;} if (f = NULL) // if p is the root of the original binary tree; {free (p ); * t = NULL; return;} if (p-> lchild = NULL & p-> rchild = NULL) // if p does not have left and right subtree; {if (f-> lchild = p) {f-> lchild = NULL;} else {f-> rchild = NULL;} free (p );} else if (p-> lchild = NULL & p-> rchild! = NULL) // if the left subtree of p is empty and the right subtree is not empty, {if (f-> lchild = p) {f-> lchild = p-> rchild; // link the right subtree of p to the left subtree of the parent;} else {f-> rchild = p-> rchild; // link the right subtree of p to the parent's right link;} free (p);} else if (p-> lchild! = NULL & p-> rchild = NULL) // if the left subtree of p is not empty, the right subtree is empty; {if (f-> lchild = p) {f-> lchild = p-> lchild; // link the right subtree of p to the left subtree of the parent;} else {f-> rchild = p-> lchild; // link the right subtree of p to the parent's right link;} free (p);} else if (p-> lchild! = NULL & p-> rchild! = NULL) // If neither left nor right subtree of p is empty; {q = p; s = p-> lchild; // s points to the left node of p; while (s-> rchild) {q = s; s = s-> rchild;} p-> key = s-> key; // assign the data of the s node to the p node; if (p = q) // It indicates that the Left subnode of the p node to be deleted does not have the right node; {q-> lchild = s-> lchild; // link the left subtree of the s node to the left node of q;} else {q-> rchild = s-> lchild; // link the left subtree of node s to the right node of node q;} free (s );}}
4. Search for the binary sorting tree

According to the definition of the binary sorting tree, the search on the binary sorting tree is similar to the binary search. That is, when the binary sorting tree is not empty, the given value is first compared with the key value of the root node. If the value is equal, the query is successful. Otherwise, based on the size relationship between the given value and the key value of the root node, the query continues on the left and right Subtrees. The search process is based on the following algorithms:

BSTree SearchBST(BSTree bst,short key){if(bst==NULL){return NULL;}if(bst->key==key){return bst;}else if(bst->key>key){return SearchBST(bst->lchild,key);}else {return SearchBST(bst->rchild,key);}}

5. search and analysis of binary sorting tree

Based on the search algorithm of the binary sorting tree, we can see that the search of the binary sorting tree is similar to that of the Binary Search Tree, and the number of comparisons with keywords does not exceed the depth of the tree, however, the decision tree of a table with a length of n in the binary search is unique, but the decision tree with n nodes is not unique. For a group of nodes with the same keyword sequence, the node insertion sequence is different, and the structure and depth of the binary sorting tree are different. The average search length ASL of the binary tree is related to the form of the binary tree. The more balanced the branches of the binary tree, the lighter the depth of the tree, and the smaller the average search length ASL.

In terms of average performance, the search on the binary sorting tree is not much different from the binary search, and the insertion and deletion nodes on the binary sorting tree are very convenient, so there is no need to move a large number of nodes. Therefore, for tables that require frequent insertion, deletion, and search operations, the binary sorting tree is recommended. Therefore, the binary sorting tree is also called the binary search tree.

6. Summary

The above is my summary of the binary sorting tree during data structure intensive reading. I have also referred to the relevant books. I hope you will give me more advice.
























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.