【问题标题】:Binary Search Tree Printing Issue二叉搜索树打印问题
【发布时间】:2017-02-22 02:22:27
【问题描述】:

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

我的 .h 文件中有一个支柱等,我很确定它可以正常工作。

谁能告诉我为什么其中一些没有遍历所有节点?

输入: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();
}

【问题讨论】:

  • 在函数postorder() 中,我怀疑if (left = 1)else if (right = 1) 中有意外分配。应该是if (left == 1)else if (right == 1)
  • Nope 仍然没有给出正确的输出。
  • 正常,是postorder()中的赋值错误,调用简单的print_inorder()函数时肯定会出现你的问题。

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


【解决方案1】:

事实上,数字没有正确插入到树中。主要问题出在class BinarySearchTreeinsert()函数中。

在while循环while (curr)中,左右切换由if-condition if (t-&gt;data &gt; curr-&gt;data)管理,假设caset-&gt;data == curr-&gt;data连接到左侧。

在 while 循环之后,左右切换由 if 条件 if (t-&gt;data &lt; parent-&gt;data) 管理,假设 case t-&gt;data == parent-&gt;data 连接到右侧。 而不是左侧!!!

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

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

代替:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多