Golang two fork Tree pre-order, middle sequence, post-order non-recursive traversal algorithm

Source: Internet
Author: User

package mainimport  (    "container/list"     "FMT")// binary  Treetype binarytree struct {   data  interface{}   left   *binarytree   right *binarytree}// constructorfunc newbinarytree ( data interface{})  *binarytree {   return &binarytree{data: data}}/ /  First Order traversal-non-recursive func  (Bt *binarytree)  preordernorecursion ()  []interface{} {    t := bt   stack := list. New ()    res := make ([]interface{}, 0)    for t !=  nil | |  stack. Len ()  != 0 {      for t != nil {          res = append (Res, t.data)//visit          stack. Pushback (t)          t = t.Left       }      if stack. Len ()  != 0 {         v := stack. Back ()          t = v.value. (*binarytree)          t = t.Right          stack. Remove (v)       }   }   return res}//  Middle sequence traversal-non-recursive func  (Bt *binarytree)  inordernorecursion ()  []interface{} {   t  := bt   stack := list. New ()    res := make ([]interface{}, 0)    for t !=  nil | |  stack. Len ()  != 0 {      for t != nIl {         stack. Pushback (t)          t = t.Left       }      if stack. Len ()  != 0 {         v := stack. Back ()          t = v.value. (*binarytree)          res = append (res, t.Data)// visit         t = t.right          stack. Remove (v)       }   }   return res}//  Post-post traversal-non-recursive func  (Bt *binarytree)  postordernorecursion ()  []interface{} {    t := bt   stack := list. New ()    res := make ([]interface{}, 0)    var previsited *binarytree   for t !=  nil | |  stack. Len ()  != 0 {      for t != nil {          stack. Pushback (t)          t = t.Left       }      v   := stack. Back ()       top := v.value. (*binarytree)       if  (top. Left == nil && top. Right == nil)  | |   (top. Right == nil && previsited == top. left)  | |  previsited == top. Right{         res = append (res, top. Data)//visit         previsited  = top         stack. Remove (v)       }else {          t = top. Right      }   }   return res}func main ( )  {   t := newbinarytree (1)    t.Left  =  Newbinarytree (3)    t.right = newbinarytree (6)    t.left.left =  newbinarytree (4)    t.left.right = newbinarytree (5)    t. Left.left.left = newbinarytree (7)    fmt. Println (T.preordernorecursion ())    fmt. Println (T.inordernorecursion ())    fmt. Println (T.postordernorecursion ())}


Golang two fork Tree pre-order, middle sequence, post-order non-recursive traversal algorithm

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.