lots of exercises in java... from https://github.com/exercism/java

BST.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Queue;
  6. public class BST<T extends Comparable<T>> {
  7. public static class Node<T> {
  8. private T data;
  9. private Node<T> left = null;
  10. private Node<T> right = null;
  11. public Node(T data) {
  12. this.data = data;
  13. }
  14. public Node<T> getLeft() {
  15. return left;
  16. }
  17. public void setLeft(Node<T> left) {
  18. this.left = left;
  19. }
  20. public Node<T> getRight() {
  21. return right;
  22. }
  23. public void setRight(Node<T> right) {
  24. this.right = right;
  25. }
  26. public T getData() {
  27. return data;
  28. }
  29. }
  30. private Node<T> root;
  31. private int nodeCount = 0;
  32. public void insert(T value) {
  33. if (root == null) {
  34. root = new Node<>(value);
  35. } else {
  36. this.insert(this.root, value);
  37. }
  38. this.nodeCount++;
  39. }
  40. public List<T> getAsSortedList() {
  41. List<T> result = new ArrayList<>(this.nodeCount);
  42. this.putInSortedOrderToList(this.root, result);
  43. return Collections.unmodifiableList(result);
  44. }
  45. public List<T> getAsLevelOrderList() {
  46. List<T> result = new ArrayList<>(this.nodeCount);
  47. this.putInLevelOrderToList(this.root, result);
  48. return Collections.unmodifiableList(result);
  49. }
  50. public Node<T> getRoot() {
  51. return root;
  52. }
  53. private void insert(Node<T> node, T value) {
  54. if (value.compareTo(node.getData()) <= 0) {
  55. if (node.getLeft() == null) {
  56. node.setLeft(new Node<T>(value));
  57. } else {
  58. insert(node.getLeft(), value);
  59. }
  60. } else {
  61. if (node.getRight() == null) {
  62. node.setRight(new Node<T>(value));
  63. } else {
  64. insert(node.getRight(), value);
  65. }
  66. }
  67. }
  68. private void putInSortedOrderToList(Node<T> node, List<T> list) {
  69. if (node == null || list == null) {
  70. return;
  71. }
  72. if (node.getLeft() != null) {
  73. putInSortedOrderToList(node.getLeft(), list);
  74. }
  75. list.add(node.getData());
  76. if (node.getRight() != null) {
  77. putInSortedOrderToList(node.getRight(), list);
  78. }
  79. }
  80. private void putInLevelOrderToList(Node<T> node, List<T> list) {
  81. if (node == null || list == null) {
  82. return;
  83. }
  84. final Queue<Node<T>> queue = new LinkedList<>();
  85. Node<T> myNode;
  86. Node<T> left;
  87. Node<T> right;
  88. queue.add(node);
  89. while (!queue.isEmpty()) {
  90. myNode = queue.poll();
  91. list.add(myNode.getData());
  92. left = myNode.getLeft();
  93. right = myNode.getRight();
  94. if (left != null) {
  95. queue.add(left);
  96. }
  97. if (right != null) {
  98. queue.add(right);
  99. }
  100. }
  101. }
  102. }