Miscellaneous-C # Binary Tree, out-of-band sequential Traversal

Source: Internet
Author: User

Miscellaneous-C # Binary Tree, out-of-band sequential Traversal

 

When we find that the data structure is implemented using the C # syntax, the code looks neat.

 

  1. Using system;
  2. Namespace binarytree
  3. {
  4. // Binary Tree node class
  5. Class Node
  6. {
  7. Public int data {Get; set ;}
  8. Public node leftsubnode {Get; set ;}
  9. Public node rightsubnode {Get; set ;}
  10. // Add a child node to the node itself (combined with appending to the left/right to form recursion)
  11. Public void append (node subnode)
  12. {
  13. If (subnode. Data <= This. Data)
  14. {
  15. This. appendleft (subnode );
  16. }
  17. Else
  18. {
  19. This. appendright (subnode );
  20. }
  21. }
  22. // Append to left
  23. Public void appendleft (node subnode)
  24. {
  25. If (this. leftsubnode = NULL)
  26. {
  27. This. leftsubnode = subnode;
  28. }
  29. Else
  30. {
  31. This. leftsubnode. append (subnode );
  32. }
  33. }
  34. // Append to the right
  35. Public void appendright (node subnode)
  36. {
  37. If (this. rightsubnode = NULL)
  38. {
  39. This. rightsubnode = subnode;
  40. }
  41. Else
  42. {
  43. This. rightsubnode. append (subnode );
  44. }
  45. }
  46. // Display your data at the node
  47. Public void showdata ()
  48. {
  49. Console. writeline ("Data = {0}", this. data );
  50. }
  51. }
  52. // Binary tree class
  53. Class tree
  54. {
  55. // Root Node
  56. Public node root {Get; set ;}
  57. // Start with a node and insert the node
  58. Public void insert (node newnode)
  59. {
  60. If (this. Root = NULL)
  61. {
  62. This. Root = newnode;
  63. }
  64. Else
  65. {
  66. This. Root. append (newnode );
  67. }
  68. }
  69. // Reload. It is inserted starting from the root node by default.
  70. Public void midtravel ()
  71. {
  72. This. midtravel (this. Root );
  73. }
  74. // Recursive Traversal)
  75. Public void midtravel (node)
  76. {
  77. If (node. leftsubnode! = NULL)
  78. {
  79. This. midtravel (node. leftsubnode );
  80. }
  81. Node. showdata ();
  82. If (node. rightsubnode! = NULL)
  83. {
  84. This. midtravel (node. rightsubnode );
  85. }
  86. }
  87. }
  88. Class Program
  89. {
  90. Static void main (string [] ARGs)
  91. {
  92. TREE tree = new tree ();
  93. Tree. insert (new node {DATA = 3 });
  94. Tree. insert (new node {DATA = 6 });
  95. Tree. insert (new node {DATA = 2 });
  96. Tree. insert (new node {DATA = 7 });
  97. Tree. insert (new node {DATA = 18 });
  98. Tree. midtravel ();
  99. }
  100. }
  101. }
  102. // The true meaning of water
  103. // Http://blog.csdn.net/FantasiaX

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.