#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;
Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem: Each entry...
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 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 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, 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 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 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 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 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: 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 "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...