二叉树的递归遍历+非递归遍历,Swift实现

定义二叉树模型

class Tree {
    var value = 0
    var left: Tree?
    var right: Tree?
    
    var isVist = false
}

创建二叉树:

let tree = createTree(0, 6)!

func createTree(_ index: Int, _ total: Int ) -> Tree? {
    if index >= total {
        return nil
    }
    let node = Tree.init()
    node.value = index + 1
    node.left = createTree(2 * index + 1, total)
    node.right = createTree(2 * index + 2, total)
    return node
}

创建的二叉树如下:


二叉树

这个二叉树的遍历分别为:

  • 先序遍历: 124536
  • 中序遍历:425163
  • 后序遍历:452631

递归实现

前序遍历:

func firstRoot(_ tree: Tree?) {
    guard let t = tree else {
        return
    }
    print(t.value, terminator: "")
    firstRoot(t.left)
    firstRoot(t.right)
}

中序遍历:

func midRoot(_ tree: Tree?) {
    guard let t = tree else {
        return
    }
    midRoot(t.left)
    print(t.value, terminator: "")
    midRoot(t.right)
}

后序遍历:

func lastRoot(_ tree: Tree?) {
    guard let t = tree else {
        return
    }
    lastRoot(t.left)
    lastRoot(t.right)
    print(t.value, terminator: "")
}

非递归遍历实现

首先,写一个栈:

struct Stack {
    var items = [T]()
    mutating func push(_ item: T) {
        items.append(item)
    }
    
    mutating func pop() -> T? {
        return items.popLast()
    }
    
    var isEmpty: Bool {
        items.isEmpty
    }
}

前序遍历:

func normalFirstRoot(_ tree: Tree?) {
    var tree = tree
    var stack = Stack.init()
    while tree != nil || !stack.isEmpty  {
        if tree != nil {
            print(tree!.value, terminator: "")
            stack.push(tree!)
            tree = tree?.left
        } else {
            tree = stack.pop()
            tree = tree?.right
        }
    }
}

中序遍历:

func normalMidRoot(_ tree: Tree?) {
    var tree = tree
    var stack = Stack.init()
    while tree != nil || !stack.isEmpty {
        if tree != nil {
            stack.push(tree!)
            tree = tree?.left
        } else {
            tree = stack.pop()
            if tree != nil {
                print(tree!.value, terminator: "")
            }
            tree = tree?.right
        }
    }
}

后序遍历:
后序遍历的非递归实现算是相对较难的一种遍历了,需要用到节点的isVist属性,实现如下:

func normalLastRoot(_ tree: Tree?) {
    var tree = tree
    var stack = Stack.init()
    while tree != nil || !stack.isEmpty {
        if tree != nil {
            if tree!.left?.isVist == true, tree?.right?.isVist == true {
                if tree != nil {
                    print(tree!.value, terminator: "")
                }
                tree = stack.pop()
            } else if tree?.left?.isVist == true {
                stack.push(tree!)
                tree?.isVist = true
                tree = tree?.right
            } else {
                stack.push(tree!)
                tree?.isVist = true
                tree = tree?.left
            }
        } else {
            tree = stack.pop()
            if tree != nil {
                print(tree!.value, terminator: "")
            }
            tree = stack.pop()
        }
    }
}

你可能感兴趣的:(二叉树的递归遍历+非递归遍历,Swift实现)