Question

Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry...

Goal: design and implement a dictionary. implement your dictionary using AVL tree .
Problem​:
Each entry in the dictionary is a pair: (word, meaning). Word is a one-word string, meaning can
be a string of one or more words (it’s your choice of implementation, you can restrict the
meaning to one-word strings). The dictionary is case-insensitive. It means “Book”, “BOOK”, “book” are all the same .
Your dictionary application must provide its operations through the following menu (make sure that you follow this format):
Possible operations:
f) find the meaning of a word.
i) insert and entry.
l) load the dictionary from an input file.
p) print all the entries.
r) remove an entry.
s) save the contents of the dictionary in an output file. x) exit
Please choose an option (f, i, l, p, r, s, or x):
The user should enter one of the options (just one character f, i, l, p,r, s or x). Your program should act based on the user’s option:
Find option:
The program asks the user to enter a word, then searches the dictionary and if it exists, prints the meaning of the word, otherwise prints:
Word not found.
Insert option:
The program asks the user to enter a word. The program searches the word in the dictionary, and if it does not exist, it asks the user to enter the meaning of the word and it should insert the entry (word, meaning) into the dictionary. If the word exists in the dictionary the program prints:
The word you entered already exists in the dictionary.
Load option:
The program asks the user to enter the name (address) of the file containing a set of entries (e.g. “dictionary.txt”). It should open the file, read the entries and insert them in the dictionary. Format of the file: each line is one entry. The first word in the line is the word and the rest of the line is the meaning (it can be just one word, depending on your design).
Print option:
The program should print all the entries sorted based on the word (alphabetically). Each entry on a separate line. For example:
abide follow
boss chief
destroy demolish domestic family ...
Remove option:
The program asks the user to enter a word and the program searches the word in the dictionary and remove it, otherwise prints:
Word not found.
Save option:
The program asks the user to enter the name (address) of a file to store the entries. ​The program should write the entries in the dictionary according to the pre-order traversal of the tree into the output file.​​Each line of the file contains one entry of the dictionary.

Exit option:
Terminates the program.
Guideline:
In addition to AVL tree ADT, you need at least three other classes: one class for word entries (containing word-meaning) and the main dictionary class, and a test class. AVL trees are an enhanced form of Binary Search Trees. You have to implement method remove (modify method remove from Binary Search Tree implementation).
Specific requirements
You must name your main class (the class with the main method) ​dictionary.java.​Make sure to add a readme file that explains your code (different classes and their methods) and any additional notes about your program
Important Remarks
It’s very important that you make sure your program compiles without any problem. Your input and output file format should exactly follow the requirements stated previously.
The code should be in java.

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

#include <iostream>
#include<iomanip>
using namespace std;

class node
{
int key;
string meaning;
node *left,*right;
int height;
public:
node()
{
left=right=NULL;
height=1;
meaning="";
key=-1;
}
node(int key,string meaning)
{
this->key=key;
this->meaning=meaning;
left=right=NULL;
height=1;
}
void print()
{
cout<<endl<<setw(10)<<key<<setw(10)<<meaning;
}
friend class Dictionary;
};
class Dictionary
{
node *root;
public:
Dictionary()
{
root=NULL;
}
int max(int,int);
int getheight(node*);
node *insert(node *rnode,int key,string meaning);
void insertInit(int key,string meaning);
node *rightRotate(node *);
node *leftRotate(node *);
int getbalance(node*);
void preorder();
void preorderRec(node*);
void postorder();
void postorderRec(node *);
void inorder();
void inorderRec(node *);
};
int Dictionary::max(int a,int b)
{
return (a>b)?a:b;
}
int Dictionary::getheight(node *nnode)
{
if(nnode==NULL)
return 0;
else
return nnode->height;
}
int Dictionary::getbalance(node *n)
{
if(n==NULL)
return 0;
else
return (getheight(n->left)-getheight(n->right));
}
node* Dictionary::rightRotate(node *y)
{
node *x=y->left;
node *xr=x->right;

//Update Pointers after rotation
x->right=y;
y->left=xr;

y->height=max(getheight(y->left),getheight(y->right))+1;
x->height=max(getheight(x->left),getheight(x->right))+1;
return x;
}

node* Dictionary::leftRotate(node *y)
{
node *x=y->right;
node *t2=x->left;

x->left=y;
y->right=t2;

y->height=max(getheight(y->left),getheight(y->right))+1;
x->height=max(getheight(x->left),getheight(x->right))+1;

return x;
}

