二叉树的介绍及python实现其遍历

文章目录

    • 1.二叉树的概念
    • 2.二叉树的遍历
    • 3.python实现二叉树的遍历
      • (1)递归的形式
      • (2)非递归的形式

1.二叉树的概念

在计算机科学中,二叉树是每个结点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。
一棵深度为k,且有2^k-1个结点的二叉树,称为满二叉树。这种树的特点是每一层上的结点数都是最大结点数。而在一棵二叉树中,除最后一层外,若其余层都是满的,并且或者最后一层是满的,或者是在右边缺少连续若干结点,则此二叉树为完全二叉树。具有n个结点的完全二叉树的深度为floor(log2n)+1。深度为k的完全二叉树,至少有2k-1个叶子结点,至多有2k-1个结点。

2.二叉树的遍历

假设有这样一棵二叉树,如下图所示。
二叉树的介绍及python实现其遍历_第1张图片

①先序遍历:根节点——>左叶子节点——>右叶子节点
②中序遍历:左叶子节点——>根节点——>右叶子节点
③后序遍历:左叶子节点——>右叶子节点——>根节点

于是,可以得到先序、中序、后序遍历的顺序,如下图所示。
二叉树的介绍及python实现其遍历_第2张图片

3.python实现二叉树的遍历

(1)递归的形式

主要代码:

#(1)递归的形式
#①前序遍历
def preOrderRecusive(root):
    if root == None:
        return None
    print (root.val)
    preOrderRecusive(root.left)
    preOrderRecusive(root.right)
#②中序遍历
def midOrderRecusive(root):
    if root == None:
        return None
    midOrderRecusive(root.left)
    print (root.val)        
    midOrderRecusive(root.right)
#③后序遍历
def latOrderRecusive(root):
    if root == None:
        return None
    latOrderRecusive(root.left)
    latOrderRecusive(root.right)
    print (root.val)

完整代码:

class TreeNode(object):
    def __init__(self,x):
        self.val = x
        self.left = None
        self.right = None
#(1)递归的形式
#①前序遍历
def preOrderRecusive(root):
    if root == None:
        return None
    print (root.val,end=" ")
    preOrderRecusive(root.left)
    preOrderRecusive(root.right)
#②中序遍历
def midOrderRecusive(root):
    if root == None:
        return None
    midOrderRecusive(root.left)
    print (root.val,end=" ")        
    midOrderRecusive(root.right)
#③后序遍历
def latOrderRecusive(root):
    if root == None:
        return None
    latOrderRecusive(root.left)
    latOrderRecusive(root.right)
    print (root.val,end=" ")

if __name__ == "__main__":
    t1 = TreeNode(1)
    t2 = TreeNode(2)
    t3 = TreeNode(3)
    t4 = TreeNode(4)
    t5 = TreeNode(5)
    t6 = TreeNode(6)
    t7 = TreeNode(7)
    t8 = TreeNode(8)

    t1.left = t2
    t1.right = t3
    t2.left = t4
    t2.right = t5
    t3.left = t6
    t3.right = t7
    t6.right = t8
    print("前序遍历:")
    preOrderRecusive(t1)
    print( "\n中序遍历:")
    midOrderRecusive(t1)
    print("\n后序遍历:")
    latOrderRecusive(t1)

运行结果:
二叉树的介绍及python实现其遍历_第3张图片

(2)非递归的形式

遍历的过程都是相同的(如下图所示)。
用栈的方法实现,前序和中序只是打印的位置不同,后序则增加判断何时pop根节点
二叉树的介绍及python实现其遍历_第4张图片

主要代码:

#(2)非递归的形式
 #①前序遍历
def preOrder(root):
    if root == None:
        return None
    stack = []
    tmpNode = root
    while tmpNode or stack:
        while tmpNode:
            print (tmpNode.val,end=" ")
            stack.append(tmpNode)
            tmpNode = tmpNode.left
        node = stack.pop()
        tmpNode = node.right
