1. What is a two-fork search tree
Binary search tree is an ordered two-fork tree, so we can also call it a two-fork sort tree (do not know the two-tree children's shoes, first look at the binary tree: portal ). A two-fork tree with the following properties we call the two-fork search tree: If its left subtree is not empty, then all values on the left subtree are smaller than its root node, and if its right subtree is not empty, all values on the right subtree are greater than its root node. Its Saozi right sub-tree is also a two-fork search tree.
2. Binary Search tree structure
Binary search tree can be performed efficiently: ① inserts a numeric value ② whether the query contains a value ③ delete a value
Depending on the implementation, you can also implement a variety of other operations, which is a highly used data structure. Let's look at an example:
This is the binary search tree storage structure, all the nodes, are satisfied with the left subtree on the smaller than their own, and the right subtree on all nodes than their own larger. Binary search tree because of its order, so it can efficiently manage the collection of numbers
(1) Enquiry
We look for the existence of 17:
The <1> root node is 7 because it is less than 17, so go to the right subtree to find
<2> go to Node 12, or less than 17, so continue to the right subtree to find
<3> go to Node 17 and find 17 at this point.
(2) Insert
We use the search method to insert, the number 6 for example,:
(3) Delete
The deletion operation is slightly more complex than the other two operations that preceded it. In general, we can be divided into three situations:
<1> the node that needs to be removed there is no left son , so bring up the right son .
<2> the left son of the node that needs to be removed has no right son , then bring the left son up
<3> does not meet the above two cases, the largest node in the left Dial hand tree is placed on the node to be deleted.
3. The complexity of binary search tree
Whichever action we take, the time it takes is proportional to the height of the tree. It is not difficult to know that the average complexity of the binary search tree is O (log n).
4. Realization of binary search tree
Through the above understanding, we have roughly known the binary search tree working principle. So now we come to a simple implementation of the two-fork search tree Basic additions and deletions to check the function, the code is as follows:
struct body struct node{int val representing the node; Node *lch, *rch;};/ /Insert integer XNode *insert (node *p, int x) {if (p = = NULL) {Node *newnode = new node; Newnode->val = x; Newnode->lch = Newnode->rch = NULL; p = newNode; }else{if (x < p->val) P->lch = insert (P->lch, x); else P->rch = insert (P->rch, x); } return p;} Find integer Xbool Find (node *p, int x) {if (p = = NULL) return false; else if (P->val = = x) return true; else if (P->val > x) return Find (P->lch, x); else return find (P->rch, x);} Delete integer xnode *remove (node *p, int x) {if (p = = null) return null; else if (x < p->val) P->lch = Remove (P->lch, x); else if (x > P->val) p->rch = Remove (p->rch, x); Case <1> Else if (P->lch = = NULL) {Node *q = p->rch; Delete p; return q; }//Conditions <2> else if (P->lch->rch = = NULL) {Node *q = p->lch; Q->rch = p->rch; Delete p; return q; }//conditions <3> else {node *q; for (q = p->lch; q->rch->rch! = NULL; q = q->rch); Node *r = q->rch; Q->rch = r->lch; R->lch = p->lch; R->rch = p->rch; Delete p; return R; } return p;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Binary search Tree