CMSC 341 Lecture 13 Leftist Heaps

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Heap: A Special Kind of Tree
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
CS 367 Introduction to Data Structures Lecture 8.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Fibonacci Heaps. Fibonacci Binary insert O(1) O(log(n)) find O(1) N/A union O(1) N/A minimum O(1) O(1) decrease key O(1) O(log(n)) delete O(log(n) O(log(n))
CSE 326: Data Structures Priority Queues (Heaps)
BST Trees
Priority Queues and Heaps
Topics covered (since exam 1):
Week 6 - Wednesday CS221.
Binary Heaps Priority Queues
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
B+-Trees.
October 30th – Priority QUeues
Hashing Exercises.
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
ITEC 2620M Introduction to Data Structures
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
(edited by Nadia Al-Ghreimil)
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 10 Binary Search Trees
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
7/23/2009 Many thanks to David Sun for some of the included slides!
Binary Trees.
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 8 – Binary Search Tree
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Priority Queue & Heap CSCI 3110 Nan Chen.
Binary Heaps Priority Queues
Topics covered (since exam 1):
CMSC 341 Lecture 14 Priority Queues & Heaps
B- Trees D. Frey with apologies to Tom Anastasio
ITEC 2620M Introduction to Data Structures
Heaps and the Heapsort Heaps and priority queues
Binary Trees.
Binary Heaps Priority Queues
CMSC 341 Binary Search Trees.
B- Trees D. Frey with apologies to Tom Anastasio
A Robust Data Structure
Heaps Priority Queues.
B- Trees D. Frey with apologies to Tom Anastasio
CMSC 341 Binary Search Trees.
CSC 143 Binary Search Trees.
CMSC 341 Lecture 19.
Priority Queues CSE 373 Data Structures.
Topics covered (since exam 1):
B-Trees.
CS 6310 Advanced Data Structure Wei-Shian Wang
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Heaps & Multi-way Search Trees
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

CMSC 341 Lecture 13 Leftist Heaps Prof. Neary Based on slides from previous iterations of this course

Today’s Topics Review of Min Heaps Introduction of Left-ist Heaps Merge Operation Heap Operations

Review of Heaps

Min Binary Heap A min binary heap is a… Complete binary tree Neither child is smaller than the value in the parent Both children are at least as large as the parent In other words, smaller items go above larger ones

Min Binary Heap Performance (n is the number of elements in the heap) construction O( n ) findMin() O( 1 ) insert() O( lg n ) deleteMin() O( lg n )

Problem Suppose you’re using more than one heap Multiple emergency rooms, etc. What if you need to merge them? One is too small, etc. Merging regular heaps is O(m+n)

Introduction to Leftist Heaps

Leftist Heap Concepts Structurally, a leftist heap is a min tree where each node is marked with a rank value. Uses a Binary Tree (BT)!! Merging heaps is much easier and faster May use already established links to merge with a new node Rather than copying pieces of an array

Leftist Heap Concepts Values STILL obey a heap order (partial order) Uses a null path length to maintain the structure (covered in detail later) The null path of a node’s left child is >= null path of the right child At every node, the shortest path to a non-full node is along the rightmost path

Leftist Heap Example A leftist heap, then, is a purposefully unbalanced binary tree (leaning to the left, hence the name) that keeps its smallest value at the top and has an inexpensive merge operation 2 3 7 9 8 10 15 8

Leftist Heap Performance Leftist Heaps support: findMin() = O(1) deleteMin() = O(log n) insert() = O(log n) construct = O(n) merge() = O(log n)

Null Path Length (npl) Length of shortest path from current node to a node without exactly 2 children value is stored IN the node itself leafs npl = 0 nodes with only 1 child

Null Path Length (npl) Calculation To calculate the npl for each node, we look to see how many nodes we need to traverse to get to an open node 2 3 7 9 8 10 15 8

Null Path Length (npl) Calculation Leaves -> npl = 0 One child -> npl = 0 Others -> npl = 1 + min(left, right) 2 2 2 1 1 3 7 9 8 10 15 8

Leftist Node The leftist heap node should keep track of: links (left and right) element (data) npl

Looks like a binary tree node except the npl being stored. Leftist Node Code Looks like a binary tree node except the npl being stored. private: struct LeftistNode { Comparable element; LeftistNode *left; LeftistNode *right; int npl; LeftistNode( const Comparable & theElement, LeftistNode *lt = NULL, LeftistNode *rt = NULL, int np = 0 ) : element( theElement ), left( lt ), right( rt ), npl( np ) { } }; LeftistNode *root;

Building a Leftist Heap

Building a Leftist Heap Value of node STILL matters Lowest value will be root, so still a min Heap Data entered is random Uses CURRENT npl of a node to determine where the next node will be placed

Leftist Heap Algorithm Add new node to right-side of tree, in order If new node is to be inserted as a parent (parent < children) make new node parent link children to it link grandparent down to new node (now new parent) If leaf, attach to right of parent If no left sibling, push to left (hence left-ist) why?? (answer in a second) Else left node is present, leave at right child Update all ancestors’ npls Check each time that all nodes left npl > right npls if not, swap children or node where this condition exists

Merging Leftist Heaps

Merging Leftist Heaps In the code, adding a single node is treated as merging a heap (just one node) with an established heap’s root And work from that root as we just went over

Merging Leftist Heaps The heaps we are about to merge must be LEFTIST heaps At the end we will get a heap that is both a min-heap leftist

Merging Leftist Heaps The Merge procedure takes two leftist trees, A and B, and returns a leftist tree that contains the union of the elements of A and B. In a program, a leftist tree is represented by a pointer to its root.

Merging Leftist Heaps Example 2 1 1 1 22 5 7 1 75 10 15 20 25 50 99 Where should we attempt to merge?

Merging Leftist Heaps Example 22 7 75 20 25 50 99 In the right sub-tree

Merging Leftist Heaps Example 22 75 25 All the way down to the right most node

Merging Leftist Heaps Example 22 75 25 Important: We don’t “split” a heap, so 22 must be the parent in this merge As there are two nodes in the right subtree, swap.

Merging Leftist Heaps Example 22 75 25 Merge two subtrees

Merging Leftist Heaps Example 7 20 22 50 99 75 25 Next level of the tree

Merging Leftist Heaps Example 1 1 2 5 7 10 15 20 22 50 99 75 25 Right side of the tree has a npl of 2 so we need to swap

Merging Leftist Heaps Example 2 1 2 1 7 5 20 22 10 15 50 99 75 25 Now the highest npl is on the left.

Merging Leftist Heaps Compare the roots. Smaller root becomes new root of subheap “Hang up” the left child of the new root Recursively merge the right subheap of the new root with the other heap. Update the npl Verify a leftist heap! (left npl >= right npl) if not, swap troubled node with sibling

Merging Leftist Heaps Code /** * Merge rhs into the priority queue. * rhs becomes empty. rhs must be different from this. */ void merge( LeftistHeap & rhs ) { if( this == &rhs ) // Avoid aliasing problems return; root = merge( root, rhs.root ); rhs.root = NULL; }

Merging Leftist Heaps Code /** * Internal method to merge two roots. * Deals with deviant cases and calls recursive merge1. */ LeftistNode * merge( LeftistNode *h1, LeftistNode *h2 ) { if( h1 == NULL ) return h2; if( h2 == NULL ) return h1; if( h1->element < h2->element ) return merge1( h1, h2 ); else return merge1( h2, h1 ); }

Merging Leftist Heaps Code /** * Internal method to merge two roots. * Assumes trees are not empty, & h1's root contains smallest item. */ LeftistNode * merge1( LeftistNode *h1, LeftistNode *h2 ) { if( h1->left == NULL ) // Single node h1->left = h2; // Other fields in h1 already accurate else h1->right = merge( h1->right, h2 ); if( h1->left->npl < h1->right->npl ) swapChildren( h1 ); h1->npl = h1->right->npl + 1; } return h1;

Deleting from Leftist Heap

Deleting from Leftist Heap Simple to just remove a node (since at top) this will make two trees Merge the two trees like we just did

Deleting from Leftist Heap We remove the root. 2 3 1 1 8 10 14 23 21 17 26

Deleting from Leftist Heap Then we do a merge and because min is in left subtree, we recursively merge right into left 2 3 1 1 8 10 14 23 21 17 26

Deleting from Leftist Heap Then we do a merge and because min is in left subtree, we recursively merge right into left 2 3 1 1 8 10 14 23 21 17 26

Deleting from Leftist Heap 1 After Merge 8 1 10 14 1 17 26 21 23

Leftist Heaps Merge with two trees of size n O(log n), we are not creating a totally new tree!! some was used as the LEFT side! Inserting into a left-ist heap O(log n) same as before with a regular heap deleteMin with heap size n remove and return root (minimum value) merge left and right subtrees