Skip to content

Instantly share code, notes, and snippets.

@sampathsl
Created June 21, 2020 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sampathsl/1f07eaad16079e9901358df91812f41a to your computer and use it in GitHub Desktop.
Save sampathsl/1f07eaad16079e9901358df91812f41a to your computer and use it in GitHub Desktop.
Treaps - Insertion
// 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