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:     <Artificial Intelligence Lab of HUST>
#-------------------------------------------------------------------------------

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.data<treenode.data:
                if currentNode.right==0:
                    currentNode.right = treenode
                    return
                else:
                    currentNode = currentNode.right
            elif currentNode.data>treenode.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()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值