Leftist Heaps Text Leftist Heap Building a Leftist Heap

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.
Chapter 6: Priority Queues (Heaps) Priority Queue ADT Heap Implementation CS 340 Page 100 Heap Applications Leftist Heaps.
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.
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
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.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Computer Science CS 330: Algorithms Priority Queues Gene Itkis.
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.
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
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]
Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement.
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
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity.  insert  remove min (or max)  initialize Can.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Binomial Queues Text Read Weiss, §6.8 Binomial Queue Definition of binomial queue Definition of binary addition Building a Binomial Queue Sequence of inserts.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Chapter 21 Binary Heap.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
Lecture 8COMPSCI.220.FS.T Algorithm HeapSort J. W. J. Williams (1964): a special binary tree called heap to obtain an O(n log n) worst-case sorting.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Foundations of Data Structures Practical Session #8 Heaps.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
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.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
Leftist Trees Linked binary tree.
AA Trees.
Red Black Trees
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Hashing Exercises.
Source: Muangsin / Weiss
CSCE 3100 Data Structures and Algorithm Analysis
CMSC 341 Lecture 13 Leftist Heaps
Binary Heaps Text Binary Heap Building a Binary Heap
Priority Queues.
Binary Tree Application Operations in Heaps
Priority Queues.
Tree Representation Heap.
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
CMSC 341 Lecture 19.
CS 6310 Advanced Data Structure Wei-Shian Wang
Red Black Trees.
Heaps & Multi-way Search Trees
A Heap Is Efficiently Represented As An Array
Priority Queues (Heaps)
Presentation transcript:

Leftist Heaps Text Leftist Heap Building a Leftist Heap Read Weiss, §6.6 Leftist Heap Definition of null path length Definition of leftist heap Building a Leftist Heap Sequence of inserts Re-heapification if leftist heap property is violated

Motivation A binary heap provides O(log n) inserts and O(log n) deletes but suffers from O(n log n) merges A leftist heap offers O(log n) inserts and O(log n) deletes and O(log n) merges Note, however, leftist heap inserts and deletes are more expensive than Binary Heap inserts and deletes

Definition A Leftist (min)Heap is a binary tree that satisfies the follow-ing conditions. If X is a node and L and R are its left and right children, then: X.value ≤ L.value X.value ≤ R.value null path length of L ≥ null path length of R where the null path length of a node is the shortest distance between from that node to a descendant with 0 or 1 child. If a node is null, its null path length is −1.

Example: Null Path Length 4 8 19 12 27 20 15 25 43 node 4 8 19 12 15 25 27 20 43 npl 1 0 1 1 0 0 0 0 0

Example: Null Path Length 4 8 19 1 What is the npl of the right child of 8? 12 1 27 20 What are the npl’s of the children of 20 and the right child of 27? 15 25 43 node 4 8 19 12 15 25 27 20 43 npl 1 0 1 1 0 0 0 0 0

Example: Null Path Length 4 node 4 violates leftist heap property  fix by swapping children 8 19 1 12 1 27 20 15 25 43 node 4 8 19 12 15 25 27 20 43 npl 1 0 1 1 0 0 0 0 0

Leftist Heap 4 19 20 27 43 1 25 12 15 8 1 node 4 8 19 12 15 25 27 20 43 npl 1 0 1 1 0 0 0 0 0

Merging Leftist Heaps Consider two leftist heaps … 4 6 19 8 8 7 27 20 12 14 15 25 43 Task: merge them into a single leftist heap

Merging Leftist Heaps First, instantiate a Stack 4 6 19 8 8 7 27 20 12 14 15 25 43 First, instantiate a Stack

Merging Leftist Heaps Compare root nodes x y merge(x,y) 4 6 19 8 8 7 27 20 12 14 15 25 43 Compare root nodes merge(x,y)

Merging Leftist Heaps Remember smaller value x y 4 4 6 19 8 8 7 27 20 12 14 15 25 43 Remember smaller value

Merging Leftist Heaps y 4 4 x 6 19 8 8 7 27 20 12 14 15 25 43 Repeat the process with the right child of the smaller value

Merging Leftist Heaps Remember smaller value y x 6 4 4 6 19 8 8 7 27 20 12 14 15 25 43 Remember smaller value

Merging Leftist Heaps 6 4 4 x 6 y 19 8 8 7 27 20 12 14 15 25 43 Repeat the process with the right child of the smaller value

Merging Leftist Heaps Remember smaller value x y 7 6 4 4 6 19 8 8 7 27 20 12 14 15 25 43 Remember smaller value

Merging Leftist Heaps 7 6 4 4 x 6 19 8 8 7 y 27 20 12 14 null 15 25 43 Repeat the process with the right child of the smaller value

Merging Leftist Heaps 7 6 4 4 x 6 19 8 8 7 27 20 12 14 15 25 43 8 Because one of the arguments is null, return the other argument

Merging Leftist Heaps Make 8 the right child of 7 x Refers to node 8 7 6 4 4 6 x 19 Refers to node 8 8 7 27 20 14 8 12 43 8 15 25 Make 8 the right child of 7

Merging Leftist Heaps Make 7 leftist (by swapping children) 6 4 4 6 19 Refers to node 8 8 7 8 27 20 14 12 43 8 15 25 Make 7 leftist (by swapping children)

Merging Leftist Heaps Return node 7 Refers to node 8 6 4 7 4 6 19 8 7 27 20 14 12 43 15 25 7 Return node 7

Make 7 the right child of 6 (which it already is) Merging Leftist Heaps 6 4 4 6 19 Refers to node 8 8 7 8 27 20 14 12 43 15 25 7 Make 7 the right child of 6 (which it already is)

