Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. public class binaryTree
  2. {
  3. public binaryTree()
  4. {
  5. root = cursor = null;
  6. }
  7. public final void insert(String name, int weight)
  8. {
  9. if (root == null)
  10. {
  11. root = new Node();
  12. root.name = name;
  13. root.weight = weight;
  14. root.left = root.right = null;
  15. return;
  16. }
  17.  
  18. cursor = root;
  19. set_cursor(name);
  20.  
  21. if (cursor.name.equals(name))
  22. {
  23. System.out.print("Node already exists!!!");
  24. System.out.print("\n");
  25. return;
  26. }
  27.  
  28. if (name.compareTo(cursor.name) < 0)
  29. {
  30. cursor.left = new Node();
  31. cursor = cursor.left;
  32. cursor.name = name;
  33. cursor.weight = weight;
  34. cursor.left = cursor.right = null;
  35. return;
  36. }
  37.  
  38. cursor.right = new Node();
  39. cursor = cursor.right;
  40. cursor.name = name;
  41. cursor.weight = weight;
  42. cursor.left = cursor.right = null;
  43. }
  44. public final void set_cursor(String x)
  45. {
  46. if (cursor.name.equals(x))
  47. {
  48. return;
  49. }
  50.  
  51. if (x.compareTo(cursor.name) < 0 && cursor.left != null)
  52. {
  53. cursor = cursor.left;
  54. set_cursor(x);
  55. }
  56.  
  57. if (x.compareTo(cursor.name) > 0 && cursor.right != null)
  58. {
  59. cursor = cursor.right;
  60. set_cursor(x);
  61. }
  62. }
  63. public final void print()
  64. {
  65. System.out.print("Preorder: ");
  66. preorder(root);
  67. System.out.print("\n");
  68.  
  69. System.out.print("Inorder: ");
  70. inorder(root);
  71. System.out.print("\n");
  72.  
  73. System.out.print("Postorder: ");
  74. postorder(root);
  75. System.out.print("\n");
  76. System.out.print("\n");
  77. }
  78.  
  79. public static class Node
  80. {
  81. public String name;
  82. public int weight;
  83. public Node left;
  84. public Node right;
  85. }
  86. public Node root;
  87. public Node cursor;
  88.  
  89. public final void preorder(Node p)
  90. {
  91. if (p != null)
  92. {
  93. System.out.print(p.name);
  94. System.out.print(' ');
  95. preorder(p.left);
  96. preorder(p.right);
  97. }
  98. }
  99. public final void inorder(Node p)
  100. {
  101. if (p != null)
  102. {
  103. inorder(p.left);
  104. System.out.print(p.name);
  105. System.out.print(' ');
  106. inorder(p.right);
  107. }
  108. }
  109. public final void postorder(Node p)
  110. {
  111. if (p != null)
  112. {
  113. postorder(p.left);
  114. postorder(p.right);
  115. System.out.print(p.name);
  116. System.out.print(' ');
  117. }
  118. }
  119. public final int findHeight(Node p)
  120. {
  121. if (p.left == null && p.right == null)
  122. {
  123. return 0;
  124. }
  125. else
  126. {
  127. if (p.left != null)
  128. {
  129. left = findHeight(p.left);
  130. }
  131. if (p.right != null)
  132. {
  133. right = findHeight(p.right);
  134. }
  135.  
  136. if (left > right)
  137. {
  138. h = 1 + left;
  139. }
  140. else
  141. {
  142. h = 1 + right;
  143. }
  144. }
  145. return h;
  146. }
  147. public final int findLeaves(Node p)
  148. {
  149. if (p == null)
  150. {
  151. return 0;
  152. }
  153. if (p.left == null && p.right == null)
  154. {
  155. return 1;
  156. }
  157. else
  158. {
  159. return findLeaves(p.left) + findLeaves(p.right);
  160. }
  161. }
  162. public final int search(String name)
  163. {
  164. int weight = 0;
  165. cursor = root;
  166.  
  167. while (cursor != null)
  168. {
  169. if (cursor.name.equals(name))
  170. {
  171. return cursor.weight;
  172. }
  173.  
  174. else if (name.compareTo(cursor.name) < 0)
  175. {
  176. cursor = cursor.left;
  177. }
  178.  
  179. else
  180. {
  181. cursor = cursor.right;
  182. }
  183. }
  184.  
  185. System.out.print("Could not find ");
  186. System.out.print(name);
  187. System.out.print("\n");
  188. System.out.print("\n");
  189. return 0;
  190. }
  191. public final int lowestWeight(Node p)
  192. {
  193. if (p != null)
  194. {
  195. if (p.weight < small)
  196. {
  197. small = p.weight;
  198. }
  199. lowestWeight(p.left);
  200. lowestWeight(p.right);
  201. }
  202.  
  203. return small;
  204. }
  205. public final String firstName()
  206. {
  207. String first = "";
  208. cursor = root;
  209.  
  210. if (root == null)
  211. {
  212. System.out.print("Tree is empty!!!");
  213. return first;
  214. }
  215.  
  216. else
  217. {
  218. while (cursor.left != null)
  219. {
  220. cursor = cursor.left;
  221. }
  222.  
  223. first = cursor.name;
  224. return first;
  225. }
  226. }
  227. public int small;
  228. public int left;
  229. public int right;
  230. public int h;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement