## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

CSE4101/5101

LEFTIST HEAPS

Leftist heaps are a data structure for representing priority queues. They were discovered by Clark A. Crane [1971] (also described in Knuth, Art of Computer Programming, vol. 3, 1973, pp. 151-152) and have the following nice properties: • INSERT and DELETEMIN operations can be performed in O(log n) time in the worst case — as with standard heaps. • In addition, the UNION operation joins two leftist heaps with n and m nodes, respectively, into a single leftist heap in O(log (max(m, n))) time in the worst case. The two old heaps are distroyed as a result. (Other names used in the literature for UNION are meld, join, merge.) • The rearrangement of nodes following an INSERT, DELETEMIN or UNION operation involves changing pointers, not moving records. In contrast, in the array representation of standard heaps, upheap and downheap involves exchanging contents of array elements. The difference could be signiﬁcant if the elements in the priority queue are sizeable objects (e.g. themselves arrays), in which case we can no longer assume that exchanging two array entries takes constant time.† Deﬁnition 1: The distance of a node m in a tree, denoted dist[m], is the length of the shortest path from m down to a descendant node that has at most one child. Deﬁnition 2: A leftist heap is a binary tree such that for every node m, (a) key[m] ≤ key[lchild[m]] and key(m) ≤ key[rchild[m]], and (b) dist[lchild[m]] ≥ dist[rchild[m]]. In the above deﬁnition, key[m] is the key stored at node m. We assume that there is a total ordering among the keys. We also assume that key[nil] = ∞ and dist[nil] = − 1. Deﬁnition 3: The right path of a tree is the path m1 , m2, ... , m k where m1 is the root of the tree, m i+1 = rchild[m i ] for 1 ≤ i < k, and rchild[m k ] = nil. Figure 1 below shows two examples of leftist heaps. Here are a few simple results about leftist heaps that you should be able to prove easily: Fact 1: The left and right subtrees of a leftist heap are leftist heaps. Fact 2: The distance of a leftist heap’s root is equal to the length of the tree’s right path. Fact 3: For any node m of a leftist heap, dist[m] = dist[rchild[m]] +1 (where, as usual, we take dist[nil] = − 1).

† Note, however, that in this case we could still use the array representation for heaps, now storing in its entries pointers to the heap’s nodes rather than the (large) nodes themselves. Then upheap and downheap can be done by exchanging pointers, while leaving the nodes themselves ﬁxed.

