Created
June 21, 2020 08:32
-
-
Save sampathsl/1f07eaad16079e9901358df91812f41a to your computer and use it in GitHub Desktop.
Treaps - Insertion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// base case | |
if (root == nullptr) | |
{ | |
root = new TreapNode(data); | |
return; | |
} | |
// if given data is less than the root node, insert in the left subtree | |
// else insert in the right subtree | |
if (data < root->data) | |
{ | |
insertNode(root->left, data); | |
// rotate right if heap property is violated | |
if (root->left != nullptr && root->left->priority > root->priority) | |
rotateRight(root); | |
} | |
else | |
{ | |
insertNode(root->right, data); | |
// rotate left if heap property is violated | |
if (root->right != nullptr && root->right->priority > root->priority) | |
rotateLeft(root); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment