Skip to content

Adding Treap Data Structure #215

Closed
@Aimaanhasan

Description

@Aimaanhasan

Description of the data structure

A Treap is a randomized binary search tree. It is different from random binary search tree, because Treap allows to have add(x) and delete(x) operations. A node in a Treap is similar to a node in BST, as it has a data value, but also has a unique random priority attached to it. With this priority, it follows the heap property, i.e, every node's priority should be greater than the priority of its parent.

Add operation

A node is added into a Treap in a similar way to a Binary Search Tree, with comparing it's data value with each node, and getting added as a leaf. However, this node has a unique priority attached to it, which is assigned randomly on its creation. To justify the heap property, the node is then bubbled up into the tree using rotations.

Delete operation

A node is deleted from the Treap, by first trickling down the node by using rotations, until the node becomes a leaf and then it is removed from the tree.

Time Complexity

Both add and delete operations use find operation similar to BST, which takes O(logn) operation, and then the expected/amortized time to do rotations is O(1). Thus it takes O(logn) time.

Advantages of Treap

It allows to have a balanced binary search tree, resulting in efficient operations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions