C ++打印二叉树中的所有值

时间:2018-05-03 04:51:47

标签: c++ pointers vector nodes traversal

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

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

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

101 10 Bob

102 11史蒂夫

104 14 Walt

103 12 Lan

105 14 Bill

它会带来它们 101 10 Bob 0,1

02 11 Steve 1,1,

03 12 Lan 1,

104 13 Walt 2,

105 14 Bill 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;

    }
}

提前谢谢!

1 个答案:

答案 0 :(得分:0)

此代码存在很多问题。

int count在标头中初始化。 删除它并在.cpp中初始化它以获得“count is ambiguous”错误。

这主要归功于

using namespace std;

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