Python implements an example of a heap sorting algorithm based on binary tree storage structure

Source: Internet
Author: User

In this paper, the algorithm of heap sorting based on binary tree storage structure is described in Python. Share to everyone for your reference, as follows:

Since the implementation of the Python two fork tree, of course, write something to practice practiced hand.

There are many tutorials on the heap ordering on the network, but almost all of them are stored in the array, directly following the index to access the elements, of course, this is completely no problem, simple implementation, access speed, and easy to understand.

But from a practiced hand point of view, I wrote a binary tree. Heap Ordering of storage structures

One of the hardest problems is swapping two nodes in a binary tree.

Because a node is connected to three nodes at most, then two nodes are interchangeable, it is necessary to take into account the relationship between the 5 nodes, also need to judge the child, this will be very cumbersome, but also prone to error.

Class Tree:def __init__ (self, val = ' # ', left = none, right = none): Self.val = val Self.left = left Self.right = Right Self.ponit = None Self.father = None Self.counter = 0 #前序构建二叉树 def frontbuildtree (self): temp = INP UT (' Please Input: ') node = Tree (temp) if (temp! = ' # '): Node.left = self. Frontbuildtree () Node.right = self. Frontbuildtree () return node# because there are no references and no pointers, return the new node back #前序遍历二叉树 def visitnode (self): print (Self.val) if (self). Left! = None): Self.left.VisitNode () if (self.right! = none): Self.right.VisitNode () #中序遍历二叉树 def Mvisittree (self): if (self.left! = none): Self.left.MVisitTree () print (Self.val) if (self.right! = none): Self.righ  T.mvisittree () #获取二叉树的第dec个节点 def getpoint (self, dec): road = str (DEC) [3:] p = Self for R in Road:if (r = = ' 0 '): p = p.left else:p = p.right #print (' P.val = ', p.val) return p #构建第一个堆 def BUILDH  Eadtree (self, List):  For the Val in List: The #print (' val = ', val, ' Self.counter = ', self.counter) Self.ponit = self.        GetPoint (int ((Self.counter + 1)/2)) #print (' Self.ponit.val = ', self.ponit.val) if (Self.counter = = 0): Self.val = val Self.father = Self Else:temp = self.counter + 1 node = Tree (val) node.fa ther = self.ponit if (temp% 2 = = 0): #新增节点为左孩子 self.ponit.left = node Else:self.ponit.righ t = node while (temp! = 0): if (Node.val < node.father.val): #如果新增节点比其父亲节点值要大 p = node.father#              Save three of its chains first lefttemp = Node.left righttemp = Node.right if (p.father! = p): #判断其不是头结点 if (int (TEMP/2)% 2 = = 0): #新增节点的父亲为左孩子 p.father.left = node else:p.               father.right = Node Node.father = P.father Else:node.father = node# is the head node father it to itself.   Node.counter = Self.counter           Self = node if (temp% 2 = = 0): #新增节点为左孩子 node.left = p node.right = p.right              if (p.right! = None): p.right.father = node Else:node.left = P.left Node.right = P if (p.left! = None): p.left.father = node P.left = Left  Temp P.right = righttemp p.father = Node temp = Int (TEMP/2) #print (' Node.val = ', Node.val, ' Node.father.val = ', node.father.val) #print (' Tree = ') #self.      Visitnode () Else:break; Self.counter + = 1 return self #将头结点取出后重新调整堆 def Adjust: #print (' Frontselftree = ') #self. Visitnode () #print (' Mselftree = ') #self. Mvisittree () print (' Get ', self.val) p = self. GetPoint (Self.counter) #print (' P.val = ', p.val) #print (' P.father.val = ', p.father.val) root = P if (self.cou Nter% 2 = = 0): p.father.left = None Else:p.father.right = None #print (' Self.left = ', self.left.val) #print (' Self.right = ', self.right  . val) P.father = p# Move the last leaf node of the two fork tree to the head node P.left = Self.left P.right = self.right while (1): #优化是万恶之源 lefttemp = P.left righttemp = P.right fathertemp = P.father if (p.left! = None and P.right!=none): #判断此时正在处理的结点的左后孩子情 if (P.left.val < P.right.val): next = p.left Else:next = p.right if (p.val &l T      Next.val): break;  elif (P.left = = None and p.right! = None and P.val > P.right.val): Next = P.right elif (p.right = = None and      P.left! = None and P.val > P.left.val): next = P.left else:break; P.left = Next.left P.right = Next.right P.father = Next if (next.left! = None): #之后就是一系列的交换节点的链的处理 NE Xt.left.father = P if (next.right! = None): Next.right.father = P if (fathertemp = = p): next.fathe        R = NextRoot = next Else:next.father = = Fathertemp if (fathertemp.left = = p): Fathertemp.left = Next  Else:FatherTemp.right = Next if (next = = lefttemp): next.right = righttemp Next.left = P if (righttemp! = None): Righttemp.father = Next else:next.left = Lefttemp Next.righ t = P if (lefttemp! = None): Lefttemp.father = Next #print (' Tree = ') #root.  Visitnode () Root.counter = self.counter-1 return rootif __name__ = ' __main__ ': print ("Script home test result") root = Tree () Number = [-1,-1, 0, 0, 0, 3, 5, 4, 3, 1, 6; Buildheadtree (number) while (Root.counter! = 0): root = root. Adjust ()

Running Result: http://www.pdfxs.com/search?q=BEB-107

Python implements an example of a heap sorting algorithm based on binary tree storage structure

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.