Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 二叉搜索树打印问题_C++_Algorithm_Sorting_Tree_Binary Search Tree - Fatal编程技术网

C++ 二叉搜索树打印问题

C++ 二叉搜索树打印问题,c++,algorithm,sorting,tree,binary-search-tree,C++,Algorithm,Sorting,Tree,Binary Search Tree,我正在开发一个图形化的sfmlbst菜单系统。我有测试文件。它们中的一些给出了预期的输出,但另一些没有。这几天我一直在想,我不明白为什么。数字似乎插入到树中是正确的,但具有重复数字的输入似乎破坏了树遍历的结构,因为并非所有节点都将被输出。请参阅下面的代码和示例 在我的.h文件中,我有一个关于支柱等的文件,我确信它工作得很好 有人能告诉我为什么其中一些没有遍历所有节点吗 输入:1,3,5,6,7,8,9,10 输出:图形看起来像预期的,遍历看起来像预期的 输入:3,4,99,3,10,11,10

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

在我的.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();
}
//二进制搜索树程序
#包括“BinarySearchTree.h”
使用名称空间std;
sf::圆形(50);
BinarySearchTree::BinarySearchTree()
{
root=NULL;
如果(!font.loadFromFile(“font.ttf”))

cout事实上,数字没有正确插入到树中。主要问题在于
类BinarySearchTree
insert()
函数

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

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

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

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

在函数
postorder()
中,我怀疑
if(left=1)
else if(right=1)
中存在意外赋值。应该是
if(left==1)
else if(right==1)
。没有正确的输出。正常情况下,
postorder()中存在赋值错误
调用simple
print\u inoorder()
函数时,您的问题肯定会出现。
if (t->data > curr->data) curr = curr->right;
else curr = curr->left;