Data Structures CIA – II
Data Structures –
- Van Emde Boas Tree
- Treap
Roles – Project manager: Madhu Shraya
Business Analyst: Bharat Kameswaran
Developers: Ashwin V Madhu Shraya Gaurav Mahesh Manasvi Niharika
Tester: Mohammed Farhan K
Project Overview –
Introduction: The project focuses on the implementation and analysis of two advanced data structures: the Van Emde Boas Tree and the Treap. Both data structures are essential tools in computer science for efficient storage, retrieval, and manipulation of data. Objective: The primary objective of the project is to understand the principles, implementation, and performance characteristics of the Van Emde Boas Tree and Treap data structures. Key goals include implementing the data structures, analyzing their time complexities, and evaluating their suitability for various applications. Implementation: The project includes complete implementations of both the Van Emde Boas Tree and Treap data structures, including all necessary functions and operations. Each data structure is implemented in a C++. Function Analysis: Comprehensive analyses of the functions and operations within each data structure are conducted to determine their time complexities. The time complexities of essential operations such as insertion, deletion, search, and traversal are thoroughly examined and documented. GitHub Repository: A dedicated GitHub repository is created to host the project code, documentation, and related resources. The repository contains organized directories for each data structure, including source code files, README.md files, and any additional documentation. Van Emde Boas Tree
Introduction – “A van Emde Boas tree, also known as a vEB tree or van Emde Boas priority queue, is a tree data structure which implements an associative array with m-bit integer keys.” – Wikipedia It is also called as “Van Emde Boas Priority Queue”, as it provides efficient implementations for many priority queue operations, such as finding the minimum or maximum element, inserting elements, and deleting elements. It's particularly useful when the universe of possible key values is known in advance and limited. “It was formulated by Peter Van Emde Boas in 1975.” - www.sanfoundry.com
Supported functions –
- Insert – Insert a value with an m-bit value
- Delete – Remove the value with a given value
- Search – Find the value associated with a given value
- Successor – Find the value with the smallest value which is greater than a given value
- Predecessor – Find the value with the smallest value which is smaller than a given value
- Minimum – Return the smallest element stored in the tree
- Maximum – Return the largest element stored in the tree
Algorithms –
Algorithm 1: Inserting a value.
Input: v1 – the van Emde Boas tree to insert to value – the value to insert
Output:
The value successfully inserted to the vEB tree.
Algorithm:
1. if v1 is empty:
Create a new v1 with appropriate parameters.
-
if value already exists in v1: Update the value associated with the value. Exit the algorithm.
-
if v1 is a base case: Update the value associated with the value. Exit the algorithm.
-
cluster, offset = SplitValue(value)
-
if cluster not in v1.cluster_summary: Insert(cluster, value) into v1.cluster_summary recursively.
-
Insert(offset, value) into v1.cluster[cluster] recursively.
-
if v1.min_value is None or value < v1.min_value: Update v1.min_value to value.
-
if v1.max_value is None or value > v1.max_value: Update v1.max_value to value.
Time Complexity: O(log log N)
Algorithm 2: Deleting a value
Input:
v1 – the van Emde Boas tree to insert to
value – the value to delete
Output:
The value completely removed from the vEB tree
Algorithm:
1. if v1 is empty:
Exit the algorithm.
-
if value does not exist in v1: Exit the algorithm.
-
if v1 is a base case: Remove the value and its associated value from the base case. Exit the algorithm.
-
cluster, offset = SplitValue(value)
-
if cluster is not in v1.cluster_summary: Exit the algorithm (value not found).
-
Delete(offset, v1.cluster[cluster]) recursively.
-
if v1.cluster[cluster] is empty: Delete(cluster, v1.cluster_summary) recursively.
-
if value is equal to v1.min_value: Find the successor of value in v1. Update v1.min_value to the successor.
-
if value is equal to v1.max_value: Find the predecessor of value in v1. Update v1.max_value to the predecessor
Time Complexity: O(log log N)
Algorithm 3: Finding the value associated with the given value
Input:
v1 – the van Emde Boas tree to insert to value – the value to look for
Output:
return true, if the value was found
return false, if the value was not found
Algorithm:
- if v1 is empty: Return false.
- if value is equal to v1.min_value or value is equal to v1.max_value: Return true.
- if v1 is a base case: If value exists in the base case, return true, otherwise return false.
- cluster, offset = SplitValue(value)
- if cluster is not in v1.cluster_summary: Return false.
- Lookup offset in v1.cluster[cluster] recursively.
- Return the result of the lookup operation.
Time complexity: O(log log N)
Algorithm 4: Finding the next value.
Input:
v1 - the van Emde Boas tree to search value - the value for which to find the next value
Output:
return true, if the next value was found return false, if the next value was not found
Algorithm:
- if v1 is empty: Return false.
- if value is equal to v1.max_value: Return false.
- if v1 is a base case: If the base case contains a value larger than 'value', return true, otherwise return false.
- cluster, offset = SplitValue(value)
- if cluster is not in v1.cluster_summary or offset < v1.cluster[cluster].max_value: Find the successor of value in v1.cluster[cluster]. Return true.
- Find the successor of cluster in v1.cluster_summary.
- If no successor exists, return false.
- Let next_cluster be the successor.
- Let min_value_in_next_cluster be the minimum value in v1.cluster[next_cluster].
- Return true.
Time Complexity: O(log log N)
Algorithm 5: Finding the previous value.
Input: v1 - the van Emde Boas tree to search value - the value for which to find the next value
Output:
return true, if the previous value was found return false, if the previous value was not found
Algorithm:
- if v1 is empty: Return false.
- if value is equal to v1.min_value: Return false.
- if v1 is a base case: If the base case contains a value smaller than 'value', return true, otherwise return false.
- cluster, offset = SplitValue(value)
- if cluster is not in v1.cluster_summary or offset > v1.cluster[cluster].max_value: Find the predecessor of value in v1.cluster[cluster]. Return true.
- Find the predecessor of cluster in v1.cluster_summary.
- If no predecessor exists, return false.
- Let prev_cluster be the predecessor.
- Let max_value_in_prev_cluster be the maximum value in v1.cluster[prev_cluster].
- Return true.
Time Complexity: O(log log N)
Algorithm 6: Returning the smallest element stored in the tree
Input:
v1 - the van Emde Boas tree to search
Output:
return 0 if minimum element was not found
return the minimum element itself if found
Algorithm:
1. if v1 is empty:
Return 0
- if v1 is a base case: Return the minimum value in the base case.
- Let min_cluster be the minimum value in v1.cluster_summary.
- Let min_offset_in_min_cluster be the minimum value in v1.cluster[min_cluster].
- Return CombineValues(min_cluster, min_offset_in_min_cluster).
Time Complexity: O(1) Algorithm 7: Returning the largest element stored in the tree
Input:
v1 - the van Emde Boas tree to search
Output:
return 0 if maximum element was not found
return the maximum element itself if found
Algorithm:
1. if v1 is empty:
Return 0
- if v1 is a base case: Return the maximum value in the base case.
- Let max_cluster be the maximum value in v1.cluster_summary.
- Let max_offset_in_max_cluster be the maximum value in v1.cluster[max_cluster].
- Return CombineValues(max_cluster, max_offset_in_max_cluster).
Time Complexity: O(1) Advantages of Van Emde Boas Tree – Some of the advantages of Van Emde Boas Tree are:
-
Its highly efficient operations: - vEB trees excel in performing crucial operations like finding minimum, maximum, predecessor, successor, inserting, and deleting elements. They achieve a time complexity of O(log log N) for these operations, which is significantly faster than standard binary search trees (O(log N)).
-
Ordered Retrieval: - vEB trees maintain order within the stored elements. This allows for efficient retrieval of elements based on their position.
-
Constant Time Minimum and Maximum Operations: vEB trees explicitly store the minimum and maximum values within the data structure. This enables finding the minimum and maximum elements in constant time (O(1)), regardless of the tree size.
Limitations – While offering high speeds, there are some limitations involved, viz.
-
Space Usage: - Space usage of a vEB tree is given as follows: S(2) = Θ(1) S(U) = (U1/2 + 1)S(U1/2) + Θ(U1/2) The space complexity of the van Emde Boas (vEB) tree is Θ(U), where U represents the universe of possible values. Additional: In order to reduce the space complexity to Θ(N), one can use cuckoo hashing on every operation besides insert. However, this is an expensive solution and requires a bunch of hash tables.
-
Limited Data Type: - vEB trees are designed specifically for storing and manipulating integers. They cannot handle other data types like strings or custom objects unless you implement additional conversion mechanisms.
Applications of Van Emde Boas Tree – Some of the applications include,
- Priority Queues – provides efficient implementations for many priority queue operations.
- Cache-oblivious data structures - vEB layout optimizes cache utilization and improves performance for cache-oblivious data structures.
- Computational geometry – vEB trees can help find objects within a specific rectangular query region due to its high-speed algorithms. Treap
Introduction – “A treap is a search tree data structure based on binary search trees (BST) and heaps.” – hawaii.edu In a treap, each node has a key (which follows the binary search tree property) and a priority (which follows the heap property). The binary search tree property ensures that the keys are stored in a sorted order, while the heap property ensures that the priorities maintain a certain order, typically following a heap structure such as a max-heap or a min-heap.
Supported functions –
- Insert – Insert a node into the treap data structure
- Delete – Delete a node from the treap data structure
- Inorder – Traverse through the treap by in order traversal
- Preorder – Traverse through the treap by pre order traversal
- Postorder – Traverse through the treap by post order traversal
- Split – Splits the treap into two separate treaps: one containing all nodes with keys less than or equal to the given key, and the other containing all nodes with keys greater than the given key.
- Merge – Merges the current treap with another treap (treap2).
- RotationLeft – Rearrange the tree by moving the target node downwards and making the left child node the parent.
- RotationRight – Rearrange the tree by moving the target node downwards and making the right child node the parent. Algorithms –
Algorithm 1: Inserting an element into the treap
Input: Key and priority of the node to be inserted.
Output: The updated treap after insertion
Algorithm:
- Create a new node with the given key and priority.
- If the treap is empty, set the new node as the root of the treap and return.
- Let (x, y) be a pointer to the root of the treap, and let p be a pointer to the parent of x (initialize p as null).
- While x is not null, do the following: Set p to x. If the key to be inserted is less than x.key, set x to x.left. If the key to be inserted is greater than x.key, set x to x.right. If the key to be inserted is equal to x.key, handle as needed (e.g., update priority) and return.
- Set the new node's parent pointer to p.
- If the key to be inserted is less than p.key, set p.left to the new node; otherwise, set p.right to the new node.
- While the priority of the new node is greater than the priority of its parent (p): If the new node is the root of the treap, set the new node as the new root and return. If the new node is a left child of its parent (p): Perform a right rotation on p. If the new node is a right child of its parent (p): Perform a left rotation on p.
- End Time Complexity: O(log N)
Algorithm 2: Deleting an element from the treap
Input: Key of the node to be deleted
Output: The updated treap after deletion
Algorithm:
- Let (x, y) be a pointer to the root of the treap, p be a pointer to the parent of x, and found be a boolean flag indicating if the node to be deleted is found (initialize p as null and found as false).
- While x is not null: Set p to x. If the key to be deleted is less than x.key, set x to x.left. If the key to be deleted is greater than x.key, set x to x.right. If the key to be deleted is equal to x.key, set found to true and break.
- If the node to be deleted is not found, return the treap unchanged.
- While x is not a leaf node: If x has both a left and right child: If the priority of x.left is greater than the priority of x.right, perform a right rotation on x. Otherwise, perform a left rotation on x. Otherwise, if x has only a left child, perform a right rotation on x. Otherwise, if x has only a right child, perform a left rotation on x.
- Remove the leaf node x from the treap.
- If x is the root of the treap, set the root to null and return.
- Otherwise, if x is the left child of p, set p.left to null; otherwise, set p.right to null.
- End. Time Complexity – O(log N)
Algorithm 3: Traversing the treap by in order
Input: root of the treap.
Output: A list of keys obtained from the inorder traversal.
Algorithm:
- Initialize an empty list to store the keys obtained from the inorder traversal.
- Define a function InorderTraversal(node): If node is null, return. Recursively call InorderTraversal(node.left) to traverse the left subtree. Append the key of node to the list. Recursively call InorderTraversal(node.right) to traverse the right subtree.
- Call InorderTraversal(root) with the root of the treap.
- Return the list obtained from the inorder traversal.
Time Complexity: O(N)
Algorithm 4: Traversing the treap by preorder
Input: root of the treap
Output: A list of keys obtained from the preorder traversal
Algorithm:
- Initialize an empty list to store the keys obtained from the preorder traversal.
- Define a function PreorderTraversal(node): If node is null, return. Append the key of node to the list. Recursively call PreorderTraversal(node.left) to traverse the left subtree. Recursively call PreorderTraversal(node.right) to traverse the right subtree.
- Call PreorderTraversal(root) with the root of the treap.
- Return the list obtained from the preorder traversal.
Time Complexity: O(N)
Algorithm 5: Traversing the treap by postorder
Input: root of the treap
Output: A list of keys obtained from the postorder traversal
Algorithm:
- Initialize an empty list to store the keys obtained from the postorder traversal.
- Define a function PostorderTraversal(node): If node is null, return. Recursively call PostorderTraversal(node.left) to traverse the left subtree. Recursively call PostorderTraversal(node.right) to traverse the right subtree. Append the key of node to the list.
- Call PostorderTraversal(root) with the root of the treap.
- Return the list obtained from the postorder traversal.
Time Complexity: O(N)
Algorithm 6: Splitting the treap into two treaps
Input: root of the original treap and a key for splitting.
Output: roots of two separate treaps: one containing all nodes with keys less than or equal to the given key, and the other containing all nodes with keys greater than the given key.
Algorithm:
- If root is null, return two null pointers (no nodes to split).
- If the key of root is less than or equal to the given key: Recursively call SplitTreap(root.right, key) to split the right subtree of root. Set root.right to the second output of the recursive call. Return root and the first output of the recursive call. c. If the key of root is greater than the given key: Recursively call SplitTreap(root.left, key) to split the left subtree of root. Set root.left to the first output of the recursive call. Return the second output of the recursive call and root. Time Complexity: O(log N)
Algorithm 7: Merging two treaps with one another
Input: roots of two separate treaps.
Output: root of the merged treap.
Algorithm:
-
If either root1 or root2 is null, return the other root (no merging needed).
-
If the priority of root1 is greater than the priority of root2: Set root1.right to the result of MergeTreaps(root1.right, root2). Return root1.
-
Otherwise (the priority of root2 is greater than or equal to the priority of root1): Set root2.left to the result of MergeTreaps(root1, root2.left). Return root2.
Time Complexity: O(log N)
Algorithm 8: Rotating the treap to the left
Input: node to be rotated.
Output: new root of the subtree after rotation.
Algorithm:
- If node is null or node.right is null (no right child), return node.
- Let newRoot be node.right.
- Set node.right to newRoot.left.
- Set newRoot.left to node.
- Return newRoot.
Time Complexity: O(1)
Algorithm 9: Rotating the treap to the right
Input: Node to be rotated
Output: new root of the subtree after rotation
Algorithm:
- If node is null or node.left is null (no left child), return node.
- Let newRoot be node.left.
- Set node.left to newRoot.right.
- Set newRoot.right to node.
- Return newRoot.
Time Complexity: O(1) Advantages of Treap –
- Provides a hierarchical way of storing data
- Reflects structural relationship in a data set
- Allows insertion, deletion and searching operations that yield results faster than an array or linked list
- Provides a flexible way to hold and move data
- Allows storage of many nodes
Limitations of Treap –
- All these operations take worst case time O(H), where H is the height of the treap.
- However, treaps (like BST’s) can become very unbalanced, so that H = O(N), and that’s bad.
Applications of Treap – Some of the applications include,
- Databases and Indexing: Treaps can be used in databases and indexing systems to efficiently store and retrieve data based on keys or priorities.
- Wireless Communication Networks: Treaps can be applied in wireless communication networks for scheduling and prioritizing data transmission. Nodes in the network can be represented as treap nodes, where the priorities represent the importance or urgency of transmitting data packets.
- Operating Systems: Treaps can be utilized in operating systems for various purposes, such as process scheduling and memory management. In process scheduling, treaps can help prioritize processes based on their importance or resource requirements.
- Online Gaming: Treaps can be applied in online gaming systems for managing player rankings and leaderboards. Each player's ranking can be represented as a node in a treap, where the priorities represent the player's score or ranking.
Conclusion: The exploration of the Treap and Van Emde Boas tree data structures has provided valuable insights into advanced techniques for efficient data organization and manipulation. The Treap, with its ingenious combination of binary search tree properties and heap priorities, offers a versatile solution for priority-based operations and balanced storage. Its randomized structure ensures optimal performance in various applications, including priority queues, databases, and optimization algorithms. The Van Emde Boas tree, with its hierarchical structure and specialized operations, provides an elegant solution for managing large dynamic sets with integer keys. Its efficient support for predecessor and successor queries, along with its space-efficient representation, makes it suitable for applications requiring fast search and retrieval operations. Bibliography –
Van Emde Boas Tree:
Sites referred – TISparta, José Leonidas García Gonzales. “Van-Emde-Boas-Tree/Src/Vebtree.Cpp at Master · Tisparta/Van-Emde-Boas-Tree.” GitHub, 2019, github.com/TISparta/Van-Emde-Boas-tree/blob/master/src/vEBTree.cpp. Panchal, Aakash. “Van Emde Boas Tree: Set 1: Basics and Construction.” GeeksforGeeks, GeeksforGeeks, 4 Dec. 2023, www.geeksforgeeks.org/van-emde-boas-tree-set-1-basics-and-construction/. Tal, Mika. “Van Emde Boas Trees.” Medium, Medium, 5 Sept. 2022, medium.com/@mikatal/van-emde-boas-trees-3e3c228cebc7. “Van Emde Boas Tree.” Wikipedia, Wikimedia Foundation, 24 Feb. 2024, en.wikipedia.org/wiki/Van_Emde_Boas_tree. Van Emde Boas Trees, web.stanford.edu/class/archive/cs/cs166/cs166.1166/lectures/14/Slides14.pdf. Accessed 28 Apr. 2024. “Van Emde Boas Tree Questions and Answers.” Sanfoundry, 1 Feb. 2022, www.sanfoundry.com/van-emde-boas-tree-multiple-choice-questions-answers-mcqs/#:~:text=d)%20Non%20–%20Binary-,Explanation%3A%20The%20Van%20Emde%20Boas%20Tree%20data%20structure%20is%20also,non%20–%20binary%20type%20of%20tree.
Blogs referred – Templatetypedef. “Applications of van Emde Boas Trees?” Stackoverflow, 4 Feb. 2012, https://stackoverflow.com/questions/8545851/applications-of-van-emde-boas-trees. Accessed 28 Apr. 2024. Ménétrier, Jérémy. “How Is a van Emde Boas Tree Used in Practice?” Quora, 2011, https://www.quora.com/How-is-a-van-Emde-Boas-tree-used-in-practice. Accessed 28 Apr. 2024. Video referred – MIT OpenCourseWare. “4. Divide & Conquer: Van Emde Boas Trees.” YouTube, YouTube, 4 Mar. 2016, www.youtube.com/watch?v=hmReJCupbNU&t=1s.
AI prompts used – “What is the van emde boas tree?” prompt. ChatGPT 3.5, OpenAI, 28 April 2024, https://chat.openai.com/chat
“Name me a few advantages of van emde boas tree” prompt. Gemini, Google, 28 April 2024, https://gemini.google.com/app/chat
“Give me some real time applications of vEB tree” prompt. Gemini, Google, 28 April 2024, https://gemini.google.com/app/chat
Treap:
Sites referred – “Lecture 7: Randomized Search Trees 1 Treaps.” Hawaii.Edu, www2.hawaii.edu/~nodari/teaching/f19/scribes/notes07.pdf. Accessed 28 Apr. 2024. Singh, Ramandeep. “Treap.” Scaler Topics, Scaler Topics, 13 Apr. 2022, www.scaler.com/topics/data-structures/treap/. “Treap (a Randomized Binary Search Tree).” GeeksforGeeks, GeeksforGeeks, 15 Dec. 2022, www.geeksforgeeks.org/treap-a-randomized-binary-search-tree/. Indeed Editorial Team. “What Is the Tree Data Structure? (With Advantages and Types) | Indeed.Com India.” Indeed, 3 Feb. 2023, in.indeed.com/career-advice/finding-a-job/what-is-tree-data-structure. Disadvantages of Treaps, seweb.ucsd.edu/~kube/cls/100/Lectures/lec5/lec5-20.html. Accessed 28 Apr. 2024. Video referred – Professor Painter. “Binary Search Trees 9 - Left and Right Rotations.” YouTube, YouTube, 4 Feb. 2021, www.youtube.com/watch?v=dCF_d-zc_bQ&t=253s.
AI prompts used – “What is a treap” prompt. ChatGPT 3.5, OpenAI, 28 April 2024, https://chat.openai.com/chat
“Give me some real life applications of treaps” prompt. ChatGPT 3.5, OpenAI, 28 April 2024, https://chat.openai.com/chat