【问题标题】:C++ Printing All Values in a Binary TreeC++ 打印二叉树中的所有值
【发布时间】:2018-10-13 07:15:44
【问题描述】:

所以我创建了一个存储在数组中的二叉搜索树。此二叉搜索树 (BST) 存储用户输入的 ID、年龄和姓名,然后将其放入按 ID 升序排序的数组中。

我正在尝试编写一个循环遍历数组的函数报告,打印每个节点的 ID、年龄、名称和如果表示为二叉树,它们将处于的级别。

例如,如果我有这些特定的节点

101 10 鲍勃

102 11 史蒂夫

104 14 沃尔特

103 12 局域网

105 14 比尔

这会让他们成为 101 10 鲍勃 0, 1

02 11 史蒂夫 1, 1

03 12 局域网 1,

104 13 沃尔特 2,

105 14 法案 2,

但是,由于某种原因,当尝试使用我的报告功能打印这个特定示例时,我得到了奇怪的负数和大量以前未插入的附加节点。

是不是我做错了什么?

编辑:我不再将 BST 数组大小初始化为 30,但是,现在报告不再打印任何内容。我是初学者,所以我对 C++ 的了解相当少。

这是我的代码。

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

using namespace std;

int count = 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;


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 == 3)
    {
        find();
    }

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


}

void BST::insert()
{

    int ID;
    int AGE;
    string NAME;
    cout << "Please enter the ID number, age and name" << endl;
    cin >> ID >> AGE >> NAME;

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

    if (count == 0)
    {
        binaryTree.push_back(*tree);
        count++;
    }

    if (count > 0)
    {
        if ((binaryTree.at(count - 1).ID) < ID)
        {
            binaryTree.push_back(*tree);
            count++;
        }
    }


    if (count > 0)
    {
        if ((binaryTree.at(count - 1).ID) > ID)
        {
            Node *temp = new Node();
            *temp = binaryTree.at(count - 1);
            binaryTree.at(count - 1) = *tree;

            binaryTree.at(count) = *temp;
            count++;
        }
    }
    cout << "Added! Size: " << binaryTree.size() << endl;

    start();


}


void BST::Delete()
{

}


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 == 2 || i == 3) level = 1;
        if (i >= 4 && i <= 7) level = 2;
        if (i >= 8 && i <= 15) level = 3;
        cout << binaryTree.at(i).ID << " " << binaryTree.at(i).age << " " << binaryTree.at(i).name << " " << level << endl;

    }
}

提前谢谢你!

【问题讨论】:

  • 您的 binaryTree 向量由 30 个元素构成。你用了多少?从 30 中减去该数字,您将获得看到“奇怪的负值”的次数。也许……
  • 请提取minimal reproducible example。它应该是一个文件,不需要用户输入。很可能你会自己发现错误。
  • @user2618142 如果我可以问的话,这会造成什么问题?
  • @DeiDei 已修复,但现在出现不打印任何内容的问题。

标签: c++ pointers vector nodes traversal


【解决方案1】:

这段代码有很多问题。

int 计数在标题中初始化。 将其删除并在 .cpp 中对其进行初始化以获取“计数不明确”错误。

这主要是由于

using namespace std;

因为 count 是该命名空间中的一个项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-04
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    相关资源
    最近更新 更多