python實現非平衡二叉樹

專注的阿熊發表於2021-08-06

class streenode:

     def __init__(self, key, val):

         self.right = None

         self.left = None

         self.key = key

         self.val = val

     def __str__(self):

         return str((self.key,self.val))

class searchtree(object):

     def __init__(self, key, val):

         self.root = streenode(key, val)

     def insert(self, key, val):

         node = self.root

         while node:

             if key > node.key:

                 parent = node

                 node = node.right

             elif key == node.key:

                 node.val = val

                 return

             else:

                 parent = node

                 node = node.left

         node = streenode(key, val)

         if key > parent.key:

             parent.right = node

         else:

             parent.left = node

     def delete(self, key):

         node, parent, kind = self.get(key)

         if node == None:

             return

         if node.right!=None and node.left!=None:

             minnode = 外匯跟單gendan5.comself.getmin(node.right)

             minkey = minnode.key

             minval = minnode.val

             self.delete(minnode.key)

             node.key = minkey

             node.val = minval

         elif node.right==None and node.left==None:

             if kind == "left":

                 parent.left = None

             elif kind == "self":

                 self.root = None

             else:

                 parent.right = None

             del node

         else:

             if kind == "left":

                 parent.left = node.right if node.right else node.left

             elif kind == "self":

                 self.root = node.right if node.right else node.left

             else:

                 parent.right = node.right if node.right else node.left

             del node

     def get(self, key):

         node = self.root

         kind = "self"

         parent = None

         while(node):

             if key > node.key:

                 parent = node

                 kind = "right"

                 node = node.right

             elif key == node.key:

                 return node, parent, kind

             else:

                 parent = node

                 kind = "left"

                 node = node.left

         return None, None, None

     def getmin(self, root):

         if root.left == None:

             return root

         return self.getmin(root.left)

     def inorder(self, root):

         if root == None:

             return

         self.inorder(root.left)

         print(root)

         self.inorder(root.right)

     def preorder(self, root):

         if root == None:

             return

         print(root)

         self.preorder(root.left)

         self.preorder(root.right)

     def postorder(self, root):

         if root == None:

             return

         self.postorder(root.left)

         self.postorder(root.right)

         print(root)

a = searchtree(4.5,"a")

a.insert(2,"a")

a.insert(3,"a")

a.insert(4,"a")

a.insert(4.2,"a")

a.insert(4.1,"a")

a.insert(4.7,"a")

a.insert(5,"a")

a.insert(8,"a")

a.insert(9,"a")

a.delete(4)

a.delete(4.5)

a.inorder(a.root)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2785715/,如需轉載,請註明出處,否則將追究法律責任。

相關文章