Advertisement
Guest User

Untitled

a guest
May 6th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. import java.util.*;
  2. public class BSTree<T extends Comparable<T>>{
  3. private class Node{
  4. T data;
  5. Node right, left;
  6. boolean DEBUG = true;
  7.  
  8. public void debug(T i){System.out.println(i);}
  9. public void debug(int i){System.out.println(i);}
  10.  
  11. public void setData(T d){ data = d; }
  12. public void setRight(Node r){ right = r; }
  13. public void setLeft(Node l){ left = l; }
  14.  
  15. public T getData(){ return data; }
  16. public Node getRight(){ return right; }
  17. public Node getLeft(){ return left; }
  18.  
  19. public Node(T value){
  20. setData(value);
  21. }
  22. public Node(T value, Node right, Node left){
  23. setData(value);
  24. setRight(right);
  25. setLeft(left);
  26. }
  27.  
  28. public int height(){
  29. int rightH = 0;
  30. int leftH = 0;
  31. if(left == null && right == null){
  32. return 1;
  33. }else if(right != null){
  34. rightH += right.height();
  35. }else if(left != null){
  36. leftH += left.height();
  37. }
  38.  
  39. if(rightH > leftH){
  40. return rightH + 1;
  41. }
  42. return leftH + 1;
  43. }
  44.  
  45. public void add(T value){
  46. if(value.compareTo(data) < 0){
  47. if(left != null){
  48. left.add(value);
  49. }else{
  50. setLeft(new Node(value));
  51. }
  52. }else{
  53. if(right != null){
  54. right.add(value);
  55. }else{
  56. setRight(new Node(value));
  57. }
  58. }
  59. }
  60.  
  61. public String toString(){
  62. String Tree = "";
  63. if(right == null && left == null){
  64. Tree += data + "__";
  65. //debug(" null " + Tree);
  66. }
  67. if(right != null && left == null){
  68. Tree += data + "_" + right.toString();
  69. //debug(" right " + Tree);
  70. }
  71. if(left != null && right == null){
  72. Tree += data + left.toString() + "_";
  73. //debug(" left " + Tree);
  74. }
  75. if(left != null && right != null){
  76. Tree += data + left.toString() + right.toString();
  77. //debug(" both " + Tree);
  78. }
  79. return Tree;
  80. }
  81.  
  82. public boolean contains(T value){
  83. if(data.compareTo(value) == 0){
  84. return true;
  85. }else if(data.compareTo(value) > 0 && left != null){
  86. return left.contains(value);
  87. }else if(right!= null){
  88. return right.contains(value);
  89. }
  90. return false;
  91. }
  92. }
  93.  
  94.  
  95. private Node root;
  96.  
  97. public int getHeight(){
  98. if(root == null){
  99. return 0;
  100. }else{
  101. return root.height();
  102. }
  103. }
  104.  
  105. public void add(T value){
  106. if(root == null){
  107. root = new Node(value);
  108. }else{
  109. root.add(value);
  110. }
  111. }
  112. public String toString(){
  113. if (root == null){
  114. return "";
  115. }else{
  116. return root.toString();
  117. }
  118. }
  119. public boolean contains(T value){
  120. if(root == null){
  121. return false;
  122. }else{
  123. return root.contains(value);
  124. }
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement