VOOZH about

URL: https://www.geeksforgeeks.org/dsa/implementation-of-search-insert-and-delete-in-treap/

⇱ Implementation of Search, Insert and Delete in Treap - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementation of Search, Insert and Delete in Treap

Last Updated : 23 Jul, 2025

We strongly recommend to refer set 1 as a prerequisite of this post.
Treap (A Randomized Binary Search Tree)
In this post, implementations of search, insert and delete are discussed.
Search: 
Same as standard BST search. Priority is not considered for search. 

Insert
1) Create new node with key equals to x and value equals to a random value.
2) Perform standard BST insert.
3) A newly inserted node gets a random priority, so Max-Heap property may be violated.. Use rotations to make sure that inserted node's priority follows max heap property.
During insertion, we recursively traverse all ancestors of the inserted node. 
a) If new node is inserted in left subtree and root of left subtree has higher priority, perform right rotation.
b) If new node is inserted in right subtree and root of right subtree has higher priority, perform left rotation.

Delete:
The delete implementation here is slightly different from the steps discussed in previous post
1) If node is a leaf, delete it. 
2) If node has one child NULL and other as non-NULL, replace node with the non-empty child. 
3) If node has both children as non-NULL, find max of left and right children. 
....a) If priority of right child is greater, perform left rotation at node 
....b) If priority of left child is greater, perform right rotation at node.
The idea of step 3 is to move the node to down so that we end up with either case 1 or case 2.

A Complete Program to Demonstrate All Operations


Output
...eft child: 20 | right child: 40
key: 40 | priority: %d 87 | right child: 60
key: 50 | priority: %d 46
key: 60 | priority: %d 62 | left child: 50 | right child: 80
key: 70 | priority: %d 10
key: 80 | priority: %d 57 | left child: 70

Delete 20
Inorder traversal of the modified tree 
key: 30 | priority: %d 92 | right child: 40
key: 40 | priority: %d 87 | right child: 60
key: 50 | priority: %d 46
key: 60 | priority: %d 62 | left child: 50 | right child: 80
key: 70 | priority: %d 10
key: 80 | priority: %d 57 | left child: 70

Delete 30
Inorder traversal of the modified tree 
key: 40 | priority: %d 87 | right child: 60
key: 50 | priority: %d 46
key: 60 | priority: %d 62 | left child: 50 | right child: 80
key: 70 | priority: %d 10
key: 80 | priority: %d 57 | left child: 70

Delete 50
Inorder traversal of the modified tree 
key: 40 | priority: %d 87 | right child: 60
key: 60 | priority: %d 62 | right child: 80
key: 70 | priority: %d 10
key: 80 | priority: %d 57 | left child: 70

50 Not Found 

Explanation of the above output:

Every node is written as key(priority)

The above code constructs below tree
 20(92)
 \
 50(73)
 / \
 30(48) 60(55) 
 \ \
 40(21) 70(50)
 \
 80(44) 


After deleteNode(20)
 50(73)
 / \
 30(48) 60(55) 
 \ \
 40(21) 70(50)
 \
 80(44) 
 

After deleteNode(30)
 50(73)
 / \
 40(21) 60(55) 
 \
 70(50)
 \
 80(44) 
 

After deleteNode(50)
 60(55)
 / \
 40(21) 70(50) 
 \
 80(44) 


Thanks to Jai Goyal for providing an initial implementation.

Comment
Article Tags: