Treap basic learning and treap basic learning
Working in the company now, the weekend boss asked us to study in the company. because there were no projects recently, we used the data structures we did not have before to learn. Then we saw a Treap this afternoon.
I used to think this is a magical thing. After reading it, it seems that Treap is not difficult.
First, we need to know the binary sorting tree (SBT), which is easy to understand. For example (left small and right big ), each insert operation is to determine whether the number of inserts is smaller or larger than the current node. If the number is smaller, the left subtree is recursive. Otherwise, the right subtree is recursive, he can always find his location and insert it in. Under Random circumstances, it seems that there is no problem, but there may be special circumstances, resulting in the entire book becoming a chain. The red-black tree can solve this problem, but because it is too troublesome, there is a simple data structure that can solve this problem, that is, the Treap ).
The difference between Treap and SBT lies in the addition of a heap, which ensures that the tree shape is unique and avoids degradation.
After the node is inserted, recursive upwards to adjust heap... It is the basic idea of Treap. The specific adjustment method is left and right.
To put it bluntly, if the rank value of the right subnode is large, it will be changed to the root node, and then the current root node will be changed to the left subnode. At the same time, then adjust the left subnode of the right subnode (for the specific reason, just take a look at the drawing ). And vice versa.
With such a Logn complex operation, it basically ensures that the depth will not degrade very seriously (of course it cannot reach the absolute logn ).
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.