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
31
Embed
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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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;
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
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
Building a leftist Heap21, 14, 17, 10, 3, 23, 26, 8
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
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
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
13
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
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!)
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
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