Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <iostream>
#include<cstring>
#include <fstream>
class BinTreeNode
{
public:
BinTreeNode(std::string value,int frecv)
{
this->value = value;
this->frecv = frecv;
this->left = NULL;
this->right = NULL;
}
int frecv;
std::string value;
BinTreeNode* left;
BinTreeNode* right;
};
BinTreeNode* tree_insert(BinTreeNode* tree, std::string word, int frecv)
{
if (tree == NULL)
tree = new BinTreeNode(word, frecv);
else if (word<tree->value)
if (tree->left == NULL)
tree->left = new BinTreeNode(word, frecv);
else
tree_insert(tree->left, word, frecv);
else if (tree->right == NULL)
tree->right = new BinTreeNode(word, frecv);
else
tree_insert(tree->right, word, frecv);
return tree;
}
void preorder(BinTreeNode* tree)
{
std::cout << tree->value << ":"<<tree->frecv<<std::endl;
if (tree->left != NULL)
preorder(tree->left);
if (tree->right != NULL)
preorder(tree->right);
}
bool SearchNode(BinTreeNode* tree, std::string word)
{
if(tree == NULL)
return false;
else if(tree->value == word)
return true;
else if(word <= tree->value)
return SearchNode(tree->left,word);
else return SearchNode(tree->right,word);
}
BinTreeNode* Frequency(BinTreeNode* tree, std::string word, int frecv)
{
if(tree->value == word)
tree->frecv=frecv+1;
else if(word <= tree->value)
return Frequency(tree->left,word, frecv);
else return Frequency(tree->right,word, frecv);
return tree;
}
int main()
{
int frecv;
std::string word;
std::ifstream fin("paragraph.txt");
fin>>word;
BinTreeNode* t = tree_insert(0, word,1);
while(fin>>word)
if(SearchNode(t,word)==true)
Frequency(t,word,frecv);
else
{
frecv=1;
tree_insert(t, word,frecv);
}
preorder(t);
std::cout<<"Finding a word:";
std::cin>>word;
if(SearchNode(t,word)==true)
std::cout<<"Yes"<<std::endl;
else std::cout<<"No"<<std::endl;
return 0;
}