0% found this document useful (0 votes)
233 views8 pages

Dictionary by Vignesh Prasad V & Vishal Sapatla: Objective

The document describes creating a dictionary data structure using a binary search tree. It defines a class to hold word and meaning pairs, and includes functions for inserting, deleting, searching, printing and loading/saving the tree to a file. The code provided implements these functions on a binary search tree of word-meaning pairs to build a basic dictionary program.

Uploaded by

Manoj Prasad
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
233 views8 pages

Dictionary by Vignesh Prasad V & Vishal Sapatla: Objective

The document describes creating a dictionary data structure using a binary search tree. It defines a class to hold word and meaning pairs, and includes functions for inserting, deleting, searching, printing and loading/saving the tree to a file. The code provided implements these functions on a binary search tree of word-meaning pairs to build a basic dictionary program.

Uploaded by

Manoj Prasad
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy