Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *The purpose of this class is to create functions that allow for the user to manipulate the BST. The methods within
- * this class allow the user to insert into the tree, search the tree, get a count of words in the BST, and unique words
- * 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
- * the BST.
- * @
- * @author Daniel Henrichs
- * @version 1.0
- * @since 2019-07-30
- * @see BSTFunctions
- */
- import java.util.ArrayList;
- import java.util.List;
- class BSTFunctions {
- BSTNode ROOT;
- int nodecount, uc, key;
- public BSTFunctions() {
- this.ROOT = null;
- nodecount = 0;
- uc = 0;
- }
- /**
- * The purpose of this method to check whether to insert new word or update word count
- * @author Daniel Henrichs
- * @version 1.0
- * @since 2019-07-30
- * @param node
- * @param word
- * @param data
- */
- void insert(BSTNode node, String word, @SuppressWarnings("SameParameterValue") int data) {
- if (search(node, word)) {
- } else {
- insertNode(node, word, data);
- }
- }
- /**
- * The purpose of this method is to add nodes to the BST
- *@author Daniel Henrichs
- * @version 1.0
- * @since 2019-07-30
- * @param node
- * @param word
- * @param data
- */
- private void insertNode(BSTNode node, String word, int data) {
- if (node == null) {
- node = new BSTNode(word, data);
- ROOT = node;
- } else if (word.compareTo(node.word) < 0 && node.left == null) {
- node.left = new BSTNode(word, data);
- node.left.parent = node;
- } else if (word.compareTo(node.word) > 0 && node.right == null) {
- node.right = new BSTNode(word, data);
- node.right.parent = node;
- } else {
- if (word.compareTo(node.word) < 0) {
- insertNode(node.left, word, data);
- } else {
- insertNode(node.right, word, data);
- }
- }
- }
- /**
- *The purpose of this method is to search the tree to see if the word is already part of the tree
- * @author Daniel Henrichs
- * @version 1.0
- * @since 2019-07-30
- * @param node
- * @param word
- * @return search
- */
- private boolean search(BSTNode node, String word) {
- if (node == null) {
- return false;
- } else if (word.compareTo(node.word) == 0) {
- node.data++;
- return true;
- } else {
- if (word.compareTo(node.word) < 0) {
- return search(node.left, word);
- } else {
- return search(node.right, word);
- }
- }
- }
- /**
- * The purpose of this method is to count words, unique words in Binary search tree
- * @author Daniel Henrichs
- * @version 1.0
- * @since 2019-07-30
- * @param node
- */
- public void wordCount(BSTNode node) {
- if (node != null) {
- nodecount++;
- wordCount(node.left);
- if (node.data == 1)
- uc++;
- wordCount(node.right);
- }
- }
- /**
- * Returns the number of occurrences of each individual word
- * @author Daniel Henrichs
- * @version 1.0
- * @since 2019-07-30
- * @param node
- * @return result
- */
- public List<String> listInOrder(BSTNode node) {
- List<String> result = new ArrayList<>();
- if (node != null) {
- result.addAll(listInOrder(node.left));
- result.add(node.word + " - " + node.data);
- result.addAll(listInOrder(node.right));
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement