Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package bst;
  2.  
  3. public class BinarySearchTree<E extends Comparable<? super E>> {
  4. BinaryNode<E> root;
  5. int size;
  6.  
  7. /**
  8. * Constructs an empty binary searchtree.
  9. */
  10. public BinarySearchTree() {
  11. root = null;
  12. size = 0;
  13. }
  14.  
  15. /**
  16. * Inserts the specified element in the tree if no duplicate exists.
  17. *
  18. * @param x
  19. * element to be inserted
  20. * @return true if the the element was inserted
  21. */
  22. public boolean add(E x) {
  23. int counter = size;
  24. root = add(root, x);
  25. return counter != size;
  26. }
  27.  
  28. private BinaryNode<E> add(BinaryNode<E> node, E x) {
  29. if (node == null) {
  30. size++;
  31. return new BinaryNode<E>(x);
  32. } else if (x.compareTo(node.element) > 0) {
  33. node.right = add(node.right, x);
  34. return node;
  35. } else if (x.compareTo(node.element) < 0) {
  36. node.left = add(node.left, x);
  37. return node;
  38. }
  39. return node;
  40. }
  41.  
  42. /**
  43. * Computes the height of tree.
  44. *
  45. * @return the height of the tree
  46. */
  47. public int height() {
  48. return height(root);
  49. }
  50.  
  51. private int height(BinaryNode<E> node) {
  52. if (node == null) {
  53. return 0;
  54. } else {
  55. int left = 1 + height(node.left);
  56. int right = 1 + height(node.right);
  57.  
  58. if (left >= right) {
  59. return left;
  60. }
  61. return right;
  62. }
  63. }
  64.  
  65. /**
  66. * Returns the number of elements in this tree.
  67. *
  68. * @return the number of elements in this tree
  69. */
  70. public int size() {
  71. return size;
  72. }
  73.  
  74. /**
  75. * Print tree contents in inorder.
  76. */
  77.  
  78. public void printTree() {
  79. printTree(root);
  80. }
  81.  
  82. private void printTree(BinaryNode<E> node) {
  83. if (node != null) {
  84. printTree(node.left);
  85. System.out.print(node.element + " ");
  86. printTree(node.right);
  87. }
  88. }
  89.  
  90. /**
  91. * Builds a complete tree from the elements in the tree.
  92. */
  93. public void rebuild() {
  94. }
  95.  
  96. /*
  97. * Adds all elements from the tree rooted at n in inorder to the array a
  98. * starting at a[index]. Returns the index of the last inserted element + 1 (the
  99. * first empty position in a).
  100. */
  101. private int toArray(BinaryNode<E> n, E[] a, int index) {
  102. return 0;
  103. }
  104.  
  105. /*
  106. * Builds a complete tree from the elements a[first]..a[last]. Elements in the
  107. * array a are assumed to be in ascending order. Returns the root of tree.
  108. */
  109. private BinaryNode<E> buildTree(E[] a, int first, int last) {
  110. return null;
  111. }
  112.  
  113. static class BinaryNode<E extends Comparable<? super E>> {
  114. E element;
  115. BinaryNode<E> left;
  116. BinaryNode<E> right;
  117.  
  118. private BinaryNode(E element) {
  119. this.element = element;
  120. }
  121.  
  122. }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement