首页 > 解决方案 > C++ 打印二叉树中的所有值

问题描述

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

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

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

101 10 鲍勃

102 11 史蒂夫

104 14 沃尔特

103 12 局域网

105 14 比尔

这将使他们成为 101 10 Bob 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;

    }
}

先感谢您!

标签: c++pointersvectornodestraversal

解决方案


这段代码有很多问题。

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

这主要是由于

using namespace std;

由于 count 是该命名空间中的一个项目。


推荐阅读