n R ≥ 2k −1 and n L ≥ 2k −1. Let T L . 8 while the right path of T2 is 1. we simply interchange the left and right children of T1 ’s root (thereby making what used to be the right . n R be the number of nodes in T L and T R . T R and T L are both leftist heaps. T R be the left and right subtrees of T . In the examples above. otherwise we want to join two non-empty trees T1 and T2 and we can assume. 5.-2- 1 2 3 1 4 0 9 0 12 1 15 0 20 0 T 1 T 2 2 0 3 8 0 5 0 6 0 4 0 0 1 0 5 1 6 0 13 0 Figure 1. let k be the length of T ’s right path and n be the number of nodes in T . Consider two cases: Case 1: k=0 (i. The right path of T1 is 1. k k k+1 k+1 n ≥ 2 −1 + 2 −1 +1 = 2 −1. Theorem: If the length of the right path in a leftist tree T is k then T has at least 2k+1 −1 nodes. DELETEMIN and UNION. Since T L . From this we immediately get Corollary: The right path of a leftist heap with n nodes has length ≤ log(n + 1) − 1. as wanted. Indeed. without loss of generality. Inductive Step (h>0): Suppose the theorem holds for all leftist heaps that have height < h and let T be a leftist heap of height h. Further. T has no right subtree). T has 1 ≥ 21 −1 nodes. If this has made the distance of the right subtree’s root longer than the distance of the left subtree’s root. T R have height < h we get. that the key in the root of T1 is ≤ the key in the root of T2 . Therefore n ≥ 2 −1. n L . Now let’s examine the algorithm for joining two leftist heaps. Case 2: k>0. By Facts 2 and 3. The idea is simple: if one of the two trees is empty we’re done. and k L . But n = n L + n R +1 and thus. k R be the lengths of the right paths in T L and T R respectively. k R = k − 1. Proof: By induction on the height h of T . In each node we record its key at the top half and its distance at the bottom half. as wanted. as wanted. the following theorem shows. and by deﬁnition of leftist tree k L ≥ k R . in INSERT. all activity takes place along the right path which. by induction hypothesis. But then clearly n ≥1 = 21 −1. Recursively we join T2 with the right subtree of T1 and we make the resulting leftist heap into the right subtree of T1 .e. However. T2 illustrates the fact that leftist heaps can be unbalanced. By Fact 1. Basis (h=0): Then T consists of a single node and its right path has length k=0. is short.

Let the number of nodes in these trees be n1 . n2). n2))) in the worst case. Thus p1 + p2 ≤ log n1 + log n2 + 2. Figure 2 below shows an example of the UNION operation. p2 ≤ log (n2 +1) . r2) if r1 = nil then return r2 else if r2 = nil then return r1 else if key[r1] > key[r2] then r1 ← r2 → rchild[r1] ← UNION ( rchild[r1] . By the above Corollary we have p1 ≤ log (n1 +1) . We assume that each node of the leftist heap is represented as a record with the following format lchild key dist rchild where the ﬁelds have the obvious meanings. and returns a pointer to the root of the resulting leftist heap. Finally. The following pseudo-code gives more details. Let n = max(n1. n2 . observe that there is a constant number of steps that must be executed before and after each recursive call to UNION. /* The following algorithm joins two leftist heaps whose roots are pointed at by r1 and r2 . we update the distance of T1 ’s root.-3subtree of T1 into its left subtree and vice-versa). in the worst case. A leftist heap is speciﬁed by giving a pointer to its root. Then p1 + p2 ≤ 2 log n + 2. r2 ) if d(rchild[r1]) > d(lchild[r1]) then rchild[r1] ← lchild[r1] → dist[r1] ← d(rchild[r1]) +1 return r1 end {UNION} function d(x) /* returns dist(x) */ if x = nil then return − 1 else return dist[x] end {d} What is the complexity of this algorithm? First. Therefore. UNION is called at most 2 log n + 2 times and the complexity of the algorithm is O(log (max(n1. */ function UNION (r1 . Armed with the UNION algorithm we can easily write algorithms for INSERT and DELETEMIN: . Thus the complexity of the algorithm is proportional to the number of recursive calls to UNION. It is easy to see that. this will be equal to p1 + p2 where p1 (respectively p2 ) is 1 plus the length of the right path of the leftist heap whose root is pointed at by r1 (respectively r2 ).

we note that INSERT can be written as in the heap representation of priority queues. As an exercise. On the other hand. As an exercise. The UNION oparation. by adding the new node at the end of the right path. r is pointer to root of tree} 1.-4- 11 1 35 1 39 0 37 0 31 0 15 0 24 0 28 0 22 1 26 0 14 2 16 1 20 0 18 0 UNION 11 2 14 2 22 1 24 0 28 0 26 0 20 0 16 1 18 0 15 1 31 0 39 0 35 1 37 0 Figure 2. return UNION (r´ . we cannot use the idea of percolating values down to implement DELETEMIN in leftist heaps the way we did in standard heaps: doing so would result in an algorithm with O(n) worst case complexity. In closing. rchild[r]) 3. percolating its value up (if necessary). and switching right and left children of some nodes (if necessary) to maintain the properties of the leftist heap after the insertion. r ← UNION(lchild[r] . INSERT(e. r) {e is an element. write the INSERT algorithm for leftist heaps in this fashion. return min. construct a leftist heap where this worst case behaviour would occur. r). where n is the number of nodes in the leftist heap. DELETEMIN(r) 1. min ← element stored at r (root of leftist heap) 2. . Let r´ be a pointer to the leftist heap containing only e 2. By our analysis of the worst case time complexity of UNION it follows immediately that the complexity of both these algorithms is O(log n) in the worst case.

- Non-linear Data StructuresKavialagan Arjunan
- Comp1927 Qsvem
- tree2004.pdfJenner Patrick Lopes Brasil
- Correctness Analysis1vkry007
- Midterm 3 ReviewPri Garimalla
- 2-1BinarySearchTreesmohamed.khalid
- Cs1201 Design and Analysis of Algorithmmaharajaking
- En Tanagra Perfs Comp Decision TreeRodolfo Sabala Veiles
- Data Structuressabarish001
- multidim indexing operationsmanishbhardwaj8131
- Assignment b41Kamal Ch Das
- Us 8458308david19775891
- Binary TreesLubna Ahmed
- Treescrobby777
- AdobeRanjithkumar Tamilselvam
- CodingInterview_LeetCodenareshkanduru
- Ds, c, c++, Aptitude, Unix, Rdbms, SQL, Cn, Osarchitjain
- 7223815 Data Structures Aptitudewadonga
- 18-avlabhi231594
- Data Structures ConceptsManoj Kumar
- CS 2000 Gate Question PaperVinay Gupta
- Reheating StevenSteven Yoo
- TreeNotes.docxRahul Rawat
- m3-search.pptMohammad Gulam Ahamad
- MELJUN CORTES ALGORITHM_Coping With the Limitations of Algorithm PowerMELJUN CORTES, MBA,MPA
- Fast Data Collection in Tree-Based Wireless Sensor Networkshariom2009
- Binary Tree TraversalShravaniSangaonkar
- 9781783553211_Learning_Python_Testing_Sample_ChapterPackt Publishing
- DS manualKavinhariharasudhan Sankaralingam
- 07-kdtreesmurad

- UT Dallas Syllabus for cs3v95.001 05s taught by Greg Ozbirn (ozbirn)UT Dallas Provost's Technology Group
- System Analysis and Design MCQ'SGuruKPO
- The Tower Undergraduate Research Journal Volume IIThe Tower Undergraduate Research Journal
- UT Dallas Syllabus for mis6323.001.09s taught by Jayatirtha Asundi (jxa027000)UT Dallas Provost's Technology Group
- SCT 2011Sunway University
- UT Dallas Syllabus for cs4v95.001.09f taught by Xiaohu Guo (xxg061000)UT Dallas Provost's Technology Group
- GEARS January-February 2016Rodger Bland
- UT Dallas Syllabus for ba4318.0u1.09u taught by Abhijeet Ghoshal (axg053200)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for se3376.5e3.11s taught by Mark Nelson (mrn017000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for se3376.002.10f taught by Jeyakesavan Veerasamy (veerasam)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs4337.001.09f taught by Shyam Karrah (skarrah)UT Dallas Provost's Technology Group
- Object Oriented Programming 2011GuruKPO
- UT Dallas Syllabus for cs1136.002 06s taught by George Steinhorst (csteinh)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs1136.002 05f taught by George Steinhorst (csteinh)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs1336.001 05s taught by Martha Sanchez (mxs015000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs1336.501.11f taught by Feliks Kluzniak (fxk083000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs1336.002 06s taught by Laurie Thompson (lthomp)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for se4376.001.08s taught by Yuke Wang (yuke)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs5375.502 05s taught by Rafael Lacambra (rml021000)UT Dallas Provost's Technology Group
- Implementation OF irrigation Process using embedded PLCInternational Journal for Scientific Research and Development
- School of Computer Technology - Sunway University College 2011Sunway University
- UT Dallas Syllabus for cs4337.001.10f taught by Shyam Karrah (skarrah)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs4337.501 06s taught by Shyam Karrah (skarrah)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs1337.003.07s taught by Timothy Farage (tfarage)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for mis6323.501 05f taught by Jayatirtha Asundi (jxa027000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs3375.001 05s taught by Rafael Lacambra (rml021000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs1337.521 05u taught by George Steinhorst (csteinh)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs1336.003 05s taught by George Steinhorst (csteinh)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs4337.021 06u taught by Shyam Karrah (skarrah)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs4337.001 06s taught by Shyam Karrah (skarrah)UT Dallas Provost's Technology Group

- ClassMate - The Academic AssistantInternational Journal for Scientific Research and Development
- Prediction of changes in facial structure in anaplasty using KinectInternational Journal for Scientific Research and Development
- The benefits of collaboration in TOPdeskTOPdesk
- nullM-NCPPC
- A Survey on Connected Target Coverage Algorithms in WSNInternational Journal for Scientific Research and Development
- AUTONOMOUS SURFACE CLEANING ROBOT EAMBEDED WITH AT89S52International Journal for Scientific Research and Development
- Novel Approach for Mining E-Commerce Feedback Comments Using Comm Trust Data Mining AlgorithmInternational Journal for Scientific Research and Development
- Supply Chain and Value Chain Management for Sugar FactoryInternational Journal for Scientific Research and Development
- MOUSE CONTROL USING COLOR BANDSInternational Journal for Scientific Research and Development
- GSTV 2015-16 CPNI Procedures.pdfFederal Communications Commission (FCC)
- A Study on CRAN R and MRAN R InterpretersInternational Journal for Scientific Research and Development
- Pi_Llution Monitoring Using Raspberry PiInternational Journal for Scientific Research and Development
- Web Page Pattern Prediction Model Based on Dynamic Apriori AlgorithmInternational Journal for Scientific Research and Development
- tmp5DAF.tmpFrontiers
- Image Processing Techniques For Quality Checking In Food IndustryInternational Journal for Scientific Research and Development
- Tips and TricksTOPdesk
- Ministry of Interior RequestColin Daileda
- Clustering and Summarization of Tweet StreamsInternational Journal for Scientific Research and Development
- Analytical Automation using IOTInternational Journal for Scientific Research and Development
- Ohio-Edison-Co-Trade-Ally-Sign-up-FormGenability
- Identification and Prevention of Masquerade Attack using DDSGA AlgorithmInternational Journal for Scientific Research and Development
- Liveness Detection for Biometric System using Image Quality AssessmentInternational Journal for Scientific Research and Development
- Archiving the Web GuideCAP History Library
- zen.digital Announces BuzzJS 2.0 Conference for January 27th, 2017PR.com
- nullM-NCPPC
- Optimization of Routes for Vehicles on Google MapInternational Journal for Scientific Research and Development
- Copy Move Image Forgery Detection using Pseudo Zernike Moment for Better Detection AccuracyInternational Journal for Scientific Research and Development

Sign up to vote on this title

UsefulNot usefulClose Dialog## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Loading