C # implement sequential traversal of binary tree out-of-band

Source: Internet
Author: User
  • Using system;
  • Namespace binarytree
  • {
  • // Binary Tree node class
  • Class Node
  • {
  • Public int data {Get; set ;}
  • Public node leftsubnode {Get; set ;}
  • Public node rightsubnode {Get; set ;}
  • // Add a child node to the node itself (and add a child node to the left/right to form recursion)
  • Public void append (node subnode)
  • {
  • If (subnode. Data <= This. Data)
  • {
  • This. appendleft (subnode );
  • }
  • Else
  • {
  • This. appendright (subnode );
  • }
  • }
  • // Append to left
  • Public void appendleft (node subnode)
  • {
  • If (this. leftsubnode = NULL)
  • {
  • This. leftsubnode = subnode;
  • }
  • Else
  • {
  • This. leftsubnode. append (subnode );
  • }
  • }
  • // Append to the right
  • Public void appendright (node subnode)
  • {
  • If (this. rightsubnode = NULL)
  • {
  • This. rightsubnode = subnode;
  • }
  • Else
  • {
  • This. rightsubnode. append (subnode );
  • }
  • }
  • // Display your data at the node
  • Public void showdata ()
  • {
  • Console. writeline ("Data = {0}", this. data );
  • }
  • }
  • // Binarytree class
  • Class tree
  • {
  • // Root Node
  • Public node root {Get; set ;}
  • // Start with the root node and insert the node
  • Public void insert (node newnode)
  • {
  • If (this. Root = NULL)
  • {
  • This. Root = newnode;
  • }
  • Else
  • {
  • This. Root. append (newnode );
  • }
  • }
  • // Overload. The traversal starts from the root node by default.
  • Public void midtravel ()
  • {
  • This. midtravel (this. Root );
  • }
  • // Recursive Traversal)
  • Public void midtravel (node)
  • {
  • If (node. leftsubnode! = NULL)
  • {
  • This. midtravel (node. leftsubnode );
  • }
  • Node. showdata ();
  • If (node. rightsubnode! = NULL)
  • {
  • This. midtravel (node. rightsubnode );
  • }
  • }
  • }
  • Class Program
  • {
  • Static void main (string [] ARGs)
  • {
  • TREE tree = new tree ();
  • Tree. insert (new node {DATA = 3 });
  • Tree. insert (new node {DATA = 6 });
  • Tree. insert (new node {DATA = 2 });
  • Tree. insert (new node {DATA = 7 });
  • Tree. insert (new node {DATA = 18 });
  • Tree. midtravel ();
  • }
  • }
  • }
  • Related Article

    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.