Data structure of Treap detailed _c language

Source: Internet
Author: User

1. Overview

Like splay trees, treap is also a balanced binary tree, but Treap records an extra data, which is priority. The treap of a binary search tree is based on the key code, and the properties of the heap are also met by priority. Thus, Treap=tree+heap. It should be noted here that Treap is not a binary heap, the binary heap must be a complete binary tree, and treap can not necessarily be.

2. Treap Basic operation

In order for the nodes in Treap to satisfy both the BST property and the minimum heap property, it is inevitable to adjust its structure, and the adjustment method is called rotation. In the maintenance of treap, there are only two kinds of rotation, namely, left rotation (left-handed) and right rotation (referred to as right).
A subtree that rotates its root node to the left subtree of the root. At the same time, the right child node of the root node becomes the root of the subtree, and the right one subtree rotates its root node to the right subtree of the root, while the left child node of the root node becomes the root of the subtree.

struct Treap_node
 
{
 
 treap_node *left,*right;//Pointer to the left and right subtree of the node
 
 int value,fix;//node value and precedence
 
};
 
void Treap_left_rotate (Treap_node *&a)//L-node pointer must pass reference
 
{
 
 Treap_node *b=a->right;
 
 a->right=b->left;
 
 b->left=a;
 
 a=b;
 
}
 
void Treap_right_rotate (Treap_node *&a)//Right rotation node pointer must pass reference
 
{
 
 Treap_node *b=a->left;
 
 a->left=b->right;
 
 b->right=a;
 
 a=b;
 
}

3. Operation of Treap

Like other tree structures, Treap's basic operations are: Find, insert, delete, and so on.

3.1 Find

Like other two-fork trees, the treap lookup process is a binary lookup process, with a complexity of O (LG N).

3.2 Insert

Inserts an element into the treap, similar to the insertion method in BST. First find the appropriate insertion location, and then create a new node to store the elements. But be aware that the new node will have a priority attribute that may break the heap order, so we need to rotate it appropriately. The specific methods are as follows:

1. Insert from root node;
2. If the value to be inserted is less than or equal to the current node's value, insert in the left subtree of the current node and, if the priority of the Zoozi node is less than the priority of the current node, rotate the current node to the right;
3. If the value to be inserted is greater than the current node's value, insert in the right subtree of the current node, and if the priority of the right child node is less than the priority of the current node, the current node is left;
4. If the current node is an empty node, a new node is established here, the value of the node is the value to be inserted, the left and right subtree is empty, and the insert succeeds.

Treap_node *root;
 
void Treap_insert (Treap_node *&p,int value)//node pointer must pass the reference
 
{
 
 if (!) P)//Find location, set up node
 
 {
 
  p=new treap_node;
 
  p->value=value;
 
  P->fix=rand ();//Generate a random correction value
 
 }
 
 else if (value <= p->value)
 
 {
 
  treap_insert (p->left,r);
 
  if (P->left->fix < P->fix)
 
   treap_right_rotate (P);//Zoozi node correction value is less than current node correction value, right rotation current node
 
 }
 
 Else
 
 {
 
  Treap_insert (p->right,r);
 
  if (P->right->fix < P->fix)
 
   treap_left_rotate (P);//Right child node correction value is less than current node correction value, left rotation current node}
 
}

3.3 Delete

As with BST, it takes a variety of circumstances to remove elements from treap. We can delete the element in the treap by deleting it in the same way as the element in the BST, replacing it with the value of its successor (or predecessor) node, and then deleting its successor (or predecessor) node.

The above method expects the time complexity to be O (Logn), but this method does not make full use of the existing stochastic properties of treap, but instead chooses the node randomly. We give a more general method of deletion, which is based on rotation adjustment. First find the location of the node to be deleted in the treap tree, and then discuss it in a separate situation:

Case one, the node is a leaf node or a chain node, then the node is a node that can be deleted directly. If the node has a nonempty node and replaces the node with a Non-empty node, the node is replaced with an empty node, and then the node is deleted.

In case two, the node has two nonempty nodes. Our strategy is to rotate the node into a node that can be deleted directly. If the left child node of the node has a priority less than the priority of the right child node, right-turn the node to the root node of the right subtree, and then access the root node of the right subtree to continue the discussion, instead, turn the node left to the root node of the Zuozi, and then access the root node of the left subtree to continue, Until it becomes a node that can be deleted directly.

Bst_node *root;
 
void Treap_delete (Treap_node *&p,int *value)//node pointer to pass reference
 
{
 
 if (value==p->value)////Find the node to delete to delete
 
 {
 
  if (! P->right | | ! P->left)//condition One, the node can be deleted directly
 
  {
 
   treap_node *t=p;
 
   if (! P->right)
 
    p=p->left;//replace it with the Zoozi node
 
   else
 
    p=p->right;//replace it with the right child node
 
   delete t;//delete the node
 
  }
 
  else//Situation two
 
  {
 
   if (P->left->fix < P->right->fix)//Zoozi node correction is smaller, right spin
 
   {
 
    treap_right_rotate (P);
 
    Treap_delete (p->right,r);
 
   }
 
   else//Zoozi node correction is smaller, l
 
   {
 
    treap_left_rotate (P);
 
     Treap_delete (p->left,r);
 
 }} else if (value < P->value)
 
  Treap_delete (p->left,r);//Find the node to delete in the left subtree
 
 else
 
  treap_delete (p-> RIGHT,R); Locate the node you want to delete in the right subtree
 


4. Treap Application
treap can solve all the problems splay tree can solve, specifically see another article: "The structure of the extension of trees detailed"

You can define a struct body like this:

struct Treap_node
 
{treap_node *left,*right;//Pointer to the left and right subtree of the
 
 node
 
 int value,fix,weight,size;//node value, priority, Repeat count (log the same number of nodes, save space), subtree size
 
 inline int lsize () {Return to left left->size? 0;///returns the number of nodes in the left-hand subtree
 
 inline int rsize () {RE Turn right?right->size?0; }//Returns the number of nodes in the right subtree
 
;

5. Summary

Treap, as a concise and efficient ordered data structure, plays an important role in computer science and technology application. It can be used to implement the container-type data structure of collection, multiple sets and dictionaries, and can also be used to design dynamic statistical data structures.

6. Reference materials

(1) Treap:http://www.nocow.cn/index.php/treap
(2) Analysis and application of random balanced binary lookup tree treap: http://www.byvoid.com/blog/wp-content/uploads/2010/12/treap-analysis-and-application.pdf

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.