HEAPS CS 6310 Advanced Data Structure WeiShian Wang

  • Slides: 18
Download presentation
HEAPS CS 6310 Advanced Data Structure Wei-Shian Wang

HEAPS CS 6310 Advanced Data Structure Wei-Shian Wang

� Also called priority queues � Support operations: insert, find_min, delete_min � Min-heap and

� Also called priority queues � Support operations: insert, find_min, delete_min � Min-heap and max-heap

5. 1 Balanced Search Trees as Heaps

5. 1 Balanced Search Trees as Heaps

� insert : O(log n) � find_min : O(log n) O(1) � delete_min :

� insert : O(log n) � find_min : O(log n) O(1) � delete_min : O(log n)

Make find_min in O(1) � Store � Look the current minimum in a variable

Make find_min in O(1) � Store � Look the current minimum in a variable up the new current minimum in O(log n) time when we perform the next delete_min

� delete_min – O(1) 1. Delete the object current_min->object 2. Move current_min to the

� delete_min – O(1) 1. Delete the object current_min->object 2. Move current_min to the next list position 3. If current_min->key is now larger than the key in the root of the balanced tree, add the left subtree of the balanced search tree to the invalid nodes structure; take the right subtree as new search tree; and return the node of the old root to the free list 4. Return several nodes from the invalid nodes structure to the free list

� insert – O(log n) 1. Split the search tree at current_min->key, and add

� insert – O(log n) 1. Split the search tree at current_min->key, and add the lower tree to the invalid nodes structure 2. Insert the new key in search tree 3. If the key is below current_min->key, set current_min to the new key and object � find_min – O(1) • Return current_min->key and current_min ->object

� Theorem. The heap structure can be realized using a balanced search tree with

� Theorem. The heap structure can be realized using a balanced search tree with lazy deletion in time O(log n) for insert and O(1) for find_min and delete_min operations if the heap contains n element.

5. 4 Leftist Heaps

5. 4 Leftist Heaps

Leftist Heap Properties � Each node contains an additional field, the rank, which is

Leftist Heap Properties � Each node contains an additional field, the rank, which is defined by Ø n->rank = 1 if n->left = NULL or n->right = NULL Ø n->rank = 1 + Min(n->left->rank, n->right->rank) if n->left ≠ NULL and n->right ≠ NULL � Heap is empty if root->rank = 0

� Each node in a leftist heap has the shortest path on the left

� Each node in a leftist heap has the shortest path on the left side is at least as long as that on the right side: Ø n->left->rank ≧ n->right->rank � If they are not both defined, then if one of them exists, it is the left one: Ø n->left = NULL only if n->right = NULL � Most � All nodes are on the left the merging work is done on the right

� insert Ø O(log n)

� insert Ø O(log n)

� delete_min Ø O(log n) and merge

� delete_min Ø O(log n) and merge

� Theorem. The leftist heap structure supports the operation find_min in O(1) time and

� Theorem. The leftist heap structure supports the operation find_min in O(1) time and insert, merge, and delete_min in O(log n) time

� Leftist Heap Visualization Ø https: //www. cs. usfca. edu/~galles/visualizatio n/Leftist. Heap. html

� Leftist Heap Visualization Ø https: //www. cs. usfca. edu/~galles/visualizatio n/Leftist. Heap. html

Thank you !

Thank you !