Algorithms Series 15 Days Quick - Sixth Days Five Classics Search
Do you feel that trees are prevalent in the data structure, and that every field should be touched and touched?
Take the sort we learned a few days ago and use the heap and today's "binary sort tree," so radically say, master the tree you are a cowman.
Today I'm going to talk about the "Find the Last Binary Sorting Tree" of the five classics.
1. concept:
In fact, <1> is very simple. If the root node has a left subtree, all the nodes of the left subtree are smaller than the root node.
If the root node has a right subtree, all nodes of the right subtree are larger than the root node.
The <2> figure is a "binary sort tree", and then compare with the concept.
2. Actual operation:
As we all know, to operate a thing is nothing more than adding, deleting, checking and modifying. Next, we will talk about the basic operation.
<1> Insertion: I believe you all know the concept of "sort tree", then the principle of insertion is very simple.
Let's say we insert a 20 into the tree.
First of all, 20 to 50 ratio, found that 20 is old and young, had to be boiled down to 50 left subtrees to compare.
Then: 20 to 30, we find that 20 is still young and old.
Then: 20 to 10, find yourself the eldest, and insert it into the right subtree of 10.
Finally, the effect rendering chart is as follows:
Finding: Believe that if you understand insertion, it's easy to understand.
Take the picture above, for example, I want to find node 10.
First of all, when 10 is compared with 50, it is found that 10 is young and old, and it is found in the left subtree of 50.
Then: 10 to 30, if you find that you are old or young, look in the left subtree of 30.
Then: 10 to 10, find the same, and then return to the signal found.
Delete < 3 >: Delete nodes in the tree is more troublesome, there are three main cases.
Delete "leaf node 20" in "1". This situation is relatively simple. Delete 20 will not destroy the structure of the tree. As shown in the picture:
In "2" delete "single child node 90", which is a little more troublesome than the first one, and need to put his child on top.
The deletion of "50 nodes for both right and left children" in "3" has made me tangle with coding for a long time, and the problem is very straightforward.
I deleted 50. Who had the problem, the left child? Or the right child? Or something else? Here I will
Frankly, I don't know if you know the middle order traversal of the binary tree, but I'll talk about it later. Now it's OK.
Keep in mind that the formula is to find the left child of the left subtree of the right node.
For example, first find 50 right children, 70.
Then find the leftmost child of 70 and return to him if he finds none.
Finally, the original and final drawings are as follows.
3. Having said so much, talk to the code.
Operation results:
Note that the binary sorting tree also uses the "space for time" approach.
Suddenly found that the middle order traversal of binary sort tree can also sort arrays, ha ha ha, good!
PS: Insert operation: O(LogN).
Delete operation: O(LogN).
Find operation: O(LogN).