python二叉树的深度优先遍历(包括前序,中序,后序)和广度优先遍历(完全二叉树)

85 篇文章 0 订阅
'''
python编程实现二叉树
https://www.bilibili.com/video/av17888939?from=search&seid=5546073725160462288
这个up主讲的很好
'''
'''
在python中实现二叉树的数据类型,
二叉树实际上就是链表的扩充(链表是线性的数据结构,而二叉树不是)
单向链表中每个节点具有唯一的next后继,而对于二叉树中的每个节点
都包含其左孩子节点和右孩子节点。链表和树的基本单元都是节点
所有的数据结构都是数据的组成形式,就是把一个集合中的数据集中到一起的结构
故而数据结构中的重要功能就是保存数据
'''
class Node(object):#构造节点
    def __init__(self,item):
        self.val=item
        self.left=None
        self.right=None#左子节点和右子节点
class Tree(object):#构造二叉树
    def __init__(self):#def __init__(self) 称为类的构造函数
        #对于链表,其构造函数中只需要保存首节点
        #对于二叉树,其构造函数中只需要保存根节点
        self.root=None
    def add(self,item):
        '''
        向树中添加一个元素,应该是按照完全二叉树的顺序添加
        对二叉树进行层序遍历/广度优先遍历(横向)
        二叉树的层序遍历的python实现:使用队列的数据结构
        :param item:
        :return:
        '''
        newNode=Node(item)
        if self.root is None:
            self.root=newNode
            return
        from collections import deque
        dq=deque([])
        dq.append(self.root)#将根节点添加进来

        #while(1):#加入到队列中的元素是二叉树中的一个节点,并不是节点中的数值
        while dq:#判断使用的是逻辑值
            curr = dq.popleft()
            #curr=dq.pop(0),可以只使用list结构
            if curr.left is not None:
                dq.append(curr.left)
            else:
                curr.left=newNode
                break
            if curr.right is not None:
                dq.append(curr.right)
            else:
                curr.right=newNode
                break
        return
    def breadth_travel(self):
        '''
        对二叉树进行广度优先遍历/层次遍历
        :return:
        '''
        queue=[]
        value=[]
        if self.root is None:
            return
        queue.append(self.root)
        while queue:
            curr=queue.pop(0)
            value.append(curr.val)
            if curr.left is not None:
                queue.append(curr.left)
            if curr.right is not None:
                queue.append(curr.right)
        print(value,end=' ')
    '''
    二叉树的深度优先遍历:
    先序遍历:根->对左子树先序遍历->对右子树先序遍历
             先访问根节点,再对左子树进行先序遍历(递归),最后对右子树进行先序遍历(递归)
    中序遍历:对左子树中序遍历->根->对右子树中序遍历
    后序遍历:对左子树后序遍历->对右子树后序遍历->根
    '''
    #使用递归方式实现二叉树的深度优先遍历
    def pre_order(self,root):#由于需要递归调用本函数,故而必须要有一个形参
        if root is None:
            return
        print(root.val,end=' ')
        if root.left is not None:
            self.pre_order(root.left)#对左子树进行先序遍历
        if root.right is not None:
            self.pre_order(root.right)

    def mid_order(self,root):
        if root is None:
            return
        if root.left is not None:
            self.mid_order(root.left)
        print(root.val,end=' ')
        if root.right is not None:
            self.mid_order(root.right)
    def post_order(self,root):
        if root is None:
            return
        if root.left is not None:
            self.post_order(root.left)
        if root.right is not None:
            self.post_order(root.right)
        print(root.val,end=' ')
if __name__=='__main__':
    tree=Tree()#构造一棵空的二叉树
    for i in range(10):
        tree.add(i)
    tree.breadth_travel()#[1, 2, 3, 4, 5]
    print('\n')
    tree.pre_order(tree.root)
    print('\n')
    tree.mid_order(tree.root)
    print('\n')
    tree.post_order(tree.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、付费专栏及课程。

余额充值