利用Python描述二叉树并且实现遍历

本文深入探讨了二叉树的基本概念,包括其定义、性质和常见的遍历方式,如先序、中序、后序和层次遍历,并通过Python代码实现了一个简单的二叉树,展示了不同遍历方法的具体应用。

       二叉树是n(n>=0)个结点的有限集合。当n=0时候,称为空二叉树;当n>0时,该集合由一个根结点及两棵互不相交的,被分别称为左子树和右子树的二叉树组成。

       以前面定义的树为基础,二叉树可以理解为是满足以下两个条件的树形结构。

       (1)每个结点的度不大于2。

       (2)结点每棵子树的位置是明确区分左右的,不能随意改变。

       由上述定义可以看出:二叉树中每个结点只能有0,1,2个孩子,而且孩子有左右之分,即使仅有一个孩子,也必须区分左右。位于左边的孩子(或子树)叫左孩子,位于右边的孩子(或子树)叫右孩子(右子树)。

       二叉树的遍历有:先序遍历,中序遍历,后序遍历和层次遍历。

class Node(object):
    def __init__(self, item):
        self.item = item
        self.child1 = None
        self.child2 = None


class Tree(object):
    def __init__(self):
        self.root = None

    def add(self, item):
        node = Node(item)
        if self.root is None:
            self.root = node
        else:
            q = [self.root]

            while True:
                pop_node = q.pop(0)
                if pop_node.child1 is None:
                    pop_node.child1 = node
                    return
                elif pop_node.child2 is None:
                    pop_node.child2 = node
                    return
                else:
                    q.append(pop_node.child1)
                    q.append(pop_node.child2)

    def traverse(self):  # 层次遍历
        if self.root is None:
            return None
        q = [self.root]
        res = [self.root.item]
        while q != []:
            pop_node = q.pop(0)
            if pop_node.child1 is not None:
                q.append(pop_node.child1)
                res.append(pop_node.child1.item)

            if pop_node.child2 is not None:
                q.append(pop_node.child2)
                res.append(pop_node.child2.item)
        return res

    def preorder(self, root):  # 先序遍历
        if root is None:
            return []
        result = [root.item]
        left_item = self.preorder(root.child1)
        right_item = self.preorder(root.child2)
        return result + left_item + right_item

    def inorder(self, root):  # 中序序遍历
        if root is None:
            return []
        result = [root.item]
        left_item = self.inorder(root.child1)
        right_item = self.inorder(root.child2)
        return left_item + result + right_item

    def postorder(self, root):  # 后序遍历
        if root is None:
            return []
        result = [root.item]
        left_item = self.postorder(root.child1)
        right_item = self.postorder(root.child2)
        return left_item + right_item + result


t = Tree()
for i in range(10):
    t.add(i)
print('层序遍历:', t.traverse())
print('先序遍历:', t.preorder(t.root))
print('中序遍历:', t.inorder(t.root))
print('后序遍历:', t.postorder(t.root))

结果:

层序遍历: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
先序遍历: [0, 1, 3, 7, 8, 4, 9, 2, 5, 6]
中序遍历: [7, 3, 8, 1, 9, 4, 0, 5, 2, 6]
后序遍历: [7, 8, 3, 9, 4, 1, 5, 6, 2, 0]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值