You are on page 1of 8

Dictionary by Vignesh Prasad V & Vishal Sapatla

Objective:
To create a dictionary to insert, delete, save and to search a word in it using trees.

Algorithm:
Create an class to hold the word and meaning, so that the data in the tree is of the data type of that class. nsertion: nsert the elements into the binary tree of the order data type by chec!ing the word member of the class. f the word is smaller it will be inserted as left child else right child. "eletion: #o child: f the word to be deleted doesn$t have a child just remove it. %ingle child: %wap parent and child and remove it. "ouble child: &emove that element and put its right child in that place. %earching: %imilar to insertion just chec! whether it is e'ual or not. %aving: %ave the content of the tree in a file in infi( form, so that when we retrieve it, it will be in an ordered way.

Code:
//Order.h #include <string> #include <iostream> #include <fstream> #include <sstream> using namespace std; class Order { string word_; string meaning_; public: Order(const string word ! ""# const string meaning_(meaning$

meaning ! ""$: word_(word$#

{ % Order { operator!(const Order word_ ! &.word_; meaning_ ! &.meaning_; return 'this; % string word($ const { return word_; % string meaning($ const { return meaning_; % &oid setmeaning(const string &$ { meaning_ ! &; % bool operator!!(const Order &$ const { return word_ !! &.word_; % bool operator(!(const Order &$ const { return word_ (! &.word_; % bool operator<(const Order &$ const { return word_ < &.word_; % bool operator>(const Order &$ const { return word_ > &.word_; % bool operator<!(const Order &$ const { return word_ <! &.word_; % bool operator>!(const Order &$ const { return word_ >! &.word_; % %; ostream operator<<(ostream s# const Order o$ { s << o.word($ << ":" << o.meaning($ << endl; return s; % //)ree.h #include <string> #include <iostream> #include <fstream> &$

#include <sstream> using namespace std; class Order { string word_; string meaning_; public: Order(const string word ! ""# const string meaning_(meaning$ { % Order { operator!(const Order word_ ! &.word_; meaning_ ! &.meaning_; return 'this; % string word($ const { return word_; % string meaning($ const { return meaning_; % &oid setmeaning(const string { meaning_ ! &; % bool operator!!(const Order bool operator(!(const Order bool operator<(const Order bool operator>(const Order bool operator<!(const Order bool operator>!(const Order %; ostream operator<<(ostream &$

meaning ! ""$: word_(word$#

&$

&$ const { return word_ !! &.word_; % &$ const { return word_ (! &.word_; % &$ const { return word_ < &.word_; % &$ const { return word_ > &.word_; % &$ const { return word_ <! &.word_; % &$ const { return word_ >! &.word_; % s# const Order o$

{ s << o.word($ << ":" << o.meaning($ << endl; return s; % //dictionar*.cpp #include ")ree.h" #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; Order)ree tree; &oid load($ { ifstream m*file("dict.t+t"# ios::in$; if ((m*file.is_open($$ { cout << "file ,dict.t+t, does not e+it-n"; % else if (m*file.is_open($$ { string line; while (getline(m*file# line$$ { stringstream sstrm(line$; string word; string meaning; if(getline(sstrm# word#,:,$$ { sstrm >> meaning; cout <<word<<":"<< meaning <<endl; tree.insert(Order(word# meaning$$; % % % %

&oid insert($ { cout << ".nsert a word into the tree-n" "/nter a 0ord:-n <word> : <meaning> "; string word; string meaning; cin >> word;cin >> meaning; tree.insert(Order(word# meaning$$; cout <<word<<":"<< meaning <<endl; % &oid remo&e($ { cout << "1emo&e a word" << endl; cout << "/nter word: "; string word; cin >> word; cout << tree.remo&e(word$ << endl; % &oid search($ { cout << "2earch for a word" << endl; cout << "/nter word:"; string word; cin >> word; Order 'foo ! tree.search(word$; if (foo$ cout << 'foo << endl; else cout << "word was not founded."; % &oid print(${ cout << "3rint the words" << endl; cout << tree; % &oid out($ { fstream file("dict.t+t"# ios::out$; file << tree;

file.close($; cout << "O4)34) 5.6/ 71/8)/9 : e+ample.t+t" << endl; % int menu($ { cout << "-n;. .nsert a word-n" "<. 1emo&e a word-n" "=. 2earch for a word-n" ">. 3rint the words-n" "?. 6oad a new file-n" "@. 2a&e the file-n" "A. Buit-n"; return C; % int main($ { int +; load($; menu($; bool done ! false; do { cout<<"enter menu number: ";cin>>+; switch (+$ { case ;: insert($; breaD; case <: remo&e($; breaD; case =: search($; breaD; case >: print($;

breaD; case ?: load($; breaD; case @: out($; breaD; case A: done ! true; breaD; % % while ((done$; %

Output:

You might also like