分段错误 - 显示树

发布于 2024-08-11 17:26:35 字数 1440 浏览 6 评论 0原文

调用 viewTree(root); 时出现段错误

    struct treeElement {
        unsigned long weight;
        unsigned short id;
        char chr;
        struct treeElement *lchild, *rchild, *parent;
    };

    typedef struct treeElement node;

    node *root;

    //INITIALIZE TREE
    void initTree() {
        root = malloc(sizeof(node));
        currentNYT = root;
    } //initTree

    //VIEW TREE
    void viewTree(node *tree) {
        printf("%5d%5d%5d%5d%5c%lu", tree->id, tree->parent->id, tree->lchild->id, tree->rchild->id, tree->chr, tree->weight);
        viewTree(tree->lchild);
        viewTree(tree->rchild);
    }

//ADD NODE
void addNode(char newNodeChr) {
    node *newNYT, *newExternal;
    newNYT = malloc(sizeof(node));
    newNYT->id=maxNodes-idCount; idCount++;
    newNYT->chr='\0';
    newNYT->weight=0;
    newNYT->parent=currentNYT;
    newNYT->lchild=newNYT->rchild=NULL;
    newExternal = malloc(sizeof(node));
    newExternal->id=maxNodes-idCount;
    newExternal->chr=newNodeChr;
    newExternal->weight=1;
    newExternal->parent=currentNYT;
    newExternal->lchild=newExternal->rchild=NULL;
    currentNYT->lchild = newNYT;
    currentNYT->rchild = newExternal;
    currentNYT=newNYT;
} //addNode

int main()
{
    initTree();
    addNode('a');
    addNode('b');
    viewTree(root);

    getchar();

    return 0;
}

I get a segfault when calling viewTree(root);

    struct treeElement {
        unsigned long weight;
        unsigned short id;
        char chr;
        struct treeElement *lchild, *rchild, *parent;
    };

    typedef struct treeElement node;

    node *root;

    //INITIALIZE TREE
    void initTree() {
        root = malloc(sizeof(node));
        currentNYT = root;
    } //initTree

    //VIEW TREE
    void viewTree(node *tree) {
        printf("%5d%5d%5d%5d%5c%lu", tree->id, tree->parent->id, tree->lchild->id, tree->rchild->id, tree->chr, tree->weight);
        viewTree(tree->lchild);
        viewTree(tree->rchild);
    }

//ADD NODE
void addNode(char newNodeChr) {
    node *newNYT, *newExternal;
    newNYT = malloc(sizeof(node));
    newNYT->id=maxNodes-idCount; idCount++;
    newNYT->chr='\0';
    newNYT->weight=0;
    newNYT->parent=currentNYT;
    newNYT->lchild=newNYT->rchild=NULL;
    newExternal = malloc(sizeof(node));
    newExternal->id=maxNodes-idCount;
    newExternal->chr=newNodeChr;
    newExternal->weight=1;
    newExternal->parent=currentNYT;
    newExternal->lchild=newExternal->rchild=NULL;
    currentNYT->lchild = newNYT;
    currentNYT->rchild = newExternal;
    currentNYT=newNYT;
} //addNode

int main()
{
    initTree();
    addNode('a');
    addNode('b');
    viewTree(root);

    getchar();

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最舍不得你 2024-08-18 17:26:35

根节点有父节点吗?子叶节点是否有左子节点和右子节点?

我认为您的大部分问题在于您的 printf 语句 - 在尝试打印它们的 ids。在其中添加一些 if 语句,看看是否有帮助。

Does the root node have a parent? Do the child leaf nodes have left and right children?

I think most of your problem lies in your printf statement - you don't check whether or not any of the objects you're accessing actually exist before you try to print their ids. Add some if statements in there and see if it helps.

季末如歌 2024-08-18 17:26:35

在您的 viewTree(node *tree) 中,您没有检查 tree 是否为 null 。当您在 treenull 时尝试访问 tree->id 时,就会出现段错误。

最终将在递归调用中为子树传递null

编辑:通常,每次需要访问对象的成员时,您都会检查 null 。因此,在读取 tree->id 之前使用 tree != null ,在读取 tree- 之前使用 tree->lchild != null 。必须确保>lchild->id

In your viewTree(node *tree) you are not checking if tree is null or not. Definite recipe for segfault when you try to access tree->id when tree is null.

null will be passed for a subtree in a recursive call eventually.

EDIT: In general you have check for null every time you need to access a member of an object. So, tree != null before reading tree->id and tree->lchild != null before reading tree->lchild->id must be ensured.

薔薇婲 2024-08-18 17:26:35

不要只分配根节点,还要初始化它,特别是指向兄弟节点和父节点的指针(将它们设置为 NULL)。添加节点时使用未初始化的指针。

Don't just allocate the root node, but initialize it, especially the pointers to siblings and to parent (set them to NULL). You are using the uninitialized pointers when adding nodes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文