-3

我正在尝试编写一个对树执行不同功能的程序,到目前为止,除了打印功能外,它们都可以工作。它以前可以工作,但是在尝试解决其他功能中的一些问题时(不弄乱它),现在它们已修复,这个突然不起作用,我不明白为什么。这是我的代码:

主.cpp:

using namespace std;
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "lcrs.h"

int main()
{
char *temp1;
char *temp2;
temp1 = new char;
temp2 = new char;

lcrs tree;

do{
    cout << "LCRS> ";
    cin >> temp1;
    if(strcmp(temp1, "quit") == 0)
    {
        return 0;
    }
    if(strcmp(temp1, "insert") == 0)
    {   cin >> temp2;
        bool error;
        for(int i=0; i<strlen(temp2); i++)
        {
            if(!isdigit(temp2[i]))
            {
                cout << "Error!" << endl;
                error = true;
            }
        }
        if(!error)
        {
            tree.insert(atoi(temp2), tree.root);
        }
    }
    else if(strcmp(temp1, "height") == 0)
    {
        if(tree.root == NULL)
            cout << "-1" << endl;
        else
            cout << tree.getHeight(tree.root) << endl;
    }
    else if(strcmp(temp1, "preorder") == 0)
    {
        cout << "Root is " << tree.root->data << endl;
        tree.print(tree.root);
        cout << "" << endl;
    }
    else if(strcmp(temp1, "search") == 0)
    {
        cin >> temp2;
                bool error;
                for(int i=0; i<strlen(temp2); i++)
             {
                       if(!isdigit(temp2[i]))
                       {
                                cout << "Error!" << endl;
                               error = true;
                     }
                }
               if(!error)
                   {
                         if(tree.search(atoi(temp2), tree.root))
                cout << "true" << endl;
            else
                cout << "false" << endl;
                }

    }
    else
    {
        cout << "Error! " << endl;
    }
}while(strcmp(temp1, "quit") !=0);

return 0;
}

lcrs.h:

using namespace std;
#include <cstdlib>
#include <iostream>

class node{
    public:
    int data;
    node *right;
    node *below;

    node()
    {
        right = NULL;
        below = NULL;
    }
};

class lcrs{
    public:
    node *root;
    bool search(int, node*);
    void print(node*);
    void insert(int, node*&);
    int getHeight(node*);

    lcrs()
    {
        root = NULL;
    }
};

lcrs.cpp:

using namespace std;
#include "lcrs.h"

bool lcrs::search(int x, node *b)
{
    if(b == NULL)
        return false;
    else
    {
        if(b->data == x)
            return true;
        else
        {
            return search(x, b->right) || search(x, b->below);
        }
    }
}

void lcrs::print(node *z)
{
    if(z->below == NULL || z->right != NULL)
    {
        cout << z->data << ",";
        print(z->right);
    }
    else if(z->below != NULL && z->right == NULL)
    {
        cout << z->data << ",";
        print(z->below);
    }
    else if(z->below != NULL && z->right != NULL)
    {
        cout << z->data << ",";
        print(z->below);
        print(z->right);
    }
    else if(z->right == NULL && z->below == NULL)
     {
             cout << z->data << "";
     }


}

void lcrs::insert(int x, node *&a)
{
    if(a == NULL)
    {
        node *newnode;
        newnode = new node;
        newnode->data = x;
        a = newnode;
    }
    else if(a->data < x)
    {
        if(a->right != NULL)
        {
            insert(x, a->right);
        }
        else if(a->below != NULL)
        {
            if(a->below->right != NULL)
            {
                insert(x, a->below->right);
            }
            else
            {
                insert(x, a->below);
            }
        }
        else
        {
            node *n;
            n = new node;
            n->data = x;
            a->below = n;
        }
    }
    else if(a->data > x)
    {
        if(a->below != NULL)
        {
            insert(x, a->below);
        }
        else
        {
            node *n;
            n = new node;
            n->data = x;
            a->right = n;
        }
    }
}
int lcrs::getHeight(node *h)
{
    int height = 0;
    node *n;
    n = new node;
    n = h;
    while(n->below != NULL || n->right != NULL)
    {
        if(n->below != NULL)
        {
            n = n->below;
            height ++;
        }
        else if(n->right != NULL)
        {
            n = n->right;
        }
    }
    return height;
}

我在tree.print(tree.root)函数调用时遇到了段错误。我在函数的最开始放了一个打印语句,但它从来没有做到这一点,所以我对问题出在哪里有点困惑。

非常感谢您的帮助。

4

2 回答 2

0

我找到了问题,这只是一个小错字。(当然是。)感谢所有给出合理答案并真诚尝试提供帮助的人。还有,感谢所有给我sass的人。我很感激在我使用了调试器并且仍然束手无策之后,人们可以提醒我我只是一个低级的 ComSci 学生。非常感谢。

于 2012-11-13T18:23:45.973 回答
0

有很多问题。它可能与您读取输入的方式有关,将字符串填充到包含单个字符的缓冲区中(改用 std::string - 它的存在是有原因的),或者它可能与树的事实有关。 root 可能为 null 并且您正在取消引用它:cout << "Root is " << tree.root->data << endl;

此外,您确实应该在发布代码时尝试减少内容。这有两个目的:它可以帮助您(因为您实际上可能会发现自己出了什么问题,或者至少隔离了故障,因为您正在精简内容)并且它可以帮助我们,因为我们不需要浏览页面和代码页。

于 2012-11-13T18:08:45.990 回答