Initializing a Max Heap and Leftist Tree, Study notes of Data Structures and Algorithms

How to initialize a max heap using an input array and the time complexity of the algorithm. It also introduces the concept of a leftist tree, a type of binary tree that can be used as a priority queue, and discusses its properties and operations.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-t1z
koofers-user-t1z 🇺🇸

9 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Initializing A Max Heap
input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
8
4
7
6 7
8 9
3
7
10
1
11
5
2
Initializing A Max Heap
Start at rightmost array position that has a child.
8
4
7
6 7
8 9
3
7
10
1
11
5
2
Index is n/2.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Initializing a Max Heap and Leftist Tree and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Initializing A Max Heap

input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Initializing A Max Heap

Start at rightmost array position that has a child.

Index is n/2.

Initializing A Max Heap

Move to next lower array position.

Initializing A Max Heap

Initializing A Max Heap

Initializing A Max Heap

Initializing A Max Heap

Find a home for 2.

Initializing A Max Heap

Find a home for 2.

Initializing A Max Heap

Find home for 1.

Initializing A Max Heap

Find home for 1.

Initializing A Max Heap

Find home for 1.

Initializing A Max Heap

Done.

Leftist Trees

Linked binary tree.

Can do everything a heap can do and in thesame asymptotic complexity.

Can meld two leftist tree priority queues in O(log n) time.

Extended Binary Trees

Start with any binary tree and add anexternal 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+

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 treeiff for every internal node x,

s(leftChild(x)) >= s(rightChild(x))

A Leftist Tree

Leftist Trees—Property 2

The number of internal nodes is at least 2 s(root) - 1

Because levelsexternal nodes. 1 through s(root) have no

So, s(root) <= log(n+1)

A Leftist Tree

Levels 1 and 2 have no external nodes.

Leftist Trees—Property 3

Length of rightmost path is n is the number of nodes in a leftist tree. O(log n), where

Follows from Properties 1 and 2.

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.

Put Operation

put(7)

Put Operation

put(7)

Create a single node min leftist tree. 7

Put Operation

put(7)

Create a single node min leftist tree.

Meld the two min leftist trees.

Remove Min