Merging Leftist Heaps Make 6 leftist (it already is) Refers to node 8 4 4 6 19 Refers to node 8 8 7 8 27 20 14 12 43 15 25 7 Make 6 leftist (it already is)

Merging Leftist Heaps Return node 6 Refers to node 8 4 6 4 6 19 8 7 8 27 20 14 12 43 15 25 6 Return node 6

Merging Leftist Heaps Make 6 the right child of 4 4 6 4 19 6 27 20 8 7 14 8 43 12 15 25 6 Make 6 the right child of 4

Merging Leftist Heaps Make 4 leftist (it already is) 4 6 4 19 6 27 20 8 7 14 8 43 12 15 25 6 Make 4 leftist (it already is)

Final Leftist Heap Verify that the tree is heap 4 19 6 27 20 8 7 14 8 43 12 15 25 Verify that the tree is heap Verify that the heap is leftist 4 Return node 4

Analysis Height of a leftist heap ≈ O(log n) Maximum number of values stored in Stack ≈ 2 * O(log n) ≈ O(log n) Total cost of merge ≈ O(log n)

Inserts and Deletes To insert a node into a leftist heap, merge the leftist heap with the node After deleting root X from a leftist heap, merge its left and right subheaps In summary, there is only one operation, a merge.

Skew Heaps Text Skew Heap Building a Skew Heap Read Weiss, §6.7 No need for null path length Definition of skew heap Building a Skew Heap Sequence of inserts Fix heap if leftist heap property is violated

Motivation Simplify leftist heap by not maintaining null path lengths swapping children at every step

Definition A Skew (min)Heap is a binary tree that satisfies the follow-ing conditions. If X is a node and L and R are its left and right children, then: X.value ≤ L.value X.value ≤ R.value A Skew (max)Heap is a binary tree that satisfies the follow-ing conditions. If X is a node and L and R are its left and right children, then: X.value ≥ L.value X.value ≥ R.value

Merging Skew Heaps Consider two skew heaps … 4 6 19 8 8 7 27 20 12 14 15 25 43 Task: merge them into a single skew heap

Merging Skew Heaps First, instantiate a Stack 4 6 19 8 8 7 27 20 12 14 15 25 43 First, instantiate a Stack

Merging Skew Heaps Compare root nodes x y merge(x,y) 4 6 19 8 8 7 27 20 12 14 15 25 43 Compare root nodes merge(x,y)

Merging Skew Heaps Remember smaller value x y 4 4 6 19 8 8 7 27 20 12 14 15 25 43 Remember smaller value

Merging Skew Heaps y 4 4 x 6 19 8 8 7 27 20 12 14 15 25 43 Repeat the process with the right child of the smaller value

Merging Skew Heaps Remember smaller value y x 6 4 4 6 19 8 8 7 27 20 12 14 15 25 43 Remember smaller value

Merging Skew Heaps 6 4 4 x 6 y 19 8 8 7 27 20 12 14 15 25 43 Repeat the process with the right child of the smaller value

Merging Skew Heaps Remember smaller value x y 7 6 4 4 6 19 8 8 7 27 20 12 14 15 25 43 Remember smaller value

Merging Skew Heaps 7 6 4 4 x 6 19 8 8 7 y 27 20 12 14 null 15 25 43 Repeat the process with the right child of the smaller value

Merging Skew Heaps 7 6 4 4 x 6 19 8 8 7 27 20 12 14 15 25 43 8 Because one of the arguments is null, return the other argument

Merging Skew Heaps Make 8 the right child of 7 x Refers to node 8 7 6 4 4 6 x 19 Refers to node 8 8 7 27 20 14 8 12 43 8 15 25 Make 8 the right child of 7

Merging Skew Heaps Swap children of node 7 Refers to node 8 7 6 4 8 4 19 Refers to node 8 8 7 8 27 20 14 12 43 8 15 25 Swap children of node 7

Merging Skew Heaps Return node 7 Refers to node 8 6 4 7 4 6 19 8 7 8 27 20 14 12 43 15 25 7 Return node 7

Make 7 the right child of 6 (which it already is) Merging Skew Heaps 6 4 4 6 19 Refers to node 8 8 7 8 27 20 14 12 43 15 25 7 Make 7 the right child of 6 (which it already is)

Merging Skew Heaps Swap children of node 6 Refers to node 8 6 4 7 4 6 19 Refers to node 8 7 8 8 27 20 14 12 43 15 25 7 Swap children of node 6

Merging Skew Heaps Return node 6 Refers to node 8 4 6 4 6 19 7 8 8 27 20 14 12 43 15 25 6 Return node 6

Merging Skew Heaps Make 6 the right child of 4 4 6 4 19 6 7 8 27 20 8 14 43 12 15 25 6 Make 6 the right child of 4

Merging Skew Heaps Swap children of node 4 4 6 4 6 19 7 8 27 20 8 14 43 12 15 25 6 Swap children of node 4

Final Skew Heap Verify that the tree is heap 4 6 19 7 8 27 20 8 14 43 12 15 25 Verify that the tree is heap Verify that the heap is skew 4 Return node 4

Analysis Height of a leftist heap ≈ O(log n) Maximum number of values stored in Stack ≈ 2 * O(log n) ≈ O(log n) Total cost of merge ≈ O(log n)

Inserts and Deletes To insert a node into a leftist heap, merge the leftist heap with the node After deleting root X from a leftist heap, merge its left and right subheaps In summary, there is only one operation, a merge.