# Treap Data Structure

Treap data structure is a hybrid of a binary search tree and a heap. The treap and the randomized binary search tree are two binary search tree data structures that keep a dynamic set of ordered keys and allow binary searches among the keys.

The structure of the tree is a random variable with the same probability distribution as a random binary tree after any sequence of key insertions and deletions; in particular, with high probability, its height is proportional to the logarithm of the number of keys, so that each search, insertion, or deletion operation takes logarithmic time to perform.

For better understanding the Treap Data structure, we should understand the underlying data structure of the Treap, which are the Binary search tree and the heap data structure.

### Binary Search Tree

Node deletions and additions can cause the tree to become imbalanced (heavier on sides; therefore, the property we value about BSTs, the ability to distribute data by equal divisions, goes out of whack). As a result, we must rebalance that tree (which takes O (n) time for a tree with n components). For a long time, computer scientists utilized balanced search trees, which included rotation algorithms to maintain it balanced after adding and deleting nodes, such as red-black trees, splay trees, AVL trees, etc.

B-trees hold more information per node than simply one element. These were supplemented with certain improvements. For example, splay trees shift the most often visited components to the top and rebalances. This adds complexity to the algorithm by providing the benefits of a standard BST.

### Heap

Heaps are just binary trees with a "heap" feature. (A max-heap occurs when all of the tree's parents have keys larger than or equal to the keys of its offspring.) This trait is why it is a good data structure for implementing priority queues (if we delete one element, we must pick between two children to determine who takes the missing element's position). Arrays can logically store heaps, which are a plus (random access times).

A treap data structure is a hybrid of heaps and binary search trees. When we create a heap, we are essentially creating an ordered binary tree that also satisfies the "heap" characteristic. Suppose only one element is used. In that case, it will appear as a line (because in the BST, the left child must be less than its parent and the right child must be greater than or equal to its parent, but for a heap, each parent should either be all bigger than its children or all smaller than its children).

The numbers represent the heap arrangement of the data structure (in max-heap order), while the alphabets represent the tree part of things (left child-parent = right child). So we have a tree and a heap now. Here is the magical attribute of the treap data structure. Regardless of the sequence in which the tree's elements were chosen, there is only one configuration for this tree.

Use a random heap weight to make that second key more valuable to us. As a result, the tree's structure (shape) will now be determined by the randomized weight we assigned to the heap values. We acquire randomized heap priority by ensuring that these are assigned at random (one way is by hashing the keys themselves). To insert a new key, calculate the priority and do conventional BST insertions before rotating it up the tree (to maintain the heap). To delete a key, increase its weight to infinity and rotate it down the tree (again, in heap order) until it becomes a leaf, then remove it.

So it's a tree in terms of keys and a heap in terms of priority. The theory is that re-heapifying the tree will almost certainly maintain it balanced (height will be c.log(n)) since a random binary search tree has logarithmic height.

## Importance of Treap Data Structure

Below are the following reasons to show the importance of treap data structure.

• As previously stated, it is a self-organizing data structure. They take after themselves and do not require supervision. Unlike other self-balancing trees, they do not require sophisticated algorithms (simple tree rotations will suffice, although simpler algorithms involving arrays can do the job too).
• It is a randomized data structure as well.
• Because of the randomized weights, there is a strong possibility that the tree will be balanced regardless of the sequence in which we add, remove, etc. (a random binary search tree has logarithmic height).
• It is simply a binary search tree, therefore to print a sorted order of keys, traverse it in the same way as we would conventional BSTs. Searching for a treap is similar to searching for a tree.
• A treap is a data structure that combines probability, randomization, two prominent data structures, and other factors to create a useful data structure.

## C++ Code

Let's write a C++ code to implement treap data structure.

Output

The above C++ code gives the following output.

```Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
1
Enter the value of the node to be inserted
98

Do you want to continue (Type y or n)
y

Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
1
Enter the value of the node to be inserted
32

Do you want to continue (Type y or n)
y

Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
1
Enter the value of the node to be inserted
22

Do you want to continue (Type y or n)
y

Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
1
Enter the value of the node to be inserted
10

Do you want to continue (Type y or n)
y

Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
1
Enter the value of the node to be inserted
67

Do you want to continue (Type y or n)
y

Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
1
Enter the value of the node to be inserted
30

Do you want to continue (Type y or n)
y

Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
2
Contents of the Treap Data Structure are::

10(54) > 22(12) > 30(23) > 32(81) > 67(86) > 98(19)

Do you want to continue (Type y or n)
y

Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
3
Enter the value of the node that you want to delete::
30
o you want to continue (Type y or n)
y

Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
4
Enter the word to search
98
98 found in Treap.

Do you want to continue (Type y or n)
y

Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
4
Enter the word to search
69

Do you want to continue (Type y or n)
y

Select one of the operations for the Treap Data Structure::
1. To insert a new node in the Treap Data Structure.
2. To display the nodes of the Treap Data Structure.
3. To Delete a node from the Treap Data Structure.
4. To search a node from the Treap Data Structure.
4
Enter the word to search
67
67 found in Treap.

Do you want to continue (Type y or n)
n
```

In the above C++ code, we have written different method functions for the different functionalities of the Treap data structure, like adding a new node to the treap, searching an already existing node, to count the number of nodes that are present in the treap at that time, to check whether the treap data structure is empty or not and then to make the treap data structure empty.

Once these functions are written, a menu-driven program calls all these functions and then sequentially adds data in the treap data structure, followed by the searching operation and printing operation on the created treap data structure.

## Java Code

Now let's see the implementation of treap data structure with the help of Java code.

Output

The above Java code gives the following output.

```Treap Test

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
1
Enter integer element to insert
10

Post-Order : 10
Pre-Order: 10
In order: 10
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
1
Enter integer element to insert
22

Post-Order: 10 22
Pre-Order: 22 10
In order: 10 22
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
1
Enter integer element to insert
36

Post-Order: 10 22 36
Pre-Order: 36 22 10
In order: 10 22 36
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
1
Enter integer element to insert
75

Post-Order : 10 22 75 36
Pre-Order : 36 22 10 75
In order : 10 22 36 75
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
1
Enter integer element to insert
62

Post-Order : 10 22 62 75 36
Pre-Order : 36 22 10 75 62
In order : 10 22 36 62 75
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
1
Enter integer element to insert
89

Post-Order : 10 22 62 75 89 36
Pre-Order : 36 22 10 89 75 62
In order : 10 22 36 62 75 89
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
1
Enter integer element to insert
44

Post-Order : 10 22 44 62 75 89 36
Pre-Order : 36 22 10 89 75 62 44
In order : 10 22 36 44 62 75 89
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
2
Enter integer element to search
27
Search result: false

Post-Order : 10 22 44 62 75 89 36
Pre-Order : 36 22 10 89 75 62 44
In order : 10 22 36 44 62 75 89
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
3
Nodes = 7

Post-Order : 10 22 44 62 75 89 36
Pre-Order : 36 22 10 89 75 62 44
In order : 10 22 36 44 62 75 89
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
4
Empty status = false

Post-Order : 10 22 44 62 75 89 36
Pre-Order : 36 22 10 89 75 62 44
In order : 10 22 36 44 62 75 89
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the TreapData Structure is empty?.
5. To make Treap Data Structure empty.
5

Treap Cleared

Post-Order :
Pre-Order :
In order :
Do you want to continue (Type y or n)

y

Select one of the operations for the Treap Data Structure

1. To insert a new node in the Treap Data Structure.
2. To search a node from the Treap Data Structure.
3. To count nodes in the Treap Data Structure.
4. To check if the Treap Data Structure is empty?.
5. To make Treap Data Structure empty.
4
Empty status = true

Postorder :
Pre-order :
In order :
Do you want to continue (Type y or n)

N
```

In the above Java code, we have written different method functions for the different functionalities of the Treap data structure, like adding a new node to the treap, searching an already existing node, to count the number of nodes that are present in the treap at that time, to check whether the treap data structure is empty or not and then to make the treap data structure empty.

Once these functions are written, a menu-driven program calls all these functions and then sequentially adds data in the treap data structure, followed by the searching operation and printing operation on the created treap data structure.