Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. import static java.lang.System.*;
  2.  
  3. class Tree {
  4.  
  5. private class Node {
  6. String key;
  7. Queue<Integer> value;
  8. Node left;
  9. Node right;
  10. int level;
  11.  
  12. public Node(String tmpkey){
  13. key = tmpkey;
  14. value = new Queue();
  15. left = null;
  16. right = null;
  17. }
  18.  
  19. public Node(){
  20. key = null;
  21. value = new Queue();
  22. left = null;
  23. right = null;
  24. }
  25. }
  26.  
  27. private Node root;
  28. private Node current;
  29.  
  30. private void debugHelper(Node tree, int depth) {
  31.  
  32.  
  33. if (tree != null) {
  34. String padding;
  35.  
  36.  
  37.  
  38. tree.level=depth;
  39.  
  40. depth++;
  41. debugHelper(tree.left,depth);
  42.  
  43. depth--;
  44. depth++;
  45. int num = 10;
  46. System.out.println(""+tree.level+" "+tree.key);
  47. String spaces = String.format("%"+num+"s", "");
  48. System.out.println(spaces);
  49. debugHelper(tree.right, depth);
  50.  
  51. }
  52.  
  53.  
  54.  
  55.  
  56. // Your code here might be recursive
  57. //throw new UnsupportedOperationException();
  58. }
  59.  
  60. private void outputHelper(Node tree) {
  61. // Your code here might be recursive
  62. throw new UnsupportedOperationException();
  63. }
  64.  
  65. public void insert(String key, Integer linenum) {
  66. if (current == null) {
  67. root = new Node(key);
  68. current = root;
  69. System.out.println("- ROOT - new node: " + key );
  70. //root.value.insert(linenum);
  71. } else {
  72. if ((current.key.compareToIgnoreCase(key)) > 0) {
  73. // root is bigger than key
  74. if (current.left != null) {
  75. current = current.left;
  76. insert(key, linenum);
  77. } else {
  78. current.left = new Node(key);
  79. //current.left.value.insert(linenum);
  80. System.out.println("- NEW NODE: " + key );
  81. System.out.println(" " + current.left.key + " is left of " +current.key);
  82. current = root;
  83.  
  84. }
  85. }
  86. else if ((current.key.compareToIgnoreCase(key)) < 0) {
  87. // key is bigger than root
  88. if (current.right != null){
  89. current = current.right;
  90. insert (key,linenum);
  91. } else {
  92. current.right = new Node(key);
  93. //current.right.value.insert(linenum);
  94. System.out.println("- NEW NODE: " + key );
  95. System.out.println(" " + current.right.key +" is right of " +current.key);
  96. current = root;
  97. }
  98. }
  99.  
  100. else {
  101. System.out.println("A repeating word: '"+ key + "'");
  102. //current.value.insert(linenum);
  103. current = root;
  104. //throw new UnsupportedOperationException();
  105. }
  106. }
  107. }
  108.  
  109.  
  110. public void debug() {
  111. System.out.println("\ncalling debug success.\n");
  112. debugHelper(root, 0);
  113.  
  114.  
  115. }
  116.  
  117. public void output() {
  118. // Show sorted words with lines where each word appears
  119. outputHelper(root);
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement