Question
JAVA BINARY TREE OPERATION Please Help! I've been working on a java program that'll store words and their definition in a binary tree alphabetically. I
JAVA BINARY TREE OPERATION Please Help!
I've been working on a java program that'll store words and their definition in a binary tree alphabetically. I am just learning so I apologize for my confusion. There are several operations throughout this project but just for right now, I am stuck on how I would insert a word into the BST. I have three classes: Main (takes in the word and definition from the user), Dictionary, and DefinitionNode (stores helper methods). I am not to add additional fields or public methods but can add private methods. I have posted the Dictionary and DefinitionNode classes below with respective fields, methods, and constructor. I think it'll also involve recursion to continually check both left and right. Thank you for the help in advance!
Dictionary
private DefinitionNode root; // the root definition in the BST
/**
* Inserts a new word along with its meaning into the dictionary.
* @param word The word to be inserted
* @param meaning The meaning of the word being inserted
* @throws IllegalArgumentException when the word is already in this dictionary
*/
public void insert(String word, String meaning) throws IllegalArgumentException {
// implement using the DefinitionNode's insertHelper() method
}
DefinitionNode
private final String word; // the word this definition defines
private final String meaning; // the meaning of that word
private DefinitionNode leftChild; // nodes preceding this one alphabetically
private DefinitionNode rightChild; // nodes following this one alphabetically
/**
* Constructs a DefinitionNode with the specified word and meaning.
* @param word The word associated with this definition
* @param meaning The meaning of that word
* @throws IllegalArgumentException when the word or meaning are either
* references to an empty string or null references.
*/
public DefinitionNode(String word, String meaning) { }
/**
* This helper method inserts a new node into the tree or subtree that is
* rooted at this node. If it cannot directly add the new node as a child
* of this node, then it must recursively call this method on its appropriate
* child, to eventually complete this insertion.
* @param newNode The new node that is being inserted into the tree
* @throws IllegalArgumentException when the word inside newNode is already in
* the tree. Multiple definitions for the same word are not allowed.
*/
public void insertHelper(DefinitionNode newNode) throws IllegalArgumentException { }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started