二进制搜索树打印问题

时间:2017-02-22 02:22:27

标签: c++ algorithm sorting tree binary-search-tree

我正在开发一个图形SFML BST菜单系统。我有测试文件。其中一些给出了预期的输出,但其他的没有。我一直想弄清楚几天,我不明白为什么。这些数字似乎正确地插入到树中,但是具有重复数字的输入似乎打破了树遍历的结构,因为并非所有节点都将被输出。请参阅下面的代码和示例。

我的.h文件中有一个strut等等,我确信它可以正常工作。

有谁可以告诉我为什么其中一些不会遍历所有节点?

输入:1,3,5,6,7,8,9,10 输出:图表看起来像预期的那样,遍历看起来像预期的那样。

输入:3,4,99,3,10,11,10 产出3,3,10,10(不预期)

输入:10,3,10,24,3,20 产出:3,3,10,10,24

//Binary Search Tree Program
#include "BinarySearchTree.h"

using namespace std;

sf::CircleShape shape(50);

BinarySearchTree::BinarySearchTree()
{
    root = NULL;

    if (!font.loadFromFile("font.ttf"))
        cout << "Error loading font!";
}


void BinarySearchTree::loadFile(int num) {
    data.clear();
    string s = to_string(num);
    string text;
    string temp; // Added this line
    ifstream file;
    file.open("files/file" + s + ".txt");

    while (!file.eof())
    {
        getline(file, temp);
        text.append(temp); // Added this line
    }

    std::istringstream ss(text);
    std::string token;
    while (std::getline(ss, token, ',')) {
        std::cout << token << ',';
        std::string myString = token;
        int value = atoi(myString.c_str()); //value = 45 
        data.push_back(value);
    }
}

void BinarySearchTree::passToinsert()
{
    for (std::vector<int>::iterator it = data.begin(); it != data.end(); ++it) {
        insert(*it);
        //cout << *it;
    }
}


void BinarySearchTree::insert(int d)
{
    tree_node* t = new tree_node;
    tree_node* parent;
    t->data = d;
    t->left = NULL;
    t->right = NULL;
    parent = NULL;

    // is this a new tree?
    if (isEmpty()) root = t;
    else
    {
        //Note: ALL insertions are as leaf nodes
        tree_node* curr;
        curr = root;
        // Find the Node's parent
        while (curr)
        {
            parent = curr;
            if (t->data > curr->data) curr = curr->right;
            else curr = curr->left;
        }

        if (t->data < parent->data) {
            parent->left = t;
            cout << "L-" << t->data<<endl;
        }
        else {
            parent->right = t;
            cout << "R-" << t->data << endl;

        }
    }
}


void BinarySearchTree::print_inorder()
{
    inorder(root);
}

void BinarySearchTree::inorder(tree_node* p)
{
    if (p != NULL)
    {
        if (p->left) inorder(p->left);
        cout << " " << p->data << " ";
        if (p->right) inorder(p->right);
    }
    else return;
}

void BinarySearchTree::print_postorder()
{
    postorder(root, 100,0,0,0,0);
}


void BinarySearchTree::postorder(tree_node* p, int indent, int leftNodeCount, int rightNodeCount, int left, int right)
{

    if (p != NULL) {

        if (left = 1) {
            std::string s = std::to_string(p->data);

            nodes[nodeCount].setFont(font);
            nodes[nodeCount].setFillColor(sf::Color::White);
            nodes[nodeCount].setString(s);
            nodes[nodeCount].setPosition(sf::Vector2f(indent, 80 + (leftNodeCount * 80)));
            nodeCount++;
            leftNodeCount++;
        }
        else if (right = 1) {
            std::string s = std::to_string(p->data);

            nodes[nodeCount].setFont(font);
            nodes[nodeCount].setFillColor(sf::Color::White);
            nodes[nodeCount].setString(s);
            nodes[nodeCount].setPosition(sf::Vector2f(indent, 80 + (rightNodeCount * 80)));
            nodeCount++;
            rightNodeCount++;
        }
        else {
            std::string s = std::to_string(p->data);

            nodes[nodeCount].setFont(font);
            nodes[nodeCount].setFillColor(sf::Color::White);
            nodes[nodeCount].setString(s);
            nodes[nodeCount].setPosition(sf::Vector2f(indent, 80 + (nodeCount * 80)));
            nodeCount++;
        }


        if (p->right) {
            postorder(p->right, indent + 80, leftNodeCount, rightNodeCount,left=0,right=1);
        }
        if (p->left) {
            postorder(p->left, indent - 80, leftNodeCount, rightNodeCount, left = 1, right = 0);
        }
    }
}

//Draw sub menu heading text and options to screen
void BinarySearchTree::drawNodes(sf::RenderWindow &window)
{
    window.clear();

    for (int i = 0; i < 10; i++)
    {
        window.draw(nodes[i]);
    }

    window.display();
}

1 个答案:

答案 0 :(得分:0)

实际上,数字没有正确插入树中。主要问题位于insert()的{​​{1}}函数。

在while循环class BinarySearchTree中,左侧和右侧之间的切换由if条件while (curr)管理,假设案例if (t->data > curr->data)连接到左侧。

在while循环之后,左侧和右侧之间的切换由if条件t->data == curr->data管理,假设案例if (t->data < parent->data)连接到右侧。 而不是左侧!!!

解决方案 - 只需在while循环中交换开关条件,如下所示。

t->data == parent->data

而不是:

if (t->data < curr->data) {
    curr = curr->left;
}
else {
    curr = curr->right;
}