二叉树的Python实现

树是一种非线性结构,二叉树是指每个节点最多有两个子节点的二叉树。Python实现二叉树可以有两种方法,基于列表的方法和基于链表的方法。

1.列表实现

嵌套列表可实现二叉树,应用递归的思路。采用列表[root,left,right]表示根节点和左右子节点,该方法易扩展到多叉树(增加列表元素即可)。

#1.列表实现二叉树
def BinaryTree(r):
    return [r, [], []]
def InsertLeft(root, newBranch):
    t = root.pop(1)
    if len(t) > 1:
        root.insert(1, [newBranch, t, []])
    else:
        root.insert(1, [newBranch, [], []])
    return root

def InsertRigth(root, newBranch):
    r = root.pop(2)
    if len(r) > 1:
        root.insert(2, [newBranch, [], r])
    else:
        root.insert(2, [newBranch, [], []])
    return root
def getRootVal(root):
    return root[0]
def setRootVal(root, newVal):
    root[0] = newVal
def getLeftChild(root):
    return root[1]
def getRightChild(root):
    return root[2]


# r = BinaryTree(3)
# InsertLeft(r, 4)
# InsertLeft(r, 5)
# InsertRigth(r, 6)
# InsertRigth(r, 7)
# l = getLeftChild(r)
# ri = getRightChild(r)
# print(l, ri)
# setRootVal(l, 9)
# print(r)
# InsertLeft(l, 11)
# print(r)
# print(getRightChild(getRightChild(r)))

2.链表实现

每个链表节点保存根节点的数据项以及指向左右子树的连接。

class BinaryTree:
    def __init__(self,rootObj):
        self.root=rootObj
        self.leftChild=None#指向左子树的引用(BT对象)
        self.rightChild=None

    #插入左子树
    def insertLeft(self,newNode):
        if self.leftChild==None:
            self.leftChild=BinaryTree(newNode)
        else:
            t=BinaryTree(newNode)
            t.leftChild=self.leftChild
            self.leftChild=t
    #插入右子树
    def insertRight(self,newNode):
        if self.rightChild==None:
            self.rightChild=BinaryTree(newNode)
        else:
            t=BinaryTree(newNode)
            t.rightChild=self.rightChild#顺序不能颠倒
            self.rightChild=t
    #取得右子树
    def getRightChild(self):
        return self.rightChild
    #取得左子树
    def getLeftChild(self):
        return self.leftChild
    #设置根节点
    def setRootVal(self,obj):
        self.root=obj
    #取得根节点
    def getRootVal(self):
        return self.root
    #先序遍历
    def preorder(self):
        print(self.root)
        #if self.getLeftChild(): 不需要get,在类里面可直接访问数据成员。getLeftChild()也对
        if self.leftChild:
            self.leftChild.preorder()
        if self.rightChild:
            self.rightChild.preorder()

    #中序遍历
    def inorder(self):
        if self.leftChild:
            self.leftChild.inorder()
        print(self.root)
        if self.rightChild:
            self.rightChild.inorder()

    #后序遍历
    def postorder(self):
        if self.leftChild:
            self.rightChild.postorder()
        if self.rightChild:
            self.rightChild.postorder()
        print(self.root)
# r=binaryTree('a')
# r.insertLeft('b')
# r.insertRight('c')
# print(r.getRightChild())
# r.getRightChild().setRootVal('hello')
# r.getLeftChild().insertRight('d')

代码中还实现了树的遍历。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值