Python data structures and algorithms--complete tree and minimum/large heap

Source: Internet
Author: User

1 #Complete Tree Minimum heap2 classCompletetree (list):3     defSiftdown (self,i):4         """a full tree is adjusted downward, passing in the node number I that needs to be adjusted downwards5 When the smallest element is removed, when a new number is added to the top of the heap,6 If you do not meet the characteristics of the minimum heap at this time, you need to adjust the number downward until you find a suitable location"""7n =len (self)8         #When the i node has a son (at least the left son), and there is a need to adjust, the loop executes9t =0Ten          whilei*2+1<N: One             #Step 1: From the current node, its left son, whose right son finds the smallest one, passes its number to T A             ifSelf[i] > Self[i*2+1]:  -t = i*2+1 -             Else: t =I the              -             #If there is a right son, then the right son will be discussed -             ifi*2+2<N: -                 ifSelf[t] > self[i*2+2]: t = i*2+2 +              -             #Step 2: Exchange The elements of the smallest node and the elements of node I +             ifT! =I: ASelf[t],self[i] =Self[i],self[t] ati = t#Update I is the number of the son node that I just exchanged with it so that I can continue to adjust downwards . -             Else: -                  Break   #indicates that the current parent node is smaller than the two child nodes, ending the adjustment . -          -     defSiftup (self,i): -         """An upward adjustment of a complete tree, passing in a node number I that needs to be adjusted upward in when you want to add a new element, adjust the bottom (last) element of the heap""" -         ifI==0:return ton =len (self) +         ifI < 0:i + =N -         #Note that due to the nature of the heap, there is no need to consider the situation of the left son node. the         #because the parent node is definitely smaller than the child node, it only needs to be compared once . *          whilei!=0: $             ifself[i]<self[(i-1)/2]:Panax Notoginsengself[i],self[(I-1)/2] = self[(i-1)/2],self[i] -             Else: the                  Break +i = (i-1)/2#Update I is the number of its parent node, which makes it easier to continue the upward adjustment next time . A      the     defShufflepile (self): +         """in the current state, the tree is adjusted so that it becomes a heap""" -         #downward adjustment from "heap bottom" to "heap top", which keeps the smallest elements rising $         #This allows the heap below the I node to be the local minimum heap. $          forIinchRange ((Len (self)-2)/2,-1,-1):#N/2,..., 0 - Self.siftdown (i) -  the     defdeletemin (self): -         """Remove Minimum element"""Wuyit = self[0]#record the heap vertex with a temporary variable theSelf[0] = self[-1]#assigns the last point of the heap to the top of the heap -Self.pop ()#Delete last element WuSelf.siftdown (0)#Downward Adjustment -         returnT About      $      -     defheapsort (self): -         """heap ordering of elements in a heap""" -n =len (self) As = [] +          whileN>0: the S.append (Self.deletemin ()) -N-= 1 $         #as the elements in the heap have all popped up, the sorted elements are stitched into the original heap the Self.extend (s) the          the if __name__=="__main__": the  -A = [99,5,36,7,22,17,92,12,2,19,25,28,1,46] inCT =Completetree (a) the     PrintCT the>>> [99, 5, 36, 7, 22, 17, 92, 12, 2, 19, 25, 28, 1, 46] About  the Ct.shufflepile () the     PrintCT the>>> [1, 2, 17, 5, 19, 28, 46, 12, 7, 22, 25, 99, 36, 92] +  -s =Ct.heapsort () the     PrintCTBayi>>> [1, 2, 5, 7, 12, 17, 19, 22, 25, 28, 36, 46, 92, 99]

Python data structures and algorithms--complete tree and minimum/large heap

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.