二叉树问题---二叉树的前中后序的递归,非递归及Morris遍历

来源:互联网 发布:网络直播间 编辑:程序博客网 时间:2024/06/07 23:09
class TreeNode:    def __init__(self, x):        root.val = x        root.left = None        root.right = None

创建二叉树

def create_tree(root):    element = input("Enter a key: ")    if element == '#':        root = None    else:        root = TreeNode(element)        root.left = create_tree(root.left)        root.right = create_tree(root.right)    return root

前序遍历

def pre_order(root):    if root:        print(root.val,end=' ')        pre_order(root.left)        pre_order(root.right)

中序遍历

def mid_order(root):    if root:        mid_order(root.left)        print(root.val, end=' ')        mid_order(root.right)

后序遍历

def post_order(root):    if root:        post_order(root.left)        post_order(root.right)        print(root.val, end=' ')

非递归前序遍历

def preorder(root):    if not root:        return    stack = []    while root or len(stack):        if root:            stack.append(root)            print(root.val, end=' ')            root = root.left        else:            root = stack.pop()            root = root.right

非递归中序遍历

def midorder(root):    if not root:        return    stack = []    while root or stack:        if root:            stack.append(root)            root = root.left        else:            root = stack.pop()            print(root.val, end=' ')            root = root.right

非递归后序遍历

def postorder(root):    if not root:        return    stack1 = []    stack2 = []    while root or stack1:        if root:            stack1.append(root)            stack2.append(root.val)            root = root.right        else:            root = stack1.pop()            root = root.left    while stack2:        print(stack2.pop(), end=' ')

非递归后序遍历(使用一个栈)

说明:使用一个变量c来记录每次弹出的元素,保证元素不会重复进栈

def postOrder(root):    if not root:        return    stack = []    stack.append(root)    c = None    while stack:        c = stack[-1]        if c.left and c.left != root and c.right != root:            stack.append(c.left)        elif c.right and c.right != root:            stack.append(c.right)        else:            print(stack.pop().val, end=' ')            root = c

Morris前序遍历

def morrisPre(root):    if not root:        return    while root:        cur = root.left        if cur:            while cur.right and cur.right != root:                cur = cur.right            if not cur.right:                cur.right = root                print(root.val, end=' ')                root = root.left                continue            else:                cur.right = None        else:            print(root.val, end=' ')        root = root.right

Morris中序遍历

def morrisIn(root):    if not root:        return    while root:        cur = root.left        if cur:            while cur.right and cur.right != root:                cur = cur.right            if not cur.right:                cur.right = root                root = root.left                continue            else:                cur.right = None        print(root.val, end=' ')        root = root.right

Morris后序遍历

def morrisPost(root):    def printRightEdge(root):        tail = reverseEdge(root)        cur = tail        while cur:            print(cur.val, end=' ')            cur = cur.right        reverseEdge(tail)    def reverseEdge(root):        pre = None        while root:            next = root.right            root.right = pre            pre = root            root = next        return pre    if not root:        return    head = root    while root:        cur = root.left        if cur:            while cur.right and cur.right != root:                cur = cur.right            if not cur.right:                cur.right = root                root = root.left                continue            else:                cur.right = None                printRightEdge(root.left)        root = root.right    printRightEdge(head)

层次遍历

def levelorder(root):    if not root:        return    queue = []    queue.append(root)    while queue:        root = queue.pop(0)        print(root.val, end=' ')        if root.left:            queue.append(root.left)        if root.right:            queue.append(root.right)
原创粉丝点击