-1

I am currently learning C++ and have been researching BST. I took upon myself a task within a workbook but have been struggling to finish the implementation.

I am not allowed to change the header file or any of the function definitions but I am allowed to add helpers.

I have attempted adding some helpers, but whenever I run the test file, no words are inserted into the tree.

I was hoping someone would be able to help me or give me guidance on finishing the implementation.

Any help on the other functions would be greatly appreciated!

My Header File:

#pragma once

#include <iostream>
#include <string>

// Node of the tree
struct Node
{
    std::string data;

    Node *left = nullptr;

    Node *right = nullptr;
};

class BST
{
private:
    Node *root = nullptr;
public:

    BST();

    BST(std::string word);

    BST(const BST &rhs);

    ~BST();

    void insert(std::string word);

    void remove(std::string word);

    bool exists(std::string word) const;

    std::string inorder() const;

    std::string preorder() const;

    std::string postorder() const;

    bool operator==(const BST &other) const;

    bool operator!=(const BST &other) const;
};

My BST file:

#include "bst.h"
#include <iostream>
#include <string>


using namespace std;

string HelperInsert(Node **root, std::string word)
{
    if (*root == nullptr)
    {
        // Create new node
        *root = new Node;
        // Set new value
        (*root)->data = word;
        // Set branches to nullptr
        (*root)->left = nullptr;
        (*root)->right = nullptr;
    }
    else
        if (word < (*root)->data)
        {
            HelperInsert(&(*root)->left, word);

        }
        else
        {
            if (word > (*root)->data)
            {
                HelperInsert(&(*root)->right, word);
            }
        }
}

void HelperExists(Node *root, string word)
{
    Node *temp = new Node;
    temp = root;
    // Run the loop untill temp points to a NULL pointer.
    while(temp != NULL)
    {
        if(temp->data == word)
        {
            cout<<"True "<<endl;
        }
        // Shift pointer to left child.
        else if(temp->data > word)
            temp = temp->left;
        // Shift pointer to right child.
        else
            temp = temp->right;
    }
    cout<<"\n False";
    return;
}

string Helpwr(Node *root)
{
    if(root == nullptr)
    {
        return "";
    }
    else
    {
        return inorderHelper(root->left)
        + root->data + " "
        + inorderHelper(root->right);
    }

}

void HelperPreOrder(Node *root)
{
    if ( root !=nullptr)
    {
        cout << root->data << " ";
        HelperPreOrder(root->left);
        HelperPreOrder(root->right);
    }
}
void HelperPostorder(Node *root)
{
    if (root!=nullptr)
    {
        HelperPostorder(root->left);
        HelperPostorder(root->right);
        cout << root->data<< endl;
        return;
    }
}


BST::BST()
{
    root= nullptr;
}

// Creates a binary tree by copying an existing tree
BST::BST(const BST &rhs)
{

}


void BST::insert(string word)
{
    HelperInsert(*root, word);
}


void BST::remove(string word)
{

}


bool BST::exists(string word) const
{
    HelperExists(root, word);
    return true;
}


std::string BST::inorder() const
{
    string  res = inorderHelper(root);
    int len = res.length();
    if(len >= 1 && res[len -1] == ' ')
    {
        res.pop_back();
    }
    return res;
}


std::string BST::preorder() const
{
    HelperPreOrder(root);
    return std::string("");
}


std::string BST::postorder() const
{
    HelperPostorder(root);
    return std::string("");
}


bool BST::operator==(const BST &other) const
{
    return true;
}


bool BST::operator!=(const BST &other) const
{
    return true;
}

My test file:

 tree = new BinarySearchTree();
    // Test 3 - insertion check
    tree->insert("fish");
    tree->insert("aardvark");
    tree->insert("zeebra");
    str = tree->inorder();

    if (str != string("aardvark fish zeebra"))
        cerr << "test 3 failed" << endl;
    else
        cout << "Test 3 passed" << endl;

    // Test 4 - exists check

    if (tree->exists("zeebra") && tree->exists("fish") && !tree->exists("bird") && !tree->exists("snake"))
        cout << "Test 4 passed" << endl;
    else
        cerr << "test 4 failed" << endl;
Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
D.Smith
  • 3
  • 1
  • Does your code even compile? The `BST::insert` function passes `*root` to `HelperInsert` (so you're passing a value of type `Node`) but `HelperInsert` expects a `Node**`... – Botje Apr 12 '18 at 11:19

1 Answers1

0

Seems like an issue of not storing the string words correctly:

your insert method is as follows:

void insert(std::string word);

Which means a value of the string is passed, not a reference. In order to store the string, you'll have to create a copy of the string and store it in memory:

string HelperInsert(Node **root, std::string word)
{
    if (*root == nullptr)
    {
        // Create new node
        *root = new Node;
        // Set new value
        (*root).data = new std:string(word); // note the setting of a string pointer here!
        // Set branches to nullptr
        (*root)->left = nullptr;
        (*root)->right = nullptr;
    }
    else
        if (word < (*root)->data)
        {
            HelperInsert(&(*root)->left, word);

        }
        else
        {
            if (word > (*root)->data)
            {
                HelperInsert(&(*root)->right, word);
            }
        }
}

Do not forget to delete the string when removing nodes, in order to prevent mempory leaks. Good luck!

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
  • Thats great! Thanks for the help! So the issue was that I was passing via value and not via reference? – D.Smith Apr 12 '18 at 11:24
  • @D.Smith : seems like that to me. I might be mistaken :) I did not address the rest of the code, you obviously have much work ahead of you, but I am sure you'll be fine. – Rann Lifshitz Apr 12 '18 at 11:25
  • 1
    `std::string` manages its own storage on the heap, you're fully allowed to pass those by copy. – Botje Apr 12 '18 at 11:27
  • @Botje : never been much of a fan of allowing std:string to creates copies of itslef whenever it is passed as parameter..... Had a project once where the memory used up by these copies reached ~200mb..... – Rann Lifshitz Apr 12 '18 at 12:23