Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment2;
- import java.io.File;
- import java.util.Scanner;
- public class BinarySearchTree extends LinkedBinaryTree {
- Scanner reader;
- LinkedBinaryTree tree = new LinkedBinaryTree();
- /*
- * readIn performs the following operations:
- * (1) reads from fileName word by word
- * (2) for every new word found in fileName a new BTNode is created
- * and added to T according to the rules of BST insertion
- * (3) for every duplicate word, wordCounter for the corresponding BTNode is incremented
- */
- public void readIn(String fileName) {
- BTNode current = root;
- BTNode prev = null;
- try {
- reader = new Scanner(new File(fileName));
- } catch (Exception e) {
- System.out.println("Could not find file!");
- }
- while (reader.hasNext()) {
- String word = reader.next();
- while (current != null) { // pointer for inserting new node
- prev = current;
- if (current.element().compareTo(word) < 0) {
- current = current.right;
- } else {
- current = current.left;
- }
- }
- if (root == null) { // if tree is empty
- tree.addRoot(word);
- } else if (prev.element().compareTo(word) < 0) {
- tree.addRight(prev, word);
- } else {
- tree.addLeft(prev, word);
- }
- }
- }
- /*
- * maxSearchPath finds and returns the length of the longest search path in T
- */
- public int maxSearchPath() {
- return treeHeight(tree.root);
- }
- /*
- * printWordsSorted prints all distinct words found in fileName, in sorted order
- */
- public void printWordsSorted() {
- }
- /*
- * printTenMostCommonWords finds and prints ten most common words from
- * fileName together with their respective wordCounter -s
- */
- public void printTenMostCommonWords() {
- }
- public int treeHeight(BTNode node) {
- if(node == null) {
- return -1;
- }
- return 1 + Math.max(treeHeight(node.left), treeHeight(node.right));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement