C # implements traversal of a two-fork tree

Source: Internet
Author: User

C # implements the pre-order, middle order, and post-sequence traversal of a two-fork tree.

public class binarytreenode    {         int value;        binarytreenode left;         binarytreenode right;        ///  <summary>        ///  Pre-order traversal          /// </summary>        /// < Param name= "Tree" ></param>        public static  void preorder (Binarytreenode tree)         {             if  (tree == null)                  return;         &Nbsp;   system.console.write (tree.value +  " ");             preorder (Tree.left);             preorder (tree.right);        }         /// <summary>        // /  Pre-sequence traversal loop implementation         /// </summary>         /// <param name= "Tree" ></param>         public static void preorderloop (Binarytreenode tree)          {             if  (tree == null)                  return;&nBsp;           stack<binarytreenode> stack  = new Stack<BinaryTreeNode> ();             BinaryTreeNode node = tree;             while (node != null | |  stack. Any ()) {                if (node  != null)                  {                     stack. Push (node);                     system.console.write (node.value +  " ");                     node = node.left;                 }                 else                 {                     var item = stack. Pop ();                     node = item.right;                 }            }         }        /// < summary>        ///  sequence Traversal         /// </summary>         /// <param name= "Tree" ></param>         public static void inorder (BinaryTreeNode tree)          {             if  (tree == null)                  return;            inorder (Tree.left);             system.console.write ( tree.value +  " ");             Inorder (tree.right);        }         /// <summary>        ///  sequence traversal loop implementation          /// </summary>        /// <param  Name= "Tree" ></param>        public static void  inorderloop (Binarytreenode tree)         {             if  (tree == null)                  return;             stack<binarytreenode> stack = new stack <BinaryTreeNode> ();             binarytreenode node = tree;             while  (Node != null | |  stack. Any ())             {                 if  (node != null)                  {                     stack. Push (node);                     node = node.left;                 }                 else                 {                 &Nbsp;   var item = stack. Pop ();                     system.console.write (item.value +  " ");                     node =  item.right;                }             }         }        /// <summary>         ///  Post-Traversal         /// </summary>         /// <param name= "Tree" ></param>         public static void postorder(Binarytreenode tree)         {             if  (tree == null)                  return;             postorder (Tree.left);             postorder (tree.right);             system.console.write (tree.value +  " ");         }         /// <summary>         ///  sequential traversal loop implementation 1        /// </summary>         /// <param name= "Tree" ></param>        &nBsp;public static void postorderloop (Binarytreenode tree)          {            if  (tree  == null)                  Return;            stack<binarytreenode>  stack = new Stack<BinaryTreeNode> ();             BinaryTreeNode node = tree;             BinaryTreeNode pre = null;             stack. Push (node);            while  (stack. Any ()) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBsp;            node = stack. Peek ();                 if   ((node.left == null && node.right == null)  | |                       (pre != null &&  (pre == node.left | |  pre == node.right))                  {                     system.console.write (node.value +  " ");                     pre  = node;         &Nbsp;          stack. Pop ();                }                 else                 {                     if  ( Node.right != null)                          stack. Push (node.right);                     if  (node.left != null)                          staCk. Push (Node.left);                 }            }         }        /// <summary>         ///  subsequent traversal loops implement 2        /// </ Summary>        /// <param name= "Tree" ></param &GT;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;VOID&NBSP;POSTORDERLOOP2 ( Binarytreenode tree)         {             hashset<binarytreenode> visited = new hashset <BinaryTreeNode> ();            stack< binarytreenode> stack&Nbsp;= new stack<binarytreenode> ();             BinaryTreeNode node = tree;             while  (node != null | |  stack. Any ())             {                 if  (node != null)                  {                     stack. Push (node);                     node = node.left;                 }                else                 {                     var item = stack. Peek ();                     if  (item.right != null && !visited. Contains (item.right))                      {                         node = item.right;                      }                    else                     {                          system.console.write (item.value +  " ");                          Visited. ADD (item);                         stack. Pop ();                     }                 }            }        }    } 


This article is from the "Xu Big Tree" blog, please be sure to keep this source http://abelxu.blog.51cto.com/9909959/1966963

C # implements traversal of a two-fork tree

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.