Binary search Tree
Defined:
Binary search tree is organized by a binary tree. The keywords in binary search are always stored in a way that satisfies the nature of binary tree search.
Set X is a node in a two-fork tree. If Y is a node in the X left dial hand tree, then Y.key <= X.key.
If Y is a node in the X right subtree, then Y.key >= X.key.
Analysis of the algorithm:
If we choose the middle order to traverse the entire binary tree. For example
6
5 7
2 5 8
Such a binary tree, (the middle line itself imagines).
Inorder-tree-walk
1 if x! = NULL
2 Inorder-tree-walk (x, left)
3 Print X.key;
4 Inorder-tree-walk (x, right)
Very simple we can get the results of the sequence traversal of the tree: 2 5 5 6 7 8
The time to traverse a two-fork search tree with n nodes is: Θ (n)
Prove:
Use T (n) to indicate the time required, since the middle order requires access to all n nodes of the tree, so there is t (n) =ω (n), as long as the t (n) = O (n) is shown below.
For an empty tree, you need to consume a very small constant time C, for a constant,c>0; has t (0) = C;
n > 0, if there are k nodes on the left subtree of x, then there are n-k-1 on the right subtree;
For t (n) there is t (n) <= t (k) + t (n-k-1) +d; D >0;
Use substitution method: T (N) <= (c+d) n +c; It can be concluded that T (n) = O (n);
Binary search Tree