python BST

http://blog.csdn.net/l153097889/article/details/46774725

#-------------------------------------------------------------------------------
# Name:        BinarySearchTree
# Purpose:     非平衡二叉搜索树
#
# Author:      M.E.Liu
#
# Created:     12/07/2015
# Copyright:   (c) M.E.Liu 2015
# Licence:     
#-------------------------------------------------------------------------------

import BinaryTree as BTree

class BinarySearchTree(BTree.BinaryTree):
    def __init__(self,root=0,count=0,height=0):
        BTree.BinaryTree.__init__(self,root,count,height)

    def insertNode(self,treenode):
        if treenode==0:
            return
        if self.root==0:
            self.root = treenode
            return
        self.count += 1
        currentNode = self.root
        while currentNode :
            if currentNode.datatreenode.data:
                if currentNode.left==0:
                    currentNode.left = treenode
                    return
                else:
                    currentNode = currentNode.left

    def delNode(self,currentnode,data):
        if not currentnode.left and not currentnode.right:
            currentnode.data = None
            return 'leaf'
        if currentnode.data is data:
            if currentnode.left and currentnode.right:
                rightnode = currentnode.right
                rightparent = rightnode
                while rightnode.left:
                    rightparent = rightnode
                    rightnode = rightnode.left
                currentnode.data = rightnode.data
                if self.delNode(rightnode,rightnode.data) is 'leaf':
                    rightparent.left = None
                return 'node'
            else:
                direction = 0 if currentnode.left else 1
                node = currentnode[direction]
                currentnode.data = node.data
                if self.delNode(node,node.data) is 'leaf':
                    currentnode[direction] = None
                return 'node'
        else:
            parent = currentnode
            direction = 0 if currentnode.data > data else 1
            currentnode = parent[direction]
            if self.delNode(currentnode,data) is 'leaf':
                parent[direction] = None


def main():
    n8 = BTree.BinaryTreeNode(8,0,0)
    n6 = BTree.BinaryTreeNode(6,0,0)
    n15 = BTree.BinaryTreeNode(15,0,0)
    n13 = BTree.BinaryTreeNode(13,0,0)
    n11 = BTree.BinaryTreeNode(11,0,0)
    n9 = BTree.BinaryTreeNode(9,0,0)
    n5 = BTree.BinaryTreeNode(5,0,0)
    n3 = BTree.BinaryTreeNode(3,0,0)
    n1 = BTree.BinaryTreeNode(1,0,0)

    n7 = BTree.BinaryTreeNode(7,n5,n8)
    n14 = BTree.BinaryTreeNode(14,n13,n15)
    n10 = BTree.BinaryTreeNode(10,0,n11)
    n12 = BTree.BinaryTreeNode(12,n10,n14)


    n2 = BTree.BinaryTreeNode(2,n1,n3)
    n4 = BTree.BinaryTreeNode(4,n2,n7)
    n9 = BTree.BinaryTreeNode(9,n4,n12)

    root = n9
    bt = BinarySearchTree(root)
    bt.insertNode(n6)

    bt.delNode(root,12)
    bt.delNode(root,9)
    bt.midOrder(bt.root)



##    bt = BinarySearchTree()
##    for i in range(1,16):
##        exec("n"+str(i)+" = BTree.BinaryTreeNode("+str(i)+",0,0)")
##        exec("bt.insertNode(n"+str(i)+")")
##    bt.delNode(4)
##    bt.delNode(5)
##    bt.delNode(6)
##    bt.preOrder(bt.root)

if __name__ == '__main__':
    main()


你可能感兴趣的:(#,Python)