Golang Implementing Tree Traversal

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
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 in terface{} 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) {Stac K: = 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 was%d level-----", level)) the 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 was%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: = &binary tree{} 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)//-----------Multi-fork Tree hierarchy traversal (print line number)-----------------  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)//-----------binary tree-----------------//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 (root)//---------binary tree pre-sequence traversal (recursive, non-recursive)//Preorderrecur (root)//FMT. PRINTLN ()///preorder (root)//---------binary tree in sequence traversal (recursive, non-recursive)//INORDERRECU (root)//FMT. PRINTLN ()///Inorder (root)//---------binary Tree post-traversal (recursive, non-recursive)//POSORDERRECU (root)/FMT. PRINTLN ()///Postorder (root)//---------binary tree hierarchy traversal (print line number)//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 ())}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.