Preface
This series is the review notes for graduation work. I hope that I can lay a solid foundation with the children's shoes that guangdazheng is preparing to graduate and meet all kinds of written examinations ...... In order to cope with the written test in both Chinese and English, the keywords are marked in English, so that you are not afraid to face English questions. The reason why I started this series was that when I took the Microsoft test, I was stuck by a stable sorting multiple choice question, so I found out when my basic skills became so bad. Now that you are looking for a job, you have to review it from the very beginning.AlgorithmPart of the notes from the Algorithm Design Manual.
Structural Features
A binary search tree is characterized by a small value on the left and a large value on the right, that is
For example:
The advantage of such a structure is that it is easy to obtain the maximum value (Maximum), Minimum value (Minimum), The precursor of an element (Precursor), The successor of an element (successor ).
Maximum: the rightmost node of the tree.
Minimum value: the leftmost node of the tree.
Element precursor: rightmost of the Left subtree.
The successor of an element: the leftmost part of the right subtree.
Basic operations
Basic operations of the Binary Search Tree include searching, traversal, insertion, and deletion.
(CodeIn order to save the trouble of writing code according to the standard, please follow the standard when writing code.)
① Searching
TREE * search_tree (tree *L, item_type X ){If(L =Null)ReturnNULL;If(L-> item = X)ReturnL;If(X <L->ITEM ){Return(Search_tree (L->Left, x ));}If(X> L->ITEM ){Return(Search_tree (L->Right, x ));}}
The time complexity is O (h), and H is the height of the tree.
② Traversal
Because the small node is on the left and the large node is on the right, you can use in-order traversal to conveniently obtain a sorted list.
VoidTraverse_tree (tree *L ){If(L! =Null) {traverse_tree (L->Left); process_item (L->ITEM); traverse_tree (L->Right );}}
The time complexity is O (n), and N is the sum point of the tree.
③ Insertion
Insert_tree (tree ** L, item_type X, tree * Parent) {tree * P; /* Temporary pointer */ If (* L =Null) {P = Malloc ( Sizeof (Tree); P -> Item = X; P -> Left = p-> right = NULL; P -> Parent = Parent; * L = P; Return ;} If (X <(* l)-> ITEM) {insert_tree ( & (* L)-> left), X ,* L );} Else {Insert_tree ( & (* L)-> right), X ,* L );}}
The time complexity is O (h), and H is the height of the tree.
④ Deletion
There are three situations when deleting a node:
1) the node to be deleted is a leaf node.
Then you can directly delete it.
2) the node to be deleted has a subnode.
Delete the node and replace it with its unique sub-node.
3) the node to be deleted has two subnodes.
Find the minimum node K of the right subtree of the node and replace the K with the node to be deleted.
In the worst case, the time complexity is the moving overhead of the O (h) + pointer.
Advanced
It can be seen from the above that the time complexity of dictionary operation (including search, insertion, and deletion) of the Binary Search Tree is related to O (h), and H is the height of the tree (log n ), if the tree is constructed according to the insertion method described above, the shapes of the constructed tree vary, especially when the input sequence is ordered, it degrades to the degree of linked list. Therefore, if a method is used to minimize the height of a tree, the time overhead of its dictionary operation can be reduced, but the overhead of building a tree will increase. In order to reduce the height of the binary search treeBalanced Binary Tree(Balanced binary tree) concept. It requires that the absolute value of the height difference between the left and right Subtrees should not exceed 1, and both left and right Subtrees are a balanced binary tree. In this way, the height of the search tree can be minimized. Common algorithms include red/black trees, aVL, treap, and stretch trees.