Question

This is binary search tree problem. The program reads the text file, and creates a binary...

This is binary search tree problem. The program reads the text file, and creates a binary search tree based on the words in the file.

I can create the tree but I also have to store 'the order of insertion' in each node.  

For example, if text includes "the" 3 times and it is the 1st, 5th, and 9th word in the file,

in binary search tree, one node should have string value "the" and array list{1, 5, 9}.

How can I implement this array list part in Java?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class BinarySearchTree
{
  
static class Node {
String key;
Node left, right;
ArrayList<Integer>l;
};
Node root;
public BinarySearchTree() {
   root=null;
}
  
Node newNode(String data,int pos)
{
Node temp = new Node();
  
temp.key = data;
  
temp.left = null;
temp.right = null;
temp.l=new ArrayList<>();
temp.l.add(pos);
return temp;
}
  

void insert(String key,int pos) {
root = insertRec(root, key,pos);
}

Node insertRec(Node root, String key,int pos) {

if (root == null) {
root = newNode(key, pos);
return root;
}

if (key.compareTo(root.key)<0)
root.left = insertRec(root.left, key,pos);
else if (key.compareTo(root.key)>0)
root.right = insertRec(root.right, key,pos);
else {
   root.l.add(pos);
}

return root;
}   

void Inorder(Node root)
{
if (root == null)
return;
else {
Inorder(root.left);
System.out.print( root.key +" ");
for(int i=0;i<root.l.size();i++) {
   System.out.print(root.l.get(i)+" ");
}
System.out.println();
Inorder(root.right);
}
}

public static void main(String args[]) throws IOException
{
BinarySearchTree b=new BinarySearchTree();
int count=1;
File file = new File("C:\\Users\\Administrator\\Desktop\\demo.txt");
  
BufferedReader br = new BufferedReader(new FileReader(file));
  
String st;
while ((st = br.readLine()) != null) {
  
String ss[]=st.split(" ");
for(int i=0;i<ss.length;i++) {
   b.insert(ss[i],count);
   count++;
}
}
b.Inorder(b.root);
  
}
}

// please give thumbs-up for it....Happy Coding!

Add a comment
Know the answer?
Add Answer to:
This is binary search tree problem. The program reads the text file, and creates a binary...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program...

    13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program which you can store a word with its definition. Each node of the tree will contain the word and definition. The word is what will be used as the key to sort our data. The dictionary should allow you to search for a word. If the word exist then the definition will display. You can also add words to the dictionary. For testing you...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • Creat a C Program that Reads words from a file called words, which contains one word...

    Creat a C Program that Reads words from a file called words, which contains one word per line.Each word has maximum length to M.The number of words in the file is equal to N. Incert each word to Binary Search Tree with node name dictionary.Each node of the tree must contain one word.The left child must contain a word that it is lexicographically smaller while the right child contains a lexicographically bigger one. 1) When you finish reading the file,...

  • 1 Binary Search Trees (25 points) Consider the binary tree as shown in Figure 1. 9...

    1 Binary Search Trees (25 points) Consider the binary tree as shown in Figure 1. 9 5 15 10 17 8 Figure 1: Binary Tree: The letter next to each node (e.g., a, b) denotes the tree node, and the number inside each node is the key. 1.1 Correctness (10 points) Is this binary tree a valid binary search tree? In other words, does it satisfy the binary search tree property? If not, which node(s) violates the binary search tree...

  • JAVA DATA STRUCTURES: Reading a Text file of words into two different data structures 1. Use a Binary search tree and then 2.Use a Hash Map. *USE BOTH BINARY & HASH MAP* * Get the file name as a u...

    JAVA DATA STRUCTURES: Reading a Text file of words into two different data structures 1. Use a Binary search tree and then 2.Use a Hash Map. *USE BOTH BINARY & HASH MAP* * Get the file name as a user input.* Present a menu to the user with the below options: 1) Delete the first occurrence of a given word. 2) Delete all the occurrences of a given word.

  • Read a list of names from a file. Insert the names into a binary search tree...

    Read a list of names from a file. Insert the names into a binary search tree that is a little different from what we discussed in class. For this tree, the left side will contain the larger values and the right side will contain the smaller values. In essence, it is the exact opposite of a normal binary search tree. Our tree will also be able to store duplicate names. Aside from a left and a right pointer for each...

  • Write a program named text_indexing.c that does the following: Reads text and stores it as one...

    Write a program named text_indexing.c that does the following: Reads text and stores it as one string called text. You can read from a file or from the user. (In my implementation, I read only one paragraph (up to new line) from the user. With this same code, I am able to read data from a file by using input redirection (executable < filename) when I run the program. See sample runs below). You can assume that the text will...

  • Write a program, called wordcount.c, that reads one word at a time from the standard input....

    Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT