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 "binTree.h"
BinTree::BinTree() {
root = nullptr;
}
void BinTree::insert(const std::string &text) {
root = insert(root, text);
}
BinTree::binTreeNode *BinTree::insert(binTreeNode *tree, const std::string &text) {
if (tree == nullptr) {
tree = new binTreeNode();
tree->text = text;
tree->count = 1;
tree->left = tree->right = nullptr;
} else if (tree->text == text) {
++(tree->count);
} else if (Helper::firstAbc(text, tree->text)) {
tree->left = insert(tree->left, text);
} else {
tree->right = insert(tree->right, text);
}
return tree;
}
bool BinTree::contains(const std::string &text) {
return contains(root, text);
}
bool BinTree::contains(binTreeNode *tree, const std::string &text) {
if (!tree) {
return false;
}
std::cout << tree->text << " -> ";
if (tree->text == text) {
std::cout << "[FOUND!]" << std::endl;
return true;
}
if (Helper::firstAbc(text, tree->text)) {
return contains(tree->left, text);
}
else {
return contains(tree->right, text);
}
return false;
}
int BinTree::getCount(const std::string &text) {
return contains(root, text);
}
int BinTree::getCount(binTreeNode *tree, const std::string &text) {
if (!tree) {
return 0;
}
if (tree->text == text) {
std::cout << "found" << std::endl;
return tree->count;
}
if (Helper::firstAbc(text, tree->text)) {
return contains(tree->left, text);
}
else {
return contains(tree->right, text);
}
return false;
}
void BinTree::inOrder() {
inOrder(root);
}
void BinTree::preOrder() {
preOrder(root);
}
void BinTree::postOrder() {
postOrder(root);
}
void BinTree::inOrder(binTreeNode *tree) {
if (tree) {
inOrder(tree->left);
std::cout << tree->text << ": " << tree->count << std::endl;
inOrder(tree->right);
}
}
void BinTree::preOrder(binTreeNode *tree) {
if (tree) {
std::cout << tree->text << ": " << tree->count << std::endl;
preOrder(tree->left);
preOrder(tree->right);
}
}
void BinTree::postOrder(binTreeNode *tree) {
if (tree) {
postOrder(tree->left);
postOrder(tree->right);
std::cout << tree->text << ": " << tree->count << std::endl;
}
}