0% found this document useful (0 votes)
189 views

Leftist Trees Extended Binary Trees

Leftist trees are a type of self-balancing binary search tree that can be used as a priority queue. They support insert, remove minimum, and meld operations in O(log n) time where n is the number of nodes. To meld two leftist trees, their rightmost paths are merged by making the melded subtree the right child of the root with the smaller s value, where s is the depth of the node.

Uploaded by

Mike Ash
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
189 views

Leftist Trees Extended Binary Trees

Leftist trees are a type of self-balancing binary search tree that can be used as a priority queue. They support insert, remove minimum, and meld operations in O(log n) time where n is the number of nodes. To meld two leftist trees, their rightmost paths are merged by making the melded subtree the right child of the root with the smaller s value, where s is the depth of the node.

Uploaded by

Mike Ash
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Leftist Trees Extended Binary Trees

Linked binary tree.


Can do everything a heap can do and in the Start with any binary tree and add an
same asymptotic complexity. external node wherever there is an
ƒ insert
empty subtree.
ƒ remove min (or max) Result is an extended binary tree.
ƒ initialize
Can meld two leftist tree priority queues in
O(log n) time.

A Binary Tree An Extended Binary Tree

number of external nodes is n+1


s() Values Example
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()
2

If x is an external node, then s(x) = 0.


2 1

2 1 1 0 Otherwise,
s(x) = min {s(leftChild(x)),
1 1 0 0 1 0 s(rightChild(x))} + 1

0 0 0 0 0 0
A Leftist Tree
Height Biased Leftist Trees
2

A binary tree is a (height biased) leftist tree


2 1
iff for every internal node x,
s(leftChild(x)) >= s(rightChild(x)) 2 1 1 0

1 1 0 0 1 0

0 0 0 0 0 0

A Leftist Tree
Leftist Trees – Property 1
2

In a leftist tree, the rightmost path is a


2 1
shortest root to external node path and
the length of this path is s(root). 2 1 1 0

1 1 0 0 1 0

0 0 0 0 0 0

Length of rightmost path is 2.


A Leftist Tree
Leftist Trees—Property 2
2

The number of internal nodes is at least


2 1
2s(root) - 1
Because levels 1 through s(root) have no 2 1 1 0
external nodes.
1 1 0 0 1 0

0 0 0 0 0 0

Levels 1 and 2 have no external nodes.

Leftist Trees As Priority Queues


Leftist Trees—Property 3

Length of rightmost path is O(log n), where


n is the number of (internal) nodes in a Min leftist tree … leftist tree that is a min tree.
leftist tree. Used as a min priority queue.
Max leftist tree … leftist tree that is a max tree.
Property 2 =>
ƒ n >= 2s(root) – 1 => s(root) <= log2(n+1)
Used as a max priority queue.
Property 1 => length of rightmost path is
s(root).
A Min Leftist Tree Some Min Leftist Tree Operations
2

put
4 3
removeMin()
6 8 5
meld()
8 6 9 initialize()
put() and removeMin() use meld().

Remove Min
Put Operation
2
put(7) 2
4 3

4 3
6 8 5

6 8 5

8 6 9

8 6 9
Remove the root.
Create a single node min leftist tree. 7

Meld the two min leftist trees.


Remove Min Meld Two Min Leftist Trees
2

4 3 4 3

6 8 5 6 8 5 6

8 6 9 8 6 9

Remove the root.


Traverse only the rightmost paths so as to get
Meld the two subtrees. logarithmic performance.

Meld Two Min Leftist Trees Meld Two Min Leftist Trees
4 3
4 3

6 8 5 6
6 8 5 6

8 6 9
8 6 9

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

6 8

8 6

Meld right subtree of tree with smaller root and all of Meld right subtree of tree with smaller root and all of
other tree. 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 Meld Two Min Leftist Trees
6 4
8 4
6

6 6
Make melded subtree right subtree of smaller root. 6
8
6
8 6 8
8 6

8
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) < s(right). 6

Swap left and right subtree if s(left) < s(right).


8
Meld Two Min Leftist Trees Meld Two Min Leftist Trees
3 3

5 4
4
5

6 6
9 6 6
9

8 6 8
8 6 8

Make melded subtree right subtree of smaller root.

Swap left and right subtree if s(left) < s(right).

Initializing In O(n) Time Arbitrary Remove


• Create n single-node min leftist trees and
Remove element in node pointed at by x.
place them in a FIFO queue.
• Repeatedly remove two min leftist trees from
the FIFO queue, meld them, and put the A
resulting min leftist tree into the FIFO queue. x
• The process terminates when only 1 min leftist
tree remains in the FIFO queue. B
• Analysis is the same as for heap initialization. R
L
x = root => remove min.
Arbitrary Remove, x != root Skew Heap

A • Similar to leftist tree


p
x • No s() values stored
• Swap left and right subtrees of all nodes on
B
rightmost path rather than just when s(l(x))
R < s(r(x))
L
Make L right subtree of p. • Amortized complexity of each operation is
O(log n)
Adjust s and leftist property on path from p to root.
Meld with R.

You might also like