Create a WordCount class that uses the AVL tree class to count words from a file. The trick here is that you should not have duplicate words In the list Basically when a word Is read from the file the tree should be checked to see if the word already exists in the tree. If it does exists the count for the word should simply be Incremented. If It does not exist then the node should be added to the tree and the count for that particular word should be set to 1. You should have functionality that will print each word in the tree and how many times it appears. This is not simply reading a list of words from a file. Your WordCount class should read any text from the file and count the words in it This is going to require a bit of parsing on your part Please discard punctuation characters like comma and period.
Explanation / Answer
public AVLNode search(String key) { // Tree is empty if (root == null) { return null; } // Tree is not empty AVLNode p = root; try {while (true) { if(p == null && p.getData().equals(key)) { break; } else if (key.compareTo(p.getData()) < 0) { p = p.getLeftChild(); } else { p = p.getRightChild(); } } } catch(NullPointerException e) { System.out.println(e); } return p; } // INSERT A NODE INTO AVL TRRE public void insert(String data) { // Case: tree is empty 0 && p.getRightChild() != null) { p = p.getRightChild(); } else { break; } } // insert new node to the tree as a child of node p AVLNode newNode= new AVLNode(data); if(data.equals(p.getData())) { p.setLeftChild(newNode); } else if (data.compareTo(p.getData()) < 0) { p.setLeftChild(newNode); } else { p.setRightChild(newNode); } rebalanceInsertionPath(newNode); }