Presentation is loading. Please wait.

Presentation is loading. Please wait.

Leftist Trees Linked binary tree.

Similar presentations


Presentation on theme: "Leftist Trees Linked binary tree."— Presentation transcript:

1 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.

2 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.

3 A Binary Tree

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

5 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.

6 s() Values Example

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

8 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.

9 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))

10 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

11 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).

12 A Leftist Tree 2 2 1 2 1 1 1 1 1 Length of rightmost path is 2.

13 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.

14 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.

15 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).

16 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.

17 A Min Leftist Tree 2 4 3 6 8 5 9 8 6

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

19 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.

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

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

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

23 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.

24 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.

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

26 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.

27 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

28 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).

29 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).

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

31 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.

32 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.

33 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.

34 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.


Download ppt "Leftist Trees Linked binary tree."

Similar presentations


Ads by Google