#②中序遍历
def midOrder(root):
    if root == None:
        return None
    stack = []
    tmpNode = root
    while tmpNode or stack:
        while tmpNode:
            #print (tmpNode.val,end=" ")
            stack.append(tmpNode)
            tmpNode = tmpNode.left
        node = stack.pop()
        print (node.val,end=" ")
        tmpNode = node.right
#③后序遍历
def latOrder(root):
    if root == None:
        return None
    stack = []
    tmpNode = root
    while tmpNode or stack:
        while tmpNode:
            stack.append(tmpNode)
            tmpNode = tmpNode.left
        node = stack[-1]
        tmpNode = node.right
        if node.right == None:
            node = stack.pop()
            print (node.val,end=" ")
            while stack and node == stack[-1].right:
                node = stack.pop()
                print (node.val,end=" ")

完整代码:

class TreeNode(object):
    def __init__(self,x):
        self.val = x
        self.left = None
        self.right = None
'''
#(1)递归的形式
#①前序遍历
def preOrderRecusive(root):
    if root == None:
        return None
    print (root.val,end=" ")
    preOrderRecusive(root.left)
    preOrderRecusive(root.right)
#②中序遍历
def midOrderRecusive(root):
    if root == None:
        return None
    midOrderRecusive(root.left)
    print (root.val,end=" ")        
    midOrderRecusive(root.right)
#③后序遍历
def latOrderRecusive(root):
    if root == None:
        return None
    latOrderRecusive(root.left)
    latOrderRecusive(root.right)
    print (root.val,end=" ")
'''
 #(2)非递归的形式
 #(遍历的过程都是相同的,前序和中序只是打印的位置不同,后序则增加判断何时pop根节点)
 #①前序遍历
def preOrder(root):
    if root == None:
        return None
    stack = []
    tmpNode = root
    while tmpNode or stack:
        while tmpNode:
            print (tmpNode.val,end=" ")
            stack.append(tmpNode)
            tmpNode = tmpNode.left
        node = stack.pop()
        tmpNode = node.right
#②中序遍历
def midOrder(root):
    if root == None:
        return None
    stack = []
    tmpNode = root
    while tmpNode or stack:
        while tmpNode:
            #print (tmpNode.val,end=" ")
            stack.append(tmpNode)
            tmpNode = tmpNode.left
        node = stack.pop()
        print (node.val,end=" ")
        tmpNode = node.right
#③后序遍历
def latOrder(root):
    if root == None:
        return None
    stack = []
    tmpNode = root
    while tmpNode or stack:
        while tmpNode:
            stack.append(tmpNode)
            tmpNode = tmpNode.left
        node = stack[-1]
        tmpNode = node.right
        if node.right == None:
            node = stack.pop()
            print (node.val,end=" ")
            while stack and node == stack[-1].right:
                node = stack.pop()
                print (node.val,end=" ")
if __name__ == "__main__":
    t1 = TreeNode(1)
    t2 = TreeNode(2)
    t3 = TreeNode(3)
    t4 = TreeNode(4)
    t5 = TreeNode(5)
    t6 = TreeNode(6)
    t7 = TreeNode(7)
    t8 = TreeNode(8)

    t1.left = t2
    t1.right = t3
    t2.left = t4
    t2.right = t5
    t3.left = t6
    t3.right = t7
    t6.right = t8
    '''
    #(递归形式)
    print("前序遍历:")
    preOrderRecusive(t1)
    print( "\n中序遍历:")
    midOrderRecusive(t1)
    print("\n后序遍历:")
    latOrderRecusive(t1)
    '''
    #(非递归形式)
    print("前序遍历:")
    preOrder(t1)
    print( "\n中序遍历:")
    midOrder(t1)
    print("\n后序遍历:")
    latOrder(t1)


结果如下:
二叉树的介绍及python实现其遍历_第5张图片
参考:
剑指offer-数据结构与算法视频

你可能感兴趣的:(数据结构,python,剑指offer,二叉树,数据结构,算法,python)