Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BinarySearchTree.h"
- #include <iostream>
- // **Constructors **
- std::string inorder_values = "";
- std::string preorder_values = "";
- std::string postorder_values = "";
- std::string insertedWord = "";
- bool valueExists = false;
- BinarySearchTree::BinarySearchTree()
- {
- }
- BinarySearchTree::BinarySearchTree(std::string word)
- {
- Node* node = new Node;
- node->word = word;
- node->left = node->right = nullptr;
- /*Node* node1 = new Node;
- node1->word = "aasd";
- node1->left = node1->right = nullptr;
- Node* node2 = new Node;
- node2->word = "zasd";
- node2->left = node2->right = nullptr;
- node->left = node1;
- node->right = node2;*/
- root = node;
- }
- BinarySearchTree::BinarySearchTree(const BinarySearchTree &rhs)
- {
- }
- BinarySearchTree::BinarySearchTree(const std::vector<std::string> &words)
- {
- }
- //destructor
- BinarySearchTree::~BinarySearchTree()
- {
- }
- // **Methods**
- void insertWord(Node* node, std::string wordValue) {
- if (node == NULL)
- {
- return;
- }
- if (node->word == wordValue) {
- return;
- }
- Node* tempNode = new Node;
- tempNode->word = wordValue;
- tempNode->left = tempNode->right = nullptr;
- if (node->word > wordValue && node->left == NULL) {
- node->left = tempNode;
- return;
- }
- else if (node->word > wordValue && node->left != NULL) {
- insertWord(node->left, wordValue);
- }
- if (node->word < wordValue && node->right == NULL) {
- node->right = tempNode;
- return;
- }
- else if (node->word < wordValue && node->right != NULL) {
- insertWord(node->right, wordValue);
- }
- }
- void BinarySearchTree::insert(std::string word)
- {
- if (root == NULL) {
- Node* node = new Node;
- node->word = word;
- node->left = node->right = nullptr;
- root = node;
- return;
- }
- insertWord(root, word);
- }
- bool nodeExists(Node* node, std::string wordValue)
- {
- if (node == NULL)
- {
- return false;
- }
- if (node->word == wordValue) {
- valueExists = true;
- }
- if (node->left != NULL && !valueExists) {
- nodeExists(node->left, wordValue);
- }
- if (node->right != NULL && !valueExists) {
- nodeExists(node->right, wordValue);
- }
- return valueExists;
- }
- bool BinarySearchTree::exists(std::string word) const
- {
- bool isWordExisting = nodeExists(root, word);
- valueExists = false;
- return isWordExisting; // change this to implement the method, returning true if word exists
- }
- std::string printInorder(Node* node)
- {
- std::string value = "";
- if (node == NULL)
- {
- return value;
- }
- if (node->left != NULL) {
- printInorder(node->left);
- }
- inorder_values += node->word + " ";
- if (node->right != NULL) {
- printInorder(node->right);
- }
- return inorder_values;
- }
- std::string BinarySearchTree::inorder() const
- {
- // change this to return a string representation of the words
- // in the tree inorder.
- if (root == NULL)
- {
- return std::string("");
- }
- std::string value = "";
- // not sure if this 'if' statement is needed
- if (root->left == NULL && root->right == NULL)
- {
- return value;
- }
- value += printInorder(root);
- inorder_values = "";
- return value;
- }
- std::string printPreorder(Node* node)
- {
- std::string value = "";
- if (node == NULL)
- {
- return value;
- }
- preorder_values += node->word + " ";
- if (node->left != NULL) {
- printPreorder(node->left);
- }
- if (node->right != NULL) {
- printPreorder(node->right);
- }
- return preorder_values;
- }
- std::string BinarySearchTree::preorder() const
- {
- // change this to return a string representation of the words
- // in the tree preorder.
- std::string value = "";
- if (root == NULL)
- {
- return value;
- }
- if (root->left == NULL && root->right == NULL)
- {
- value += root->word;
- return value;
- }
- value += printPreorder(root);
- preorder_values = "";
- return value;
- }
- std::string printPostorder(Node* node)
- {
- std::string value = "";
- if (node == NULL)
- {
- return value;
- }
- if (node->left != NULL) {
- printPostorder(node->left);
- }
- if (node->right != NULL) {
- printPostorder(node->right);
- }
- postorder_values += node->word + " ";
- return postorder_values;
- }
- std::string BinarySearchTree::postorder() const
- {
- // change this to return a string representation of the words
- // in the tree postorder.
- std::string value = "";
- if (root == NULL)
- {
- return value;
- }
- if (root->left == NULL && root->right == NULL)
- {
- value += root->word;
- return value;
- }
- value += printPostorder(root);
- postorder_values = "";
- return value;
- }
- // **Operator overloads**
- BinarySearchTree& BinarySearchTree::operator+(std::string word)
- {
- return *this; // returns a reference to the modified tree
- }
- BinarySearchTree& BinarySearchTree::operator=(const BinarySearchTree &rhs)
- {
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement