-1

I am tasked with storing a binary tree within a vector. Within each node is stored an int ID, int Age, and a string name.

The nodes are stored and organized within the vector by ID.

When storing the binary tree within a vector, I am using the algorithm 2i and 2i+1 to dictate a node's left and right child respectively.

I have managed to create an insert method that I believe satisfies these conditions, however for some reason, when attempting to print the values of my vector, I appear to get negative values. For this particular example, I insert the following values

50 21 Tim

75 22 Steve

30 40 Eric

20 35 Mary

100 60 Judy

After inserting these four values, I attempt to use my find() method to find Eric, in which my program returns "Not Found!"

I run my report() function to find that all the values stored within my vector are large negative valued IDs.

Is there a particular reason for this? Find() Report()

Here is my code.

#include "BinaryTree.h"
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int index = 0;

struct Node
{
    int ID;
    int age;
    string name;

    Node()
    {

    }

    Node(int id, int Age, string nm)
    {
        this->ID = id;
        this->age = Age;
        this->name = nm;
    }
};

vector<Node> binaryTree(30);


BST::BST()
{

}



void BST::start()
{
    int choice;


    cout << "What would you like to do?" << endl;
    cout << "1. Add a node to the tree" << endl;
    cout << "2. Delete a node from the tree" << endl;
    cout << "3. Find a node in the tree" << endl;
    cout << "4. Report the contents of the tree" << endl;
    cout << "5. Exit program" << endl;

    cin >> choice;

    if (choice == 1)
    {
        insert();
    }

    if (choice == 2)
    {
        Delete();
    }

    if (choice == 3)
    {
        find();
    }

    if (choice == 4)
    {
        report();
    }


}


void BST::insert()
{
    int ID;
    int AGE;
    string NAME;
    int root = 1;

    bool success = false;
    cout << "Please enter the ID number, age and name:" << endl;

    do
    {
        cin >> ID >> AGE >> NAME;
    } while (ID < 0);

    Node *tree = new Node(ID, AGE, NAME);


    if (index = 0)
    {
        binaryTree[1] = *tree;
    }

    if (index > 0)
    {
        do
        {
            if (tree->ID > binaryTree.at(root).ID)
            {
                root = 2 * root + 1;

            }

            if (tree->ID < binaryTree.at(root).ID)
            {
                root = 2 * root;
            }

            if (binaryTree.at(root).ID == NULL)
            {
                binaryTree.at(root) = *tree;
                success = true;
            }
        } while (!success);
    }

    index++;
    delete tree;

    start();
}





void BST::Delete()
{
    int input_id;
    cout << "What is the ID of the person to be deleted" << endl;
    cin >> input_id;

    for (unsigned int i = 0; i < binaryTree.size(); i++)
    {
        if (input_id == binaryTree.at(i).ID)
            binaryTree.erase(binaryTree.begin() + i);


    }
    cout << " " << endl;
    start();
}


void BST::find()
{
    int key;
    bool found = 0;

    cout << "What's the ID?" << endl;
    cout << " " << endl;

    cin >> key;

    for (unsigned int i = 0; i < binaryTree.size(); i++)
    {
        if (binaryTree.at(i).ID == key)
        {
            cout << "The ID is " << binaryTree.at(i).ID << endl;
            cout << "The age ID " << binaryTree.at(i).age << endl;
            cout << "The name is " <<binaryTree.at(i).name << endl;
            cout << " " << endl;

            found = true;

        }
        if (found == false)
        {
            cout << "Not found." << endl;
            cout << "" << endl;
            break;
        }
    }
    start();
}


void BST::report()
{

    cout << "The contents of the tree are" << endl;
    cout << " " << endl;

    for (unsigned int i = 0; i < binaryTree.size(); i++)
    {
        int level = 0;
        if (i == 0) level = 0;
        if (i == 1 || i == 2) level = 1;
        if (i >= 3 && i <= 6) level = 2;
        if (i >= 7 && i <= 14) level = 3;
//TODO complete list
        cout << binaryTree.at(i).ID << " " << binaryTree.at(i).age << " " << &binaryTree.at(i).name << " " << level << endl;

    }
}

Would appreciate the suggestions/help!

Thanks!

  • 1
    Edit your question to include a [mcve]. – Sid S May 05 '18 at 04:11
  • Do you know about the existence of a debugger? If not, you really need to learn how to use one. A debugger can easily help you solve problems like these. – eesiraed May 05 '18 at 04:40
  • @FeiXiang I know it exists, but quite honestly I'm not familiar about how to use it. – Grant Ronterio May 05 '18 at 04:46
  • The best way to learn how to use one is probably a [good book](https://stackoverflow.com/q/388242/9254539). There are also some resources near the bottom of [this page](http://idownvotedbecau.se/nodebugging/), including how to use GDB from the command line to debug a program. Although it's much easier to use an IDE which communicates with the debugger for you and provides a GUI so you don't have to type the commands into GDB. Please ignore the title of the page. You showed your effort to debug the code so I didn't downvote. – eesiraed May 05 '18 at 04:51

1 Answers1

0

I think issues is with indexing here In insert() you created binary tree with root at index in but in report() function you started output from index 0. I have no idea what binaryTree.at(int) do.

But in find() your error is due to the fact that you have included if(found == 0) inside the loop. This means that it will break the loop if 1st element of tree is not the element you are searching. Use this code instead

void BST::find()
{
    int key;
    bool found = 0;

    cout << "What's the ID?" << endl;
    cout << " " << endl;

    cin >> key;

    for (unsigned int i = 0; i < binaryTree.size(); i++)
    {
        if (binaryTree.at(i).ID == key)
        {
            cout << "The ID is " << binaryTree.at(i).ID << endl;
            cout << "The age ID " << binaryTree.at(i).age << endl;
            cout << "The name is " <<binaryTree.at(i).name << endl;
            cout << " " << endl;

            found = true;

        }
    }
    if (found == false)
    {
        cout << "Not found." << endl;
        cout << "" << endl;
    }
    start();
}
Ayush Mahajan
  • 639
  • 1
  • 5
  • 14