代码之家  ›  专栏  ›  技术社区  ›  Grant Ronterio

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

  •  -1
  • Grant Ronterio  · 技术社区  · 7 年前

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

    节点在向量中按ID存储和组织。

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

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

    50 21 Tim

    75 22史蒂夫

    30 40埃里克

    20 35玛丽

    100 60朱迪

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

    我运行我的report()函数,发现存储在向量中的所有值都是大的负值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 回复  |  直到 7 年前
        1
  •  0
  •   Ayush Mahajan    7 年前

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