fork download
  1. // From http://stackoverflow.com/questions/5476183/the-program-not-print-output-anyone-can-help
  2. // plus my corrections
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. namespace BinarySearchTree
  10. {
  11. public class Node
  12. {
  13. public int Data;
  14. public Node Left;
  15. public Node Right;
  16.  
  17. public void DisplayNode()
  18. {
  19. Console.Write(Data + " ");
  20. }
  21. }
  22.  
  23. public class BinarySearchTree
  24. {
  25. public Node root;
  26. public BinarySearchTree()
  27. {
  28. root = null;
  29. }
  30. public void Insert(int i)
  31. {
  32. Node newNode = new Node();
  33. newNode.Data = i;
  34. if (root == null)
  35. root = newNode;
  36. else
  37. {
  38. Node current = root;
  39. Node parent;
  40. while (true)
  41. {
  42. parent = current;
  43. if (i < current.Data)
  44. {
  45. current = current.Left;
  46. if (current == null)
  47. {
  48. parent.Left = newNode;
  49. break;
  50. }
  51. }
  52. else
  53. {
  54. current = current.Right;
  55. if (current == null)
  56. {
  57. parent.Right = newNode;
  58. break;
  59. }
  60. }
  61. }
  62. }
  63. }
  64.  
  65. public void InOrder(Node theRoot)
  66. {
  67. if (!(theRoot == null))
  68. {
  69. InOrder(theRoot.Left);
  70. theRoot.DisplayNode();
  71. InOrder(theRoot.Right);
  72. }
  73. }
  74.  
  75. static void Main()
  76. {
  77. BinarySearchTree nums = new BinarySearchTree();
  78. nums.Insert(23);
  79. nums.Insert(45);
  80. nums.Insert(16);
  81. nums.Insert(37);
  82. nums.Insert(3);
  83. nums.Insert(99);
  84. nums.Insert(22);
  85. Console.WriteLine("Inorder traversal: ");
  86. nums.InOrder(nums.root);
  87. }
  88. }
  89. }
  90.  
Success #stdin #stdout 0.01s 36976KB
stdin
Standard input is empty
stdout
Inorder traversal: 
3 16 22 23 37 45 99