heap sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/datastructures/... · web...

31
Leftist- Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much easier and faster o may use already established links to merge with a new node why so much faster o because we are using Binary Trees!! values STILL obey a heap order (partially ordered) uses a null path length to maintain the structure (covered later) o the null path of and node’s left child is >= null path of the right child at every node, the shortest path to a non-full node is along the rightmost path this overall ADT supports o findMin = O(1) o deleteMin = O(log n) o insert = O(log n) o construct = O(n) o merge = O(log n) 1

Upload: others

Post on 17-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Leftist-HeapsIn theory

not a political thing uses a Binary Tree (BT)!! merging heaps is much easier and faster

o may use already established links to merge with a new node why so much faster

o because we are using Binary Trees!! values STILL obey a heap order (partially ordered) uses a null path length to maintain the structure (covered later)

o the null path of and node’s left child is >= null path of the right child at every node, the shortest path to a non-full node is along the rightmost path this overall ADT supports

o findMin = O(1)o deleteMin = O(log n)o insert = O(log n)o construct = O(n)o merge = O(log n)

this is fantastic compared to regular heap merging!!

Example of a Leftist Heap

1

Page 2: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Null Path Length (npl) length of shortest path from current node (X) to a node without 2 children

o value is store IN the node itself leafs = 0 nodes with only 1 child = 0 how to count NPLs

o http://youtu.be/lZvXQH5O2i4

Determining the npl for a node

lhs >= rhsDetermine the npls for each node in the trees below. Are these left-ist heaps?

I know, it’s a max heap

lhs >= rhsanswersb:

2

Page 3: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

The Leftist Node the node will have many data members this time

