Treap basic learning and treap basic learning

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.