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.

Slides:



Advertisements
Similar presentations
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)
Advertisements

Winner trees. Loser Trees.
Pairing Heaps. Experimental results suggest that pairing heaps are actually faster than Fibonacci heaps.  Simpler to implement.  Smaller runtime overheads.
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.
1 Pertemuan 19 Leftist Tree Heap Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
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.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
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.
Binomial Heaps. Min Binomial Heap Collection of min trees
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.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
Priority Queues, Heaps & Leftist Trees
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
單元7: Heap 定義 for priority queues Leftist trees Binomial heap
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
SNU IDB Lab. Ch.13 Priority Queues © copyright 2006 SNU IDB Lab.
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.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
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.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Leftist Trees Linked binary tree.
Heaps (8.3) CSE 2011 Winter May 2018.
Splay Trees Binary search trees.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
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.
Part-D1 Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Dynamic Dictionaries Primary Operations: Additional operations:
Splay Trees Binary search trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Interval Heaps Complete binary tree.
Heaps and Priority Queues
Pairing Heaps Actual Complexity.
Chapter 8 – Binary Search Tree
Initializing A Max Heap
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heaps and Priority Queues
Heaps A heap is a binary tree that satisfies the following properties:
Dr.Surasak Mungsing CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05-2: Analysis of time Complexity of Priority.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Heaps and Priority Queues
CMSC 341 Lecture 19.
Heaps By JJ Shepherd.
Leftist Heaps Text Leftist Heap Building a Leftist Heap
CS 6310 Advanced Data Structure Wei-Shian Wang
Pairing Heaps Actual Complexity
A Heap Is Efficiently Represented As An Array
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

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 meld two leftist tree priority queues in O(log n) time.

Extended Binary Trees Start with any binary tree and add an external node wherever there is an empty subtree. Result is an extended binary tree.

A Binary Tree

An Extended Binary Tree number of external nodes is n+1

The Function s() For any node x in an extended binary tree, let s(x) be the length of a shortest path from x to an external node in the subtree rooted at x.

s() Values Example

Properties Of s() If x is an external node, then s(x) = 0. Otherwise, s(x) = min {s(leftChild(x)), s(rightChild(x))} + 1

Height Biased Leftist Trees A binary tree is a (height biased) leftist tree iff for every internal node x, s(leftChild(x)) >= s(rightChild(x))

A Leftist Tree

Leftist Trees – Property 1 In a leftist tree, the rightmost path is a shortest root to external node path and the length of this path is s(root).

A Leftist Tree Length of rightmost path is 2.

Leftist Trees—Property 2 The number of internal nodes is at least 2 s(root) - 1 Because levels 1 through s(root) have no external nodes.

A Leftist Tree Levels 1 and 2 have no external nodes.

Leftist Trees—Property 3 Length of rightmost path is O(log n), where n is the number of (internal) nodes in a leftist tree. Property 2 =>  n >= 2 s(root) – 1 => s(root) <= log 2 (n+1) Property 1 => length of rightmost path is s(root).

Leftist Trees As Priority Queues Min leftist tree … leftist tree that is a min tree. Used as a min priority queue. Max leftist tree … leftist tree that is a max tree. Used as a max priority queue.

A Min Leftist Tree

Some Min Leftist Tree Operations put removeMin() meld() initialize() put() and removeMin() use meld().

Put Operation put(7) Create a single node min leftist tree. 7 Meld the two min leftist trees.

Remove Min Remove the root.

Remove Min Remove the root. Meld the two subtrees.

Meld Two Min Leftist Trees Traverse only the rightmost paths so as to get logarithmic performance.

Meld Two Min Leftist Trees Meld right subtree of tree with smaller root and all of other tree.

Meld Two Min Leftist Trees Meld right subtree of tree with smaller root and all of other tree.

Meld Two Min Leftist Trees Meld right subtree of tree with smaller root and all of other tree.

Meld Two Min Leftist Trees 8 6 Meld right subtree of tree with smaller root and all of other tree. Right subtree of 6 is empty. So, result of melding right subtree of tree with smaller root and other tree is the other tree.

Meld Two Min Leftist Trees Swap left and right subtrees if s(left) < s(right). Make melded subtree right subtree of smaller root

Meld Two Min Leftist Trees Make melded subtree right subtree of smaller root. Swap left and right subtree if s(left) < s(right).

Meld Two Min Leftist Trees Swap left and right subtree if s(left) < s(right). Make melded subtree right subtree of smaller root

Meld Two Min Leftist Trees

Initializing In O(n) Time Create n single-node min leftist trees and place them in a FIFO queue. Repeatedly remove two min leftist trees from the FIFO queue, meld them, and put the resulting min leftist tree into the FIFO queue. The process terminates when only 1 min leftist tree remains in the FIFO queue. Analysis is the same as for heap initialization.

Arbitrary Remove Remove element in node pointed at by x. L x A B R x = root => remove min.

Arbitrary Remove, x != root L x A B R Make L right subtree of p. Adjust s and leftist property on path from p to root. Meld with R. p

Skew Heap Similar to leftist tree No s() values stored Swap left and right subtrees of all nodes on rightmost path rather than just when s(l(x)) < s(r(x)) Amortized complexity of each operation is O(log n)