o links (left and righto element (data)o npl

by default, the LeftistHeap sets and empty one as the root

The leftist Node Class and CodeJava private LeftistNode<AnyType> root; // root

private static class LeftistNode<AnyType> { // Constructors LeftistNode( AnyType theElement ) { this( theElement, null, null ); }

LeftistNode( AnyType theElement, LeftistNode<AnyType> lt, LeftistNode<AnyType> rt ) { element = theElement; left = lt; right = rt; npl = 0; }

AnyType element; // The data in the node LeftistNode<AnyType> left; // Left child LeftistNode<AnyType> right; // Right child int npl; // null path length }C++ private: struct LeftistNode { Comparable element; LeftistNode *left; LeftistNode *right; int npl;

LeftistNode( const Comparable & theElement, LeftistNode *lt = NULL, LeftistNode *rt = NULL, int np = 0 ) : element( theElement ), left( lt ), right( rt ), npl( np ) { } };

LeftistNode *root;

3

Page 4: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Building a Left-ist Heap value of node STILL matters, lowest value will be root, so still a min Heap data entered is random uses CURRENT npl of a node to determine where the next node will be placed algorithm

o add new node to right-side of tree, in ordero if new node is to be inserted as a parent (parent > children)

make new node parent link children to it link grandparent down to new node (now new parent)

o if leaf, attach to right of parento if no left sibling, push to left (hence left-ist)

why?? (answer in a second)o else left node is present, leave at right childo update all ancestors’ nplso check each time that all nodes left npl > right npls

if not, swap children or node where this condition exists this is really using heaps and links!!

4

Page 5: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Moves in Building a HeapSwing Left

Normal insertion of a new node into the tree. First placed as far right as possible, then swung left to satisfy npls.

Insert new root (and a swing)The newest value MUST be higher in the min leftist tree. 50 and 75 were attached and swung left.

Insert “in between”40 is inserted, 50 is reattached and swung

Swap ChildrenTo keep the left-ist tree npl compliant, swap children of node NOT compliant and their respective kids

5

Page 6: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Building a leftist Heap21, 14, 17, 10, 3, 23, 26, 8

https://youtu.be/GAln1pPZWBw (View here)inserting 14

inserting 17

inserting 10

inserting 3

6

Page 7: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

inserting 23

inserting 26

inserting 8

7

Page 8: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Why can we NOT have this??

Why does 26 HAVE to be moved left?

we can have it where a node’s left npl is greater than it’s right nplo simply, we swap children

Swapping children to save a leftist tree

Just inserted 52 into the leftist heap

What’s the issue now?

8

Page 9: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Try creating these leftist heaps on your own: Answersb:

1. 75, 91, 97, 9, 39, 87, 34, 8, 86, 58

Stop. We will go over. Answersb:

2. 71, 4, 13, 73, 52, 20, 50, 63, 85, 23, 1, 443. 24, 80, 98, 30, 77, 35, 65, 2, 48, 92, 18, 37, 67, 96

Why did I bold a few of the numbers above?

9

Page 10: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Inserting – the function in the code, adding a single node is treated at merging a heap (just one node)

with an established heap’s rooto and work from that root as we just went over

we will go over merging whole heaps momentarily But in reality, isn’t ONE node a heap??

The Insert functionJava /** * Insert into the priority queue, maintaining heap order. * @param x the item to insert. */ public void insert( AnyType x ) { root = merge( new LeftistNode<>( x ), root ); }C++

void insert( const Comparable & x ) { root = merge( new LeftistNode( x ), root ); }

Merging Left-ist Heaps THIS IS NOT THE SAME AS INSERT the heaps we are about to merge must be left-ist at end we will get a heap that is

o a min left-ist heap algorithm

o looking at both trees, compare which has the smallest rooto grab the smallest root and all those from its left side and add to the

rightmost side in the finalized treeo REPEADLY, until no lists left unmerged.

Always adding to RIGHT of finalized treeo Verify that it is a Min Heap!! (Parent < Children)o Verify a leftist heap! (left npl <= right npl)

using stack created on which nodes to check (usually bottom up) if not, swap troubled node with sibling

10

Page 11: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Initial Left-ist Heaps

Start at the root, and finalize the node AND LEFT with the smallest value

4

finalized Stack

option 1 option 2

Start at the root of the sub-tree, and finalize the node AND LEFT with the next smallest value. Add to RIGHT of finalized tree. Verify a leftist heap!

64

Finalized

option1option2

Start at the root of the sub-tree, and finalize the node AND LEFT with the next smallest value. Add to RIGHT of finalized tree. Verify a leftist heap!

11

Page 12: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

764

Finalized

option 1option 2

Start at the root of the sub-tree, and finalize the node AND LEFT with the next smallest value. Add to RIGHT of finalized tree. Verify a leftist heap!8764

Starting with TOP of Stack, check NPL compliance (left npl >= right npl)

Pop node from stack if ok. (8 was ok, 7 not so much)8764

12

Page 13: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

13

Page 14: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Switch problem node with sibling. All links stay in same direction.

Verify that it is a Min Heap!! (Parent < Children)

Yup

Remember, to check NPL compliance at end of building

14

Page 15: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Try these:

#1

Work shown here:What was your first step??

DO THIS ONE AND STOP!!! Will go over together.#2

a lot of work on this one

Answersb:#3

I know!! Heap is a MAX heap!! Try it anyway! (max first!)

Answersb

15

Page 16: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Merging – the function notice it is recursive! merge()

o version 1 – copy rhs to rooto version 2 - is the function to set up the order between left and right heaps

merge1() is the function to actually do the linking and swapping if left npl > right nplo notice npl is a private variable

16

Page 17: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Merging HeapsJava /** * Merge rhs into the priority queue. * rhs becomes empty. rhs must be different from this. * @param rhs the other leftist heap. */ public void merge( LeftistHeap<AnyType> rhs ) { if( this == rhs ) { return; } // Avoid aliasing problems

root = merge( root, rhs.root ); rhs.root = null; }

/** * Internal method to merge two roots. * Deals with deviant cases and calls recursive merge1. */ private LeftistNode<AnyType> merge( LeftistNode<AnyType> h1, LeftistNode<AnyType> h2 ) { if( h1 == null ) { return h2; } if( h2 == null ) { return h1; } if( h1.element.compareTo( h2.element ) < 0 ) { return merge1( h1, h2 ); } else { return merge1( h2, h1 ); } }

/** * Internal method to merge two roots. * Assumes trees are not empty, and h1's root contains smallest item. */ private LeftistNode<AnyType> merge1( LeftistNode<AnyType> h1, LeftistNode<AnyType> h2 ) { if( h1.left == null ) // Single node h1.left = h2; // Other fields in h1 already accurate else { h1.right = merge( h1.right, h2 ); if( h1.left.npl < h1.right.npl ) { swapChildren( h1 ); } h1.npl = h1.right.npl + 1; } return h1; }

/** * Swaps t's two children. */ private static <AnyType> void swapChildren( LeftistNode<AnyType> t ) { LeftistNode<AnyType> tmp = t.left; t.left = t.right; t.right = tmp;

17

Page 18: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

}C++ /** * Merge rhs into the priority queue. * rhs becomes empty. rhs must be different from this. */ void merge( LeftistHeap & rhs ) { if( this == &rhs ) // Avoid aliasing problems return;

root = merge( root, rhs.root ); rhs.root = NULL; }

/** * Internal method to merge two roots. * Deals with deviant cases and calls recursive merge1. */ LeftistNode * merge( LeftistNode *h1, LeftistNode *h2 ) { if( h1 == NULL ) return h2; if( h2 == NULL ) return h1; if( h1->element < h2->element ) return merge1( h1, h2 ); else return merge1( h2, h1 ); }

/** * Internal method to merge two roots. * Assumes trees are not empty, and h1's root contains smallest item. */ LeftistNode * merge1( LeftistNode *h1, LeftistNode *h2 ) { if( h1->left == NULL ) // Single node h1->left = h2; // Other fields in h1 already accurate else { h1->right = merge( h1->right, h2 ); if( h1->left->npl < h1->right->npl ) swapChildren( h1 ); h1->npl = h1->right->npl + 1; } return h1; }

18

Page 19: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

19

Page 20: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Deleting from a leftist Heap simple to just remove a node (since at top)

o this will make two trees! merge the two trees like we just did!!!

Merge the two trees, but min is in left, so recursively merge right into left

So 23 is the min node for Trees 1 and 10 is for tree 2

20

Page 21: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Right side is not NPL compliant so swap

21

Page 22: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Right side is not NPL compliant so swap

Delete the root and re-merge the tree below.

22

Page 23: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

So why did we do this? fast!

o merge with two trees of size n O(log n), we are not creating a totally new tree!! some was used as the LEFT side!

o inserting into a left-ist heap O(log n) same as before with a regular heap

o deleteMin with heap size n O(log n) remove and return root (minimum value) merge left and right subtrees

real life applicationo priority queue

homogenous collection of comparable items smaller value means higher priority

23

Page 24: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Answers:Counting NPLs

Leftist Heap Creation Exercise #1

http://youtu.be/Kbrli4On158

24

Page 25: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Leftist Heap Creation Exercise #2

http://youtu.be/Hvmb6uYBP-E

Leftist Heap Creation Exercise #3

http://youtu.be/eXiUEKOg7ZI

Merging Left-ist Heaps #1

https://youtu.be/3jMcEed-GlE

25

Page 26: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Merging Left-ist Heaps #2

26

Page 27: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Merging Left-ist Heaps #3

27

Page 28: Heap Sort - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/... · Web viewLeftist-Heaps In theory not a political thing uses a Binary Tree (BT)!! merging heaps is much

Sources:In Generalhttp://courses.cs.washington.edu/courses/cse332/13wi/lectures/cse332-13wi-lec04-BinMinHeaps-6up.pdfhttp://courses.cs.washington.edu/courses/cse326/08sp/lectures/05-leftist-heaps.pdf

Minimum Heap Building/Deletionhttp://www.cs.usfca.edu/~galles/visualization/Heap.html

Maximum HeapSort http://www.cs.usfca.edu/~galles/visualization/HeapSort.html

Random Heapsorthttp://www.cse.iitk.ac.in/users/dsrkg/cs210/applets/sortingII/heapSort/heapSort.htmlhttp://nova.umuc.edu/~jarc/idsv/lesson3.html

Building a Leftist Heaphttp://www.cs.usfca.edu/~galles/visualization/LeftistHeap.html

CodeC++ http://users.cis.fiu.edu/~weiss/dsaa_c++3/code/BinaryHeap.h

28