Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. /**
  2. *The purpose of this class is to create functions that allow for the user to manipulate the BST. The methods within
  3. * this class allow the user to insert into the tree, search the tree, get a count of words in the BST, and unique words
  4. * in the BST, as well as allowing the user to return all the words in the BST and the number of times they occur in
  5. * the BST.
  6. * @
  7. * @author Daniel Henrichs
  8. * @version 1.0
  9. * @since 2019-07-30
  10. * @see BSTFunctions
  11. */
  12.  
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. class BSTFunctions {
  17. BSTNode ROOT;
  18. int nodecount, uc, key;
  19.  
  20. public BSTFunctions() {
  21. this.ROOT = null;
  22. nodecount = 0;
  23. uc = 0;
  24. }
  25.  
  26.  
  27. /**
  28. * The purpose of this method to check whether to insert new word or update word count
  29. * @author Daniel Henrichs
  30. * @version 1.0
  31. * @since 2019-07-30
  32. * @param node
  33. * @param word
  34. * @param data
  35. */
  36. void insert(BSTNode node, String word, @SuppressWarnings("SameParameterValue") int data) {
  37. if (search(node, word)) {
  38. } else {
  39. insertNode(node, word, data);
  40. }
  41. }
  42.  
  43. /**
  44. * The purpose of this method is to add nodes to the BST
  45. *@author Daniel Henrichs
  46. * @version 1.0
  47. * @since 2019-07-30
  48. * @param node
  49. * @param word
  50. * @param data
  51. */
  52. private void insertNode(BSTNode node, String word, int data) {
  53.  
  54. if (node == null) {
  55. node = new BSTNode(word, data);
  56. ROOT = node;
  57. } else if (word.compareTo(node.word) < 0 && node.left == null) {
  58. node.left = new BSTNode(word, data);
  59. node.left.parent = node;
  60. } else if (word.compareTo(node.word) > 0 && node.right == null) {
  61. node.right = new BSTNode(word, data);
  62. node.right.parent = node;
  63. } else {
  64. if (word.compareTo(node.word) < 0) {
  65. insertNode(node.left, word, data);
  66. } else {
  67. insertNode(node.right, word, data);
  68. }
  69. }
  70. }
  71.  
  72. /**
  73. *The purpose of this method is to search the tree to see if the word is already part of the tree
  74. * @author Daniel Henrichs
  75. * @version 1.0
  76. * @since 2019-07-30
  77. * @param node
  78. * @param word
  79. * @return search
  80. */
  81. private boolean search(BSTNode node, String word) {
  82. if (node == null) {
  83. return false;
  84. } else if (word.compareTo(node.word) == 0) {
  85. node.data++;
  86. return true;
  87. } else {
  88. if (word.compareTo(node.word) < 0) {
  89. return search(node.left, word);
  90. } else {
  91. return search(node.right, word);
  92. }
  93. }
  94. }
  95.  
  96. /**
  97. * The purpose of this method is to count words, unique words in Binary search tree
  98. * @author Daniel Henrichs
  99. * @version 1.0
  100. * @since 2019-07-30
  101. * @param node
  102. */
  103. public void wordCount(BSTNode node) {
  104. if (node != null) {
  105. nodecount++;
  106. wordCount(node.left);
  107. if (node.data == 1)
  108. uc++;
  109. wordCount(node.right);
  110. }
  111. }
  112.  
  113.  
  114. /**
  115. * Returns the number of occurrences of each individual word
  116. * @author Daniel Henrichs
  117. * @version 1.0
  118. * @since 2019-07-30
  119. * @param node
  120. * @return result
  121. */
  122. public List<String> listInOrder(BSTNode node) {
  123. List<String> result = new ArrayList<>();
  124. if (node != null) {
  125. result.addAll(listInOrder(node.left));
  126. result.add(node.word + " - " + node.data);
  127. result.addAll(listInOrder(node.right));
  128. }
  129. return result;
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement