Leftist Trees Linked binary tree.

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

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)
Pairing Heaps. Experimental results suggest that pairing heaps are actually faster than Fibonacci heaps.  Simpler to implement.  Smaller runtime overheads.
1 Pertemuan 19 Leftist Tree Heap Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
© 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.
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 Queues, Heaps & Leftist Trees
Heapsort Based off slides by: David Matuszek
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.
單元7: Heap 定義 for priority queues Leftist trees Binomial heap
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
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.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
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.
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]
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
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))
Sorting With Priority Queue In-place Extra O(N) space
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.
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
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures and may also be used to do an increase key in a min structure (remove.
Interval Heaps Complete binary tree.
Heaps and Priority Queues
Pairing Heaps Actual Complexity.
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Chapter 8 – Binary Search Tree
Priority Queues and Heaps
Initializing A Max Heap
Part-D1 Priority Queues
i206: Lecture 14: Heaps, Graphs intro.
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
"Teachers open the door, but you must enter by yourself. "
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
(2,4) Trees (2,4) Trees (2,4) Trees.
Heaps and Priority Queues
HEAPS.
CMSC 341 Lecture 19.
Heaps By JJ Shepherd.
CS 6310 Advanced Data Structure Wei-Shian Wang
Pairing Heaps Actual Complexity
Heaps & Multi-way Search Trees
A Heap Is Efficiently Represented As An Array
Heaps 9/29/2019 5:43 PM Heaps 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. Meld used in Edmonds directed minimum spanning tree algorithm, for e.g. We have super nodes. One priority queue for each supernode; has incoming edges. Extract edges until you get one that is not a self loop in the current supernode graph. Combine PQs when two supernodes are combined into one.

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

s() Values Example 2 2 1 2 1 1 1 1 1

Properties Of s() If x is an external node, then s(x) = 0. Otherwise, s(x) = min {s(leftChild(x)), s(rightChild(x))} + 1 s() values may be computed easily using a postorder traversal.

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 2 2 1 2 1 1 1 1 Note that the s value decreases by 1 every time you move to a right child of a node. However, when you move to a left child, the s value may increase, though not in above example! Every subtree also is a leftist tree. Note also that every binary tree may be converted into a leftist tree by swapping left and right children whenever the leftist tree property is violated (use a postorder traversal, for example). 1

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 2 2 1 2 1 1 1 1 1 Length of rightmost path is 2.

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

A Leftist Tree Levels 1 and 2 have no external nodes. 2 2 1 2 1 1 1 1 1 1 1 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 >= 2s(root) – 1 => s(root) <= log2(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 2 4 3 6 8 5 9 8 6

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. 8 6 9 5 4 3 2 Create a single node min leftist tree. 7 Meld the two min leftist trees.

Remove Min 2 4 3 6 8 5 9 8 6 Remove the root.

Remove Min 8 6 9 5 4 3 2 Remove the root. Meld the two subtrees.

Meld Two Min Leftist Trees 8 6 9 5 4 3 Traverse only the rightmost paths so as to get logarithmic performance.

Meld Two Min Leftist Trees 4 3 6 8 5 6 9 8 6 Meld right subtree of tree with smaller root and all of other tree.

Meld Two Min Leftist Trees 4 3 6 8 5 6 9 8 6 Meld right subtree of tree with smaller root and all of other tree.

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

Meld Two Min Leftist Trees 6 8 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 6 8 Make melded subtree right subtree of smaller root. 6 8 Swap left and right subtrees if s(left) < s(right). 6 8

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

Meld Two Min Leftist Trees 9 5 3 8 6 4 Make melded subtree right subtree of smaller root. Swap left and right subtree if s(left) < s(right).

Meld Two Min Leftist Trees 3 4 5 6 6 9 8 6 8

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. A x B R L Need parent pointers to support arbitrary remove in O(log n) time. x = root => remove min.

Arbitrary Remove, x != root L x A B R p Note: when adjusting on path from p to root, stop if you reach a node whose s value does not change. Let the retrace path be from p to q. Relabel the nodes on this path p1 (=p), p2, p3, ..., All orientations of pi wrt to p(i+1) are those before the retrace began (i.e., before a swap that may have occurred at p(i+1). If p1  was the left child of p2, then  new s(i+1) = new s(i) + 1 < old s(i+1), for i+1 < q. So, the path has at most log n nodes (all s values are <= log n). If p1 was the right child of p2 and the path is comprised of only of moves from right children, the number of nodes is at most log n as old s values on this path increase by 1 at each move. In the last case, p1 was the right child of p2, and there is a first place where pi was the left child of p(i+1). The segment preceding pi has at most log n nodes (see case 2). The remaining segment has at most log n nodes because of case 1. So, total number of nodes is at most 2 log n. May combine the 3 cases into 1, the pq path has an initial possibly empty segment of moves from right children followed by a possible move from a left child followed by the balance of the path. An alternative is to meld L & R, adjust s values on path from p to root stopping at first node whose s doesn’t change, and then meld again. This takes an extra meld but still works in O(log n) time. Make L right subtree of p. Adjust s and leftist property on path from p to root (stop at first node, if any, whose s value does not change). Meld with R.

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 insert, remove min, meld is O(log n) Can initialize in O(n) using same algorithm as for min leftist tree or build min-heap and convert to leftist heap.