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

Slides:



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

§2 Binary Trees Note: In a tree, the order of children does not matter. But in a binary tree, left child and right child are different. A B A B andare.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
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.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
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.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
CISC220 Fall 2009 James Atlas Nov 13: Heap Implementations, Graphs.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
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.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
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.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
§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.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
§8 Binomial Queues Haven’t we had enough about queues? What is a binomial queue for? Well, what is the average time for insertions with leftist or skew.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
data ordered along paths from root to leaf
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues (Heaps)
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Review for Exam 2 Topics covered (since exam 1): –Splay Tree –K-D Trees –RB Tree –Priority Queue and Binary Heap –B-Tree For each of these data structures.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
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:
CS 201 Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
BST Trees
Topics covered (since exam 1):
Hashing Exercises.
CMSC 341 Lecture 13 Leftist Heaps
Topics covered (since exam 1):
Based on slides by Harry Zhou Read Sections 12, 13, 6, 18, 16.3
CE 221 Data Structures and Algorithms
CS Data Structure: Heaps.
CSE 326: Data Structures Lecture #5 Political Heaps
Heaps A heap is a binary tree that satisfies the following properties:
Heaps Priority Queues.
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
CMSC 341 Lecture 19.
Leftist Heaps Text Leftist Heap Building a Leftist Heap
CS 6310 Advanced Data Structure Wei-Shian Wang
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Presentation transcript:

§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property – the same Structure Property – binary tree, but unbalanced 1/10 Discussion 11: How fast can we merge two heaps if we simply use the original heap structure?

§6 Leftist Heaps 【 Definition 】 The null path length, Npl(X), of any node X is the length of the shortest path from X to a node without two children. Define Npl(NULL) = –1. Note: Npl(X) = min { Npl(C) + 1 for all C as children of X } Note: Npl(X) = min { Npl(C) + 1 for all C as children of X } 【 Definition 】 The leftist heap property is that for every node X in the heap, the null path length of the left child is at least as large as that of the right child  The tree is biased to get deep toward the left. 2/10

§6 Leftist Heaps 【 Theorem 】 A leftist tree with r nodes on the right path must have at least 2 r – 1 nodes. Proof: By induction on p Trouble makers: Insert and Merge Note: Insertion is merely a special case of merging. 3/10 Discussion 12: How long is the right path of a leftist tree of N nodes? What does this conclusion mean to us?

§6 Leftist Heaps struct TreeNode { ElementType Element; PriorityQueue Left; PriorityQueue Right; int Npl; } ;  Declaration:  Merge (recursive version): H1H1 H2H2 Step 1 : Merge( H 1 ->Right, H 2 ) Step 2 : Attach( H 2, H 1 ->Right ) Step 3 : Swap(H 1 ->Right, H 1 ->Left ) if necessary 4/10

§6 Leftist Heaps PriorityQueue Merge ( PriorityQueue H1, PriorityQueue H2 ) { if ( H1 == NULL ) return H2; if ( H2 == NULL ) return H1; if ( H1->Element Element ) return Merge1( H1, H2 ); else return Merge1( H2, H1 ); } static PriorityQueue Merge1( PriorityQueue H1, PriorityQueue H2 ) { if ( H1->Left == NULL ) /* single node */ H1->Left = H2;/* H1->Right is already NULL and H1->Npl is already 0 */ else { H1->Right = Merge( H1->Right, H2 ); /* Step 1 & 2 */ if ( H1->Left->Npl Right->Npl ) SwapChildren( H1 );/* Step 3 */ H1->Npl = H1->Right->Npl + 1; } /* end else */ return H1; } What if Npl is NOT updated? T p = O(log N) 5/10

§6 Leftist Heaps  Merge (iterative version): H1H1 H2H2 Step 1 : Sort the right paths without changing their left children Step 2 : Swap children if necessary  DeleteMin: Step 1 : Delete the root Step 2 : Merge the two subtrees T p = O(log N) Home work: p Merge two given leftist heaps p BuildHeap in O(N) 6/10

§7 Skew Heaps -- a simple version of the leftist heaps Target : Any M consecutive operations take at most O(M log N) time.  Merge: Always swap the left and right children except that the largest of all the nodes on the right paths does not have its children swapped. No Npl. Not really a special case, but a natural stop in the recursions H1H1 H2H This is NOT always the case. 7/10

§7 Skew Heaps 【 Example 】 Insert  Merge (iterative version): H1H1 H2H /10

§7 Skew Heaps Note:  Skew heaps have the advantage that no extra space is required to maintain path lengths and no tests are required to determine when to swap children.  It is an open problem to determine precisely the expected right path length of both leftist and skew heaps. Note:  Skew heaps have the advantage that no extra space is required to maintain path lengths and no tests are required to determine when to swap children.  It is an open problem to determine precisely the expected right path length of both leftist and skew heaps. Home work: p Insertions 9/10

Research Project 6 Heap Merging (25) This project requires you to implement Merge operation on ordinary heaps, leftist heaps, and skew heaps. You are to analyze and compare the performances of a sequence of merges on these heap structures. Detailed requirements can be downloaded from 10/10