code_junkie

BST C# Code with errors

Nov 13th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. BinarySearchTree t = new BinarySearchTree();
  6.  
  7. t.insert(ref t.root, 10);
  8. t.insert(ref t.root, 5);
  9. t.insert(ref t.root, 6);
  10. t.insert(ref t.root, 17);
  11. t.insert(ref t.root, 2);
  12. t.insert(ref t.root, 3);
  13.  
  14. BinarySearchTree.print(t.root);
  15. }
  16.  
  17. public class TreeNode
  18. {
  19. public int n;
  20. public TreeNode _left;
  21. public TreeNode _right;
  22.  
  23.  
  24. public TreeNode(int n, TreeNode _left, TreeNode _right)
  25. {
  26. this.n = n;
  27. this._left = _left;
  28. this._right = _right;
  29. }
  30.  
  31.  
  32. public void DisplayNode()
  33. {
  34. Console.Write(n);
  35. }
  36. }
  37.  
  38.  
  39. public class BinarySearchTree
  40. {
  41. public TreeNode root;
  42.  
  43. public BinarySearchTree()
  44. {
  45. root = null;
  46. }
  47.  
  48.  
  49. public void insert(ref TreeNode root, int x)
  50. {
  51. if (root == null)
  52. {
  53. root = new TreeNode(x, null, null);
  54. }
  55. else
  56. if (x < root.n)
  57. insert(ref root._left, x);
  58. else
  59. insert(ref root._right, x);
  60. }
  61.  
  62. public int FindMin()
  63. {
  64. TreeNode current = root;
  65.  
  66. while (current._left != null)
  67. current = current._left;
  68.  
  69. return current;
  70. }
  71.  
  72. public int FindMax()
  73. {
  74. TreeNode current = root;
  75.  
  76. while (current._right != null)
  77. current = current._right;
  78.  
  79. return current;
  80. }
  81.  
  82. public TreeNode Find(int key)
  83. {
  84. TreeNode current = root;
  85.  
  86. while (current.n != key)
  87. {
  88. if (key < current.n)
  89. current = current._left;
  90. else
  91. current = current._right;
  92. if (current == null)
  93. return null;
  94. }
  95. return current;
  96. }
  97.  
  98.  
  99.  
  100. public void InOrder(ref TreeNode root)
  101. {
  102. if (root != null)
  103. {
  104. InOrder(ref root._left);
  105. root.DisplayNode();
  106. InOrder(ref root._right);
  107. }
  108. }
  109.  
  110.  
  111. public static void print(TreeNode root)
  112. {
  113. if (root != null)
  114. {
  115. print(root._left);
  116. Console.WriteLine(root.n.ToString());
  117. print(root._right);
  118. }
  119.  
  120. }
  121.  
  122. public int CountNodes()
  123. {
  124. int count = 1; // me!
  125. if (_left != null) count += _left.CountNodes();
  126. if (_right != null) count += _right.CountNodes();
  127. return count;
  128. }
  129.  
  130. public int CountLeaves()
  131. {
  132. int count = (_left == null && _right == null) ? 1 : 0;
  133. if (_left != null) count += _left.CountLeaves();
  134. if (_right != null) count += _right.CountLeaves();
  135. return count;
  136. }
Add Comment
Please, Sign In to add comment