You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. // Binary Search Tree class
  2. // Xiwei Wang
  3. import java.util.*;
  4. public class BST {
  5. // instance variables
  6. private BSTNode m_root;
  7. private int m_size;
  8. // constructor
  9. public BST() {
  10. m_root = null;
  11. m_size = 0;
  12. }
  13. // This method returns the number of elements in the tree.
  14. // Do not make any changes to this method!
  15. public int size() {
  16. return m_size;
  17. }
  18. // This method clears the content of the tree.
  19. // Do not make any changes to this method!
  20. public void clear() {
  21. m_root = null;
  22. m_size = 0;
  23. }
  24. // This non-recursive method takes a string and inserts it into the binary
  25. // search tree, keeping the tree ordered.
  26. public void add(String value) {
  27. BSTNode to_add = new BSTNode(value);
  28. if (m_root == null) {
  29. m_root = to_add;
  30. m_size++;
  31. }
  32. else {
  33. BSTNode current = m_root;
  34. BSTNode parent = null;
  35. int comparison = 0;
  36. while (current != null) {
  37. comparison = value.compareTo(current.getInfo());
  38. if (comparison > 0) {
  39. parent = current;
  40. current = current.getRight();
  41. }
  42. else if (comparison < 0) {
  43. parent = current;
  44. current = current.getLeft();
  45. }
  46. else
  47. return;
  48. }
  49. if (comparison > 0)
  50. parent.setRight(to_add);
  51. else
  52. parent.setLeft(to_add);
  53. m_size++;
  54. }
  55. }
  56. // This non-recursive method returns a string that represents the in-order traversal
  57. // of the binary search tree.
  58. public String inOrder() {
  59. String return_string = "";
  60. Stack<BSTNode> stack = new Stack<BSTNode>();
  61. BSTNode current = m_root;
  62. while ((current != null) || (stack.size() > 0)) {
  63. while (current != null) {
  64. stack.push(current);
  65. current = current.getLeft();
  66. }
  67. current = stack.pop();
  68. return_string += current.getInfo() + " ";
  69. current = current.getRight();
  70. }
  71. return return_string;
  72. }
  73. // This method returns the smallest element in the binary search tree. You
  74. // are not allowed to create any additional structures, including but not
  75. // limited to arrays, stacks, queues, or other trees.
  76. public String min() {
  77. BSTNode current = m_root;
  78. while (current.getLeft() != null)
  79. current = current.getLeft();
  80. return current.getInfo();
  81. }
  82. // This method takes a reference to the root of the expression, evaluates
  83. // the tree, and returns the result as an int.
  84. public int evaluate(BSTNode node) {
  85. return -1; // replace this statement with your own return
  86. }
  87. }