DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.

Slides:



Advertisements
Similar presentations
 Have to copy one array into another (N)
Advertisements

§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
COL 106 Shweta Agrawal and Amit Kumar
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
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.
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Leftist Heaps Text Read Weiss, §23.1 (Skew Heaps) Leftist Heap Definition of null path length Definition of leftist heap Building a Leftist Heap Sequence.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
Heaps, Heap Sort, and Priority Queues
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Tyler Robison Summer
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
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.
CSE 373 Data Structures Lecture 12
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Heap: A Special Kind of Tree
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
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.
§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
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.
Cpt S 223 – Advanced Data Structures Priority Queues
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues (Heaps)
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Dan Grossman Spring 2012.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
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.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
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.
1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
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:
CSE 326: Data Structures Priority Queues (Heaps)
Priority Queue A Priority Queue Set S is made up of n elements: x0 x1 x2 x3 … xn-1 Functions: createEmptySet() returns a newly created empty priority.
CS 201 Data Structures and Algorithms
Binary Heaps Priority Queues
Source: Muangsin / Weiss
CMSC 341 Lecture 13 Leftist Heaps
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Heaps, Heap Sort, and Priority Queues
Trees 3: The Binary Search Tree
Binary Heaps Priority Queues
BuildHeap The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a.
Binary Heaps Priority Queues
CMSC 341 Binary Search Trees.
CE 221 Data Structures and Algorithms
Heaps Priority Queues.
CMSC 341 Binary Search Trees.
CMSC 341 Lecture 19.
Priority Queues (Heaps)
Leftist Heaps Text Leftist Heap Building a Leftist Heap
CS 6310 Advanced Data Structure Wei-Shian Wang
Presentation transcript:

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI

2 ROAD MAP PRIORITY QUEUES (HEAPS) Model Simple Implementations Binary Heap Applications of Priority Queues d-Heaps Leftist Heaps

3 Review insert worst case  O(logN) average case  O(1) deleteMin worst case  O(logN) average case  O(logN) buildHeap worst case  O(N) average case  O(N) decreaseKey worst case  O(logN) average case  O(1) increaseKey worst case  O(logN) average case  O(logN) remove worst case  O(logN) average case  O(logN) Running Times of Operations for Binary Heaps

4 d-Heaps Nodes have at most d children Shallow  Insert  one comparison for each percolate DeleteMin  d-1 comparisons Good when number of insertions are much greater than deletion

5 A 3-Heap

6 Leftist Heaps Support merge operation efficiently Merge  O(N) for binary heaps Using arrays θ(N) Efficient implementation Pointers makes others slower Leftist heap has both a structural property and an ordering property Its difference from binary heap is that, leftist heaps are not perfectly balanced

7 Leftist Heaps Definition : Null path length of a node x is the length of the shortest path from x to a node without two children.

8 Leftist Heaps Definition : For each node: NPL of left child is not less than NPL of right child Only left tree is lefist heap

9 Theorem : A leftist tree with r nodes on the right path must have at least 2 r -1 nodes Proof by induction: –if r = 1, there must be at least one tree node. –otherwise, suppose that the theorem is true for 1,2,…,r. –consider a leftist tree with r+1 noes on the right path –then the root has a right subtree with r nodes on the right path, and a left subtree with at least r nodes on the right path (otherwise it would not be leftist) –applying in the inductive hypothesis to these subtrees yields a minimum of 2 r -1 nodes in each subtree –this plus the root gives at least 2 r+1 -1 nodes in the tree, prooving the theorem. So, r = O(logN)

10 Leftist Heap Operations Fundamental operation on leftist heaps is “merging” What about insert and deleteMin Algorithm if H1 or H2 is NULL return the other one compare the root values (assume H1 is smaller) H1->right = Merge(H1->right, H2) swap the left and right subtrees if necessary if leftist heap property is violated update the NPL of the root of H1 return H1

11 Leftist Heap Operations Two leftist heaps H 1 and H 2

12 Leftist Heap Operations Result of merging H 2 and H 1 ’s right subheap

13 Leftist Heap Operations Result of attaching leftist heap of previous figure as H 1 ’s right child

14 Leftist Heap Operations Result swapping children of H 1 ’s root

15 template class 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 ) { } friend class LeftistHeap ; };

16 template class LeftistHeap { public: LeftistHeap( ); LeftistHeap( const LeftistHeap & rhs ); ~LeftistHeap( ); bool isEmpty( ) const; bool isFull( ) const; const Comparable & findMin( ) const; void insert( const Comparable & x ); void deleteMin( ); void deleteMin( Comparable & minItem ); void makeEmpty( ); void merge( LeftistHeap & rhs ); const LeftistHeap & operator=( const LeftistHeap & rhs );

17 private: LeftistNode *root; LeftistNode * merge( LeftistNode *h1, LeftistNode *h2 ) const; LeftistNode * merge1( LeftistNode *h1, LeftistNode *h2 ) const; void swapChildren( LeftistNode * t ) const; LeftistNode * clone( LeftistNode *t ) const; };

18 /*Construct the leftist heap. template LeftistHeap ::LeftistHeap( ) { root = NULL; } /* Copy constructor. template LeftistHeap ::LeftistHeap( const LeftistHeap & rhs ) { root = NULL; *this = rhs; } /*Destruct the leftist heap. template LeftistHeap ::~LeftistHeap( ) { makeEmpty( ); }

19 /*Merge rhs into the priority queue. rhs becomes empty. rhs must be different from this. template void LeftistHeap ::merge( LeftistHeap & rhs ) { if( this == &rhs ) // Avoid aliasing problems return; root = merge( root, rhs.root ); rhs.root = NULL; } /*Internal method to merge two roots. template LeftistNode * LeftistHeap ::merge( LeftistNode * h1, LeftistNode * h2 ) const { if( h1 == NULL )return h2; if( h2 == NULL )return h1; if( h1->element element ) return merge1( h1, h2 ); else return merge1( h2, h1 ); }

20 /*Internal method to merge two roots. Assumes trees are not empty, and h1's root contains smallest item. template LeftistNode * LeftistHeap :: merge1( LeftistNode * h1, LeftistNode * h2 ) const { 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 right->npl ) swapChildren( h1 ); h1->npl = h1->right->npl + 1; } return h1; }

21 /*Insert item x into the priority queue, maintaining heap order. template void LeftistHeap ::insert( const Comparable & x ) { root = merge( new LeftistNode ( x ), root ); } /* Remove the smallest item from the priority queue. template void LeftistHeap ::deleteMin( ) { if( isEmpty( ) ) throw Underflow( ); LeftistNode *oldRoot = root; root = merge( root->left, root->right ); delete oldRoot; }

22 Leftist Heap Operations Result of merging right paths of H 1 and H 2