Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. package bst;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5.  
  6. import javax.xml.soap.Node;
  7.  
  8.  
  9. public class BinarySearchTree<E> {
  10. BinaryNode<E> root; // Används också i BSTVisaulizer
  11. int size; // Används också i BSTVisaulizer
  12. private Comparator<E> comp;
  13.  
  14. /**
  15. * Constructs an empty binary search tree.
  16. */
  17. public BinarySearchTree() {
  18. root = null;
  19. comp = null;
  20. }
  21.  
  22. /**
  23. * Constructs an empty binary search tree, sorted according to the specified comparator.
  24. */
  25. public BinarySearchTree(Comparator<E> comp) {
  26. this.comp = comp;
  27. }
  28.  
  29. /**
  30. * Inserts the specified element in the tree if no duplicate exists.
  31. * @param x element to be inserted
  32. * @return true if the the element was inserted
  33. */
  34. private int compEl(E e1, E e2){
  35. if(comp == null){
  36. return ((Comparable<E>) e1).compareTo(e2);
  37. }else{
  38. return comp.compare(e1, e2);
  39. }
  40. }
  41.  
  42. public boolean add(E x) {
  43. if(size == 0){
  44. root = new BinaryNode<E>(x);
  45. size= 1;
  46. return true;
  47. }else{
  48. return add(root, x);
  49. }
  50. }
  51.  
  52. private boolean add(BinaryNode<E> n, E x){
  53. if(n == null){
  54. n = new BinaryNode<E>(x);
  55. return true;
  56. }
  57. int c = compEl(x, n.element);
  58.  
  59. if(c == 0){
  60. return false;
  61. }
  62. if(c<0){
  63. return add(n.left, x);
  64. }if(c>0){
  65. return add(n.right, x);
  66. }
  67. return false;
  68. }
  69.  
  70. /**
  71. * Computes the height of tree.
  72. * @return the height of the tree
  73. */
  74. public int height(){
  75. return height(root);
  76. }
  77.  
  78. private int height(BinaryNode<E> n){
  79. if(n == null){
  80. return 0;
  81. }else{
  82. int lefth = height(n.left);
  83. int righth = height(n.right);
  84. return 1 + Math.max(lefth, righth);
  85. }
  86.  
  87. }
  88.  
  89. /**
  90. * Returns the number of elements in this tree.
  91. * @return the number of elements in this tree
  92. */
  93. public int size() {
  94. return size;
  95. }
  96.  
  97. /**
  98. * Removes all of the elements from this list.
  99. */
  100. public void clear() {
  101. size =0;
  102. root = null;
  103. }
  104.  
  105. /**
  106. * Print tree contents in inorder.
  107. */
  108. public void printTree() {
  109. printTree(root);
  110. }
  111.  
  112. private void printTree(BinaryNode<E> n){
  113. if(n == null){
  114. System.out.println("Ajajaj, Tree is empty");
  115. }else{
  116. printTree(n.left);
  117. System.out.println(""+n.element);
  118. printTree(n.right);
  119. }
  120. }
  121.  
  122. /**
  123. * Builds a complete tree from the elements in the tree.
  124. */
  125. public void rebuild() {
  126.  
  127. }
  128.  
  129. /*
  130. * Adds all elements from the tree rooted at n in inorder to the list sorted.
  131. */
  132. private void toArray(BinaryNode<E> n, ArrayList<E> sorted) {
  133.  
  134. }
  135.  
  136. /*
  137. * Builds a complete tree from the elements from position first to
  138. * last in the list sorted.
  139. * Elements in the list a are assumed to be in ascending order.
  140. * Returns the root of tree.
  141. */
  142. private BinaryNode<E> buildTree(ArrayList<E> sorted, int first, int last) {
  143. return null;
  144. }
  145.  
  146.  
  147.  
  148. static class BinaryNode<E> {
  149. E element;
  150. BinaryNode<E> left;
  151. BinaryNode<E> right;
  152.  
  153. private BinaryNode(E element) {
  154. this.element = element;
  155. }
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement