Binary search tree

definition

1 called a binary search tree (Binary search tree) T, is either an empty tree, or is r = (key, value) as the root of the binary tree, and its left and right sub-trees are binary search tree , when n in the left subtree of r, all nodes (if any) of the key are not greater than the key; o in the right subtree of r, all nodes (if any) of the key is not less than key. 

 

T is a binary tree binary search tree, wherein if and only if the sequence preorder monotonic nondecreasing. 

 

Search algorithm

Binary search tree algorithm is to find ideas: starting from the root, in the form of recursive shrinking Look, until finding the target entry (search is successful) or narrow to find an empty tree (lookup fails). 

 

 

At each level of recursion, once the current node is null, then the Look is already empty, the lookup fails; otherwise, we will target key code and key code of the current node to do comparison. Ordered lookup table similar to the binary search process, the comparison here is nothing more than three possibilities:

1. The larger the target key (for example at node 36). At this point, you can drill the right subtree to do recursive lookup;

2. The target key is smaller (for example at the nodes 93 and 58). At this time, in-depth left subtree to do recursive lookup;

3. equal (for example at node 46). At this algorithm to "find a successful" end. 

 

 

, Binary search trees in general without any restriction of its height, in the worst case, a binary search tree of n nodes constituting the height can reach Θ (n). As shown in Figure VII.5, when all nodes have no children left, it is such an extreme situation. In this case, the search operation may be necessary in the bad case of linear time ⎯⎯ This is not surprising, because in fact, such a "binary" search tree has been degraded to a fully-fledged ordered lookup table.

Search algorithm

 

Insertion algorithm

 

 

 

Deletion algorithm

Guess you like

Origin www.cnblogs.com/CherryTab/p/12056755.html