插入后基于矢量的二叉树中的C ++打印值

时间:2018-05-05 04:02:06

标签: c++ pointers vector iteration cout

我的任务是在向量中存储二叉树。在每个节点中存储一个int ID,int Age和一个字符串名称。

节点通过ID在矢量中存储和组织。

在向量中存储二叉树时,我使用算法2i和2i + 1分别指示节点的左右孩子。

我设法创建了一个插入方法,我相信满足这些条件,但是出于某种原因,当试图打印我的向量的值时,我似乎得到负值。对于此特定示例,我插入以下值

50 21蒂姆

75 22史蒂夫

30 40 Eric

20 35玛丽

100 60 Judy

插入这四个值后,我尝试使用find()方法查找Eric,我的程序返回“Not Found!”。

我运行我的report()函数,发现我的vector中存储的所有值都是大的负值ID。

这有什么特别的原因吗? Find() Report()

这是我的代码。

#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;

    }
}

非常感谢建议/帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为问题在于索引 在insert()中,您创建了二进制树,其根目录为索引,但在report()函数中,您从索引0开始输出。我不知道binaryTree.at(int)做了什么。

但在find()中,您的错误是由于您在循环中包含if(found == 0)这一事实。这意味着如果树的第一个元素不是您正在搜索的元素,它将中断循环。请改用此代码

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();
}