Heaps Or Priority Queues Npl(Node with 2 Children) = Min(Npl(Left Child), Npl(Right Child)) + 1
Total Page:16
File Type:pdf, Size:1020Kb
Heaps or Priority Queues Npl(node with 2 children) = min(Npl(left child), Npl(right child)) + 1 leftist heap property Ming-Hwa Wang, Ph.D. COEN 279/AMTH 377 Design and Analysis of Algorithms Npl(left child) ≥ Npl(right child) Department of Computer Engineering A leftist tree with r nodes on the right path must have at least 2r - 1 Santa Clara University nodes, i.e., a leftist tree of N nodes has a right path containing at most lg(N+1) nodes. Basic Operations perform all work on the right path Insert(H, x) insertion as a merge of a one-node heap with a larger heap DeleteMin(H) or DeleteMax(H) merge DecreaseKey(H, x, D) Recursively merge the heap with a larger root with the right sub- IncreaseKey(H, x, D) heap of the heap with the smaller root. If the result heap is not a Delete(H, x) leftist heap, swap the root's left and right children and update the h+1 BuildHeap(H), at most n/2 nodes of height h null path length. O(lgN) Merge(H1, H2) Non-recursive algorithm: First pass, create a new tree by merging the right paths of both heaps, arrange the nodes on the right paths Applications of H1 and H2 in sorted order, keeping their respective left children. operating system (scheduler) Second pass, made up the heap, and child swaps are performed at selection problem nodes that violate the leftist heap property. implementation of greedy algorithm a binary heap is a leftist heap, but not vice versa event simulation Skew Heaps Binary Heaps Skew heaps have heap order but no structural constraint. Self-adjusting structure property and no need to maintain the path lengths, thus simple to implement. h h+1 complete binary tree 2 ≤ N ≤ 2 - 1, h = lg N O(N), but for M consecutive operations, O(M lgN) array implementation recursively merge the heap with larger root with the right sub-heap of Parent(i) = i/2 the smaller root, and always swap LeftChild(i) = 2i slight loss of balance information is compensated by the lack of testing RightChild(i) = 2i + 1 a leftist heap is a skew heap, but not vice versa percolate up for Insert percolate down for DeleteMin Binomial Queues DecreaseKey and then DeleteMin for Delete a heap-ordered forest with at most one binomial tree of every height heap order property a binomial tree of height 0 is a one-node tree, and a binomial tree Bk of any node ≤ all of its descendants height k is formed by attaching a Bk-1 binomial tree to the root of another has no ordering info other than heap order property Bk-1 binomial tree a binomial tree Bk consists of a root with children B0, B1, .. Bk-1; a d-Heaps binomial tree of height k have exactly 2k nodes, and the number of nodes A heap, which all its nodes have d children at depth d is the binomial coefficient C(k, d) compact representation Leftist Heaps Insertion O(1), other operations O(lgN) null path length Npl(x) Implementation: circular doubly linked list Npl(nil) = -1 Npl(node with 0 or 1 child) = 0 k-d Heaps Pairing Heaps .