node* Dictionary::insert(node *rnode,int key,string meaning)
{
//1. Normat BST Operation
if(rnode==NULL) //Empty Dictionary
return new node(key,meaning);

if(key<rnode->key)
rnode->left=insert(rnode->left,key,meaning);
else if(key>rnode->key)
rnode->right=insert(rnode->right,key,meaning);
else //equal value key
return rnode;

//2. update height of ancestors
rnode->height=1+max(getheight(rnode->left),getheight(rnode->right));
//3. Get balancing factor
int balance=getbalance(rnode);

//4. perform rotations and return nre root
//LL Case
if(balance>1 && key<rnode->left->key)
return rightRotate(rnode);

//RR Case
if(balance<-1 && key>rnode->right->key)
return leftRotate(rnode);

//LR Case
if(balance>1 && key>rnode->left->key)
{
rnode->left=leftRotate(rnode->left);
return rightRotate(rnode);
}

//RL Case
if(balance<-1 && key<rnode->right->key)
{
rnode->right=rightRotate(rnode->right);
return leftRotate(rnode);
}

return rnode; //no change in root

}
void Dictionary::preorder()
{
preorderRec(root);
}
void Dictionary::postorder()
{
postorderRec(root);
}
void Dictionary::inorder()
{
inorderRec(root);
}
void Dictionary::preorderRec(node *n)
{
if(n)
{
n->print();
preorderRec(n->left);
preorderRec(n->right);
}
}

void Dictionary::inorderRec(node *n)
{
if(n)
{
inorderRec(n->left);
n->print();
inorderRec(n->right);
}
}

void Dictionary::postorderRec(node *n)
{
if(n)
{
postorderRec(n->left);
postorderRec(n->right);
n->print();
}
}
void Dictionary::insertInit(int key,string meaning)
{
root=insert(root,key,meaning);
}
int main() {
Dictionary d;
// d.insertInit("V","Vaibhav");
// d.insertInit("N","Navnath");
// d.insertInit("K","Kumbhar");
// d.insertInit("G","Ganesha");
// d.insertInit("5","Five");

d.insertInit(10,"10");
d.insertInit(20,"20");
d.insertInit(30,"30");
d.insertInit(40,"40");
d.insertInit(50,"50");
d.insertInit(25,"25");
cout<<"\nASCENDING ORDER:";
d.inorder();

cout<<"\nPreorder: ";
d.preorder();

cout<<"\nPostorder: ";
d.postorder();

return 0;

Add a comment
Know the answer?
Add Answer to:
Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry...
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
  • Overview: The goal of this assignment is to implement a simple spell checker using a hash...

    Overview: The goal of this assignment is to implement a simple spell checker using a hash table. You will be given the basic guidelines for your implementation, but other than that you are free to determine and implement the exact classes and methods that you might need. Your spell-checker will be reading from two input files. The first file is a dictionary containing one word per line. The program should read the dictionary and insert the words into a hash...

  • 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...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • Implement an AVL tree stored in a random access file Each node contains an integer key,...

    Implement an AVL tree stored in a random access file Each node contains an integer key, one or more fixed length character strings, a left child reference, a right child reference and a height The format of the file is shown on the next slide The methods you must implement are shown on the followingslides. Duplicate keys cannot be entered in the tree Your implementation MUST NOT load the whole tree in memory. Each operation only makes copies of the...

  • What need to be done What’s given Problem Binary Search Tree Implement the BinarySearchTree class. The...

    What need to be done What’s given Problem Binary Search Tree Implement the BinarySearchTree class. The BinarySearchTree class extends the BinaryTree class. Both can be seen here. Your assignment is to implement all of the abstract methods of the BinaryTree class recursively. They are: . insert iterator (non-recursive) * remove . search You must also implement an Iterator inner class for the BinarySearchTree class. You must submit a modified BinarySearchTree.java file with your source code. Do not submit and do...

  • Using the following definition (List.h file) for a list, implement the member functions (methods) for the...

    Using the following definition (List.h file) for a list, implement the member functions (methods) for the List class and store the implementation in a List.cpp file. Use a doubly linked list to implement the list. Write the client code (the main method and other non-class methods) and put in file driver.cpp. file: List.h typedef int ElementType; class node{ ​ElementType data; ​node * next; node* prev; }; class List { public: List(); //Create an empty list bool Empty(); // return true...

  • This program is in python and thanks fro whoever help me. In this program, you will...

    This program is in python and thanks fro whoever help me. In this program, you will build an English to Hmong translator program. Hmong is a language widely spoken by most Southeast Asian living in the twin cities. The program lets the user type in a sentence in English and then translate it to a Hmong sentence. The program does not care about grammar or punctuation marks. That means your program should remove punctuation marks from the English words before...

  • In this assignment you will implement the second version of your spell checker. Using the randomi...

    In this assignment you will implement the second version of your spell checker. Using the randomized dictionary (random_dictionary.txt) given, read in the dictionary into an array of 26 Binary Search Trees (BST) , one for each letter of the alphabet. Thus the first BST would contain only those words starting with letter 'a', while the last would contain only those words starting with letter 'z'. Then, when you read in the book (oliver.txt), you examine the first character of each...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • Array Processing - Dictionary Program NOTE: Review the parallel array example from class Another method of...

    Array Processing - Dictionary Program NOTE: Review the parallel array example from class Another method of "Sequentially Searching an Array" is covered in the book in Chapter 8. Using Notepad, write psuedocode ONLY for the following situation. Create and load an array with the following 7 values. Add one more word (of your own choosing) for a total of 8 words. biff comely fez mottle peruke bedraggled quisling Create a second array (parallel array). To hold the defintions to these...

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