非递归遍历二叉树

旧城冷巷雨未停 提交于 2020-02-08 23:06:54
#类名称:BinaryTreeNode
#类说明:定义一个二叉树的结点
#类释义:分别有左孩子LeftChild,右孩子RightChild和数据data
class BinaryTreeNode(object):
    def __init__(self):
        self.data='#'
        self.LeftChild=None
        self.RightChild=None

class TreeState(object):
    def __init__(self,BinaryTreeNode,VisitedFlag):
        self.BinaryTreeNode = BinaryTreeNode
        self.VisitedFlag = VisitedFlag

#类说明:定义一个二叉树
class BinaryTreeNonRecursive(BinaryTreeNode):
    #创建二叉树的函数
    def CreateBinaryTree(self,Root):
        data=input('->')
        if data=='#':
            Root=None
        else:
            Root.data=data
            Root.LeftChild=BinaryTreeNode()
            self.CreateBinaryTree(Root.LeftChild)
            Root.RightChild=BinaryTreeNode()
            self.CreateBinaryTree(Root.RightChild)

    #先序遍历非递归算法
    def PreOrderNonRecursive(self,Root):
        StackTreeNode=[]
        tTreeNode=Root
        while len(StackTreeNode)>0 or tTreeNode is not None:
            while tTreeNode is not None:
                self.VisitBinaryTreeNode(tTreeNode)
                StackTreeNode.append(tTreeNode)
                tTreeNode=tTreeNode.LeftChild
            if len(StackTreeNode)>0:
                tTreeNode=StackTreeNode.pop()
                tTreeNode=tTreeNode.RightChild

    #中序遍历非递归算法
    def InOrderNonRecursive(self, Root):
        StackTreeNode = []
        tTreeNode = Root
        while len(StackTreeNode)>0 or tTreeNode is not None:
            while tTreeNode is not None:
                StackTreeNode.append(tTreeNode)
                tTreeNode = tTreeNode.LeftChild
            if len(StackTreeNode)>0:
                tTreeNode = StackTreeNode.pop()
                self.VisitBinaryTreeNode(tTreeNode)
                tTreeNode = tTreeNode.RightChild    

    #后序遍历的非递归算法
    def PostOrderNonRecursive(self,Root):
        StackTreeNode = []
        tBinaryTreeNode = Root
        tTree = None
        while tBinaryTreeNode is not None:
            tTree = TreeState(tBinaryTreeNode,0)
            StackTreeNode.append(tTree)
            tBinaryTreeNode = tBinaryTreeNode.LeftChild
        while len(StackTreeNode)>0:
            tTree = StackTreeNode.pop()
            if tTree.BinaryTreeNode.RightChild is None or tTree.VisitedFlag == 1:
                self.VisitBinaryTreeNode(tTree.BinaryTreeNode)
            else:
                StackTreeNode.append(tTree)
                tTree.VisitedFlag = 1
                tBinaryTreeNode=tTree.BinaryTreeNode.RightChild
                while tBinaryTreeNode is not None:
                    tTree = TreeState(tBinaryTreeNode,0)
                    StackTreeNode.append(tTree)
                    tBinaryTreeNode = tBinaryTreeNode.LeftChild
    
    #遍历二叉树的一个结点函数
    def VisitBinaryTreeNode(self, BinaryTreeNode):
        #值为#的结点代表空结点
        if BinaryTreeNode.data is not '#':
            print (BinaryTreeNode.data)

#主程序 
btnrn = BinaryTreeNode()
btrn = BinaryTreeNonRecursive()
print ('创建一棵二叉树\n')
print ('         4')
print ('        / \\')
print ('       5   6')
print ('      / \\  \\')
print ('     1   2  7 ')
print ('4 5 1 # # 2 # # 6 # 7 # #')
#创建一棵二叉树
print('请仿照上述序列,输入某一二叉树中各结点的值(#表示空结点),每输入一个值按回车换行:')
btrn.CreateBinaryTree(btnrn)
#非递归遍历二叉树
print ('对二叉树进行非递归前序遍历:\n')
#前序遍历二叉树
btrn.PreOrderNonRecursive(btnrn)
print ('对二叉树进行非递归中序遍历:\n')
#中序遍历二叉树
btrn.InOrderNonRecursive(btnrn)
print ('对二叉树进行非递归后序遍历:\n')
#后序遍历二叉树
btrn.PostOrderNonRecursive(btnrn)  

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!