golang實現樹遍曆

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package mainimport (    "container/list"    "fmt"    "strings")type MyStack struct {    List *list.List}type MyQueue struct {    List *list.List}type BinaryTree struct {    Value interface{}    Left *BinaryTree    Right *BinaryTree}type Tree struct {    Value interface{}    Children []*Tree}func (stack *MyStack) pop() interface{} {    if elem := stack.List.Back(); elem != nil {        stack.List.Remove(elem)        return elem.Value    }    return nil}func (stack *MyStack) push(elem interface{}) {    stack.List.PushBack(elem)}func (queue *MyQueue) pop() interface{} {    if elem := queue.List.Front(); elem != nil {        queue.List.Remove(elem)        return elem.Value    }    return nil}func (queue *MyQueue) push(elem interface{}) {    queue.List.PushBack(elem)}func preOrderRecur(node *BinaryTree) {    if node == nil {        return    }    fmt.Println(node.Value)    preOrderRecur(node.Left)    preOrderRecur(node.Right)}func inOrderRecu(node *BinaryTree) {    if node == nil {        return    }    inOrderRecu(node.Left)    fmt.Println(node.Value)    inOrderRecu(node.Right)}func posOrderRecu(node *BinaryTree) {    if node == nil {        return    }    posOrderRecu(node.Left)    posOrderRecu(node.Right)    fmt.Println(node.Value)}func preOrder(node *BinaryTree) {    stack := MyStack{List: list.New()}    stack.push(node)    elem := stack.pop()    for elem != nil {        node, _ := elem.(*BinaryTree)        fmt.Println(node.Value)        if right := node.Right; right != nil {            stack.push(right)        }        if left := node.Left; left != nil {            stack.push(left)        }        elem = stack.pop()    }}func inOrder(node *BinaryTree) {    stack := MyStack{List: list.New()}    current := node    for stack.List.Len() > 0 || current != nil {        if current != nil {            stack.push(current)            current = current.Left        } else {            current = stack.pop().(*BinaryTree)            fmt.Println(current.Value)            current = current.Right        }    }}func postOrder(node *BinaryTree) {    stack1, stack2 := MyStack{List: list.New()}, MyStack{List: list.New()}    stack1.push(node)    for stack1.List.Len() > 0 {        elem := stack1.pop().(*BinaryTree)        stack2.push(elem)        if elem.Left != nil {            stack1.push(elem.Left)        }        if elem.Right != nil {            stack1.push(elem.Right)        }    }    for stack2.List.Len() > 0 {        elem := stack2.pop().(*BinaryTree)        fmt.Println(elem.Value)    }}func levelOrder(node *BinaryTree) {    var nlast *BinaryTree    last := node    level := 1    queue := MyQueue{List: list.New()}    fmt.Println(fmt.Sprintf("-----this is %d level-----", level))    queue.push(node)    for queue.List.Len() > 0 {        node := queue.pop().(*BinaryTree)        if node.Left != nil {            queue.push(node.Left)            nlast = node.Left        }        if node.Right != nil {            queue.push(node.Right)            nlast = node.Right        }        fmt.Println(node.Value)        if last == node && (node.Left != nil || node.Right != nil) {            last = nlast            level++            fmt.Println()            fmt.Println(fmt.Sprintf("-----this is %d level-----", level))        }    }}func levelTreeOrder(node *Tree) {    var nlast *Tree    last := node    queue := MyQueue{List: list.New()}    queue.push(node)    for queue.List.Len() > 0 {        node := queue.pop().(*Tree)        for _, elem := range node.Children {            queue.push(elem)            nlast = elem        }        fmt.Println(node.Value)        if last == node {            last = nlast            fmt.Println()        }    }}func preOrderToStr(node *BinaryTree) (ret string) {    if node == nil {        return "#!"    }    ret += fmt.Sprintf("%d!", node.Value)    ret += preOrderToStr(node.Left)    ret += preOrderToStr(node.Right)    return ret}func strToBinaryTree(arr []string, index *int) *BinaryTree {    if *index >= len(arr) {        return nil    }    if arr[*index] == "#" {        *index++        return nil    }    node := &BinaryTree{}    node.Value = arr[*index]    *index++    node.Left = strToBinaryTree(arr, index)    node.Right = strToBinaryTree(arr, index)    return node}func main() {    str := "1!2!4!#!#!5!#!#!3!6!#!#!7!#!#!"    arr := strings.Split(str, "!")    index := 0    node := strToBinaryTree(arr, &index)    preOrder(node)    // -----------多叉樹層次遍曆(列印行號)-----------------    // node11 := &Tree{Value: 10}    // node10 := &Tree{Value: 10}    // node9 := &Tree{Value: 9}    // node8 := &Tree{Value: 8}    // node7 := &Tree{Value: 7}    // node6 := &Tree{Value: 6}    // node5 := &Tree{Value: 5, Children: []*Tree{node8, node9, node10, node11}}    // node4 := &Tree{Value: 4}    // node3 := &Tree{Value: 3}    // node2 := &Tree{Value: 2, Children: []*Tree{node5, node6, node7}}    // root := &Tree{Value: 1, Children: []*Tree{node2, node3, node4}}    // levelTreeOrder(root)    // -----------二叉樹-----------------    // node7 := &BinaryTree{Value: 7}    // node6 := &BinaryTree{Value: 6}    // node5 := &BinaryTree{Value: 5}    // node4 := &BinaryTree{Value: 4}    // node3 := &BinaryTree{Value: 3, Left: node6, Right: node7}    // node2 := &BinaryTree{Value: 2, Left: node4, Right: node5}    // root := &BinaryTree{Value: 1, Left: node2, Right: node3}    // fmt.Println(preOrderToStr(root))    // ---------二叉樹前序走訪(遞迴、非遞迴)    // preOrderRecur(root)    // fmt.Println()    // preOrder(root)    // ---------二叉樹中序遍曆(遞迴、非遞迴)    // inOrderRecu(root)    // fmt.Println()    // inOrder(root)    // ---------二叉樹後序遍曆(遞迴、非遞迴)    // posOrderRecu(root)    // fmt.Println()    // postOrder(root)    // ---------二叉樹層次遍曆(列印行號)    // levelOrder(root)    // queue := MyQueue{List: list.New()}    // queue.push(1)    // queue.push(2)    // queue.push(3)    // fmt.Println(queue.pop())    // fmt.Println(queue.pop())    // fmt.Println(queue.pop())    // fmt.Println(queue.pop())    // fmt.Println(root)    // stack := MyStack{List: list.New()}    // stack.push(1)    // stack.push(2)    // stack.push(3)    // fmt.Println(stack.pop())    // fmt.Println(stack.pop())    // fmt.Println(stack.pop())    // fmt.Println(stack.pop())}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.