Javatpoint Logo
Javatpoint Logo

Implementation of Search, Insert and Delete in Treap using Python

Introduction

A Treap is a special and effective data structure that combines the qualities of a Max Heap and a Binary Search Tree (BST). Each node in a Treap keeps two key values: one for guaranteeing a heap property and the other for maintaining the order, much like a BST. The heap property is generally defined by randomly allocating priorities to nodes during insertion. This combination offers Treap quick search, insert, and delete operations with an anticipated O(log N) time complexity.

Key Components of a Treap

  • Key: This represents each node's main value or piece of data. It adheres to the ordering property much like a BST.
  • Priority: The max-heap property is maintained when the priority value is randomly assigned during insertion. It ensures that nodes occupy the top of the tree with greater priorities.

Operations in Treap

  • Insertion: A new node is inserted into a Treap at the correct position according to its key while keeping the max-heap property by priority.
  • Deletion:To delete a node from a tree, locate the node with the target key and restructure the tree so that the BST and max-heap attributes are preserved.
  • Search: The BST property is followed while looking for a certain key in a Treap, making search operations effective.

1. Insert Operation

A new node with a key and a randomly chosen priority must be inserted into a Treap while preserving the binary search tree (BST) and the maximum heap (max-heap) properties.

  • Based on the key & priority values, this recursive function traverses the Treap to determine the ideal location for the new node. Additionally, it rotates as required to preserve the max-heap attribute.
  • To add a new key to the Treap, use this public method. After generating a random priority value, it runs the _insert method to add the new node.

2. Delete Operation

Finding the node containing the target key, removing it, and reorganizing the tree to preserve the binary search tree (BST) and max-heap attributes are the steps involved in deletion in a Treap.

  • _delete is a recursive function that searches the Treap for the node containing the target key, deletes it, and rearranges the tree as necessary to preserve the Treap's properties.
  • Use this public method to remove a node from the Treap with a certain key. It modifies the root of the Treap and invokes the _delete method.

3. Search Operation

Searching in a Treap entails traversing the tree by the binary search tree (BST) attribute to discover the node containing the target key.

  • The binary search tree (BST) attribute is used by the recursive function _search to navigate the Treap and locate the target key node. If a node is found, it returns it; otherwise, it produces None.
  • This is how the general public searches for keys in the Treap. Starting at the root of the Treap, it runs the _search function and then returns the outcome.

Code

Output:

Inorder traversal: [1, 2, 4, 5, 7, 8, 9]
Inorder traversal after deleting 5: [1, 2, 4, 7, 8, 9]
Key 7 found in the Treap.

Advantages

  • Treaps retain a balanced structure by default, ensuring that operations like search, insert, and delete have average O(log N) time complexity. This helps dynamic datasets very well.
  • Treaps' balancing system allows for effective insertion and deletion operations. In contrast to certain self-balancing BSTs, including random priorities reduces the likelihood of efficiency degradation in worst-case scenarios.
  • Treaps are adaptable and are used across a variety of industries. Task scheduling, randomized algorithms, and priority queues are examples of applications that excel where exploring and prioritization are necessary.
  • Compared to certain other balanced tree structures, Treaps have an implementation that is quite simple. Concise code can implement fundamental actions like insertion, deletion, and searching.

Disadvantages

  • The standard of its random priorities significantly influences the effectiveness of Treaps. In practice, it might be difficult to generate truly random priorities, and poorly determined priorities can impact the balance and effectiveness of the tree.
  • Treaps have non-deterministic behaviour as a result of their reliance on random priority. The generated priority can cause the same set of keys to produce several tree architectures. Analysis and debugging may become more difficult as a result.
  • It might not be easy to guarantee that operations are performed correctly in a multi-threaded environment. Concurrent access to the Treap necessitates synchronization techniques, thereby complicating the implementation.
  • Treaps are effective in many situations but are only sometimes the best option. Other data structures, such as AVL or Red-Black trees, may offer more dependable and consistent performance in particular cases.

Time and Space Complexity

For efficient insertion, deletion, and search operations, Treaps offers time complexities of O(log N). If key-priority pairs are stored in the nodes, the worst-case space complexity, where N is the number of keys in the Treap, is O(N). Treaps are excellent for various applications that call for prioritised and ordered storage because they mix performance and simplicity.

Conclusion

Treaps are an intriguing and adaptable data structure combining Max Heaps and Binary Search Trees (BSTs) benefits. They have a consistent structure, making them ideal for dynamic datasets with constantly added and removed pieces. This balancing ensures that the average time complexity of the search, insert, and delete operations is O(log N), essential for effective data manipulation. The effectiveness of Treaps in deletions and insertions is one of their most noticeable benefits. Treaps rely on the random priorities allocated to each node upon insertion, in contrast to some self-balancing BSTs that, in the worst-case scenario, may experience performance concerns. By balancing the tree naturally, this randomization lowers the possibility of abnormal cases and ensures reliable performance.

Treaps are useful in many different fields because of their adaptability. They excel in situations where prioritization and ordering are crucial. They are utilised, for instance, in priority queues, randomized algorithms, and work scheduling. They are available to programmers looking for effective methods to manage prioritised ordered data because of their implementation's relative simplicity. Additionally, it can be difficult to guarantee operations' accuracy in multi-threaded settings. Concurrent access requires synchronization techniques, which could make the implementation more difficult. Treaps provide a balanced and adaptable data structure with effective operations, but their use needs to be carefully assessed in light of each application's unique requirements and characteristics. Other data structures like AVL or Red-Black trees may offer more consistent and predictable performance in certain circumstances.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA