二叉树打印失败,求解惑

#二叉树打印失败,无报错,求解惑

#BiTree.h

#ifndef BITREE_H
#define BITREE_H
#include<stdio.h>
#include <stdlib.h>
#include <string.h>

typedef char DataType;

typedef struct Node
{
    DataType data;
    struct Node* leftchild;
    struct Node* rightchild;
}BiTreeNode;//节点结构体定义 

void Initiate(BiTreeNode** root)//初始化建立二叉树头节点 
{
    *root = (BiTreeNode*)malloc(sizeof(BiTreeNode));
    (*root)->leftchild = NULL;
    (*root)->rightchild = NULL;
}

BiTreeNode* InsertleftNode(BiTreeNode* curr, DataType x)//左节点插入 
{
    BiTreeNode* s, * t;
    if (curr == NULL) return NULL;
    t = curr->leftchild;
    s = (BiTreeNode*)malloc(sizeof(BiTreeNode));
    s->data = x;
    s->leftchild = s;
    return curr->leftchild;
}

BiTreeNode* InsertrightNode(BiTreeNode* curr, DataType x)//右节点插入 
{
    BiTreeNode* s, * t;
    if (curr == NULL) return NULL;
    t = curr->rightchild;
    s = (BiTreeNode*)malloc(sizeof(BiTreeNode));
    s->data = x;
    s->rightchild = s;
    return curr->rightchild; 
}

void PrintfBiTree(BiTreeNode *root, int n);

#endif

#BiTree.cpp

#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include"BiTree.h"


void PrintfBiTree(BiTreeNode *root, int n)
{//输出二叉树 
    int i;
    if (root == NULL) return;
    PrintfBiTree(root->rightchild, n + 1);
    for (i = 0; i < n - 1; i++) printf("   ");
    if (n > 0)
    {
        printf("--");
        printf("%c\n", root->data);
    }
    PrintfBiTree(root->leftchild, n + 1);
}
    

int main() 
{
    BiTreeNode *root,*p;
    Initiate(&root);
    p = InsertleftNode(root, 'A');
    p = InsertleftNode(p, 'B');
    p = InsertleftNode(p, 'D');
    p = InsertrightNode(p, 'G');
    p = InsertrightNode(root->leftchild, 'C');
    InsertleftNode(p, 'E');
    p = InsertrightNode(p, 'F');
    p = InsertrightNode(p, 'H');
    InsertrightNode(p, 'I');
    //建立节点为10二叉树 
    
    void PrintfBiTree(BiTreeNode* root, int n); 


}


基于Monster 组和GPT的调写:

在你的BiTree.cpp文件中,你在main()函数中定义了PrintfBiTree函数的原型,但没有实际调用该函数来打印二叉树。为了解决这个问题,你需要在main()函数的适当位置调用PrintfBiTree函数。

将你的BiTree.cpp文件的最后几行更改为以下内容:

int main() 
{
    BiTreeNode *root,*p;
    Initiate(&root);
    p = InsertleftNode(root, 'A');
    p = InsertleftNode(p, 'B');
    p = InsertleftNode(p, 'D');
    p = InsertrightNode(p, 'G');
    p = InsertrightNode(root->leftchild, 'C');
    InsertleftNode(p, 'E');
    p = InsertrightNode(p, 'F');
    p = InsertrightNode(p, 'H');
    InsertrightNode(p, 'I');
    //建立节点为10二叉树 
    
    PrintfBiTree(root, 0); // 调用PrintfBiTree函数打印二叉树
 
    return 0;
}

这样,当你运行程序时,main()函数将调用PrintfBiTree函数来打印二叉树。

该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
在你的代码中,出现了以下两个问题:

1、 InsertleftNodeInsertrightNode 函数中,对于新建的节点的左右孩子的赋值有误。应该是将 s 赋值给 t,而不是将 s 赋值给 s,因为 s 表示新建的节点,而 t 表示原来的节点。

修改后的 InsertleftNodeInsertrightNode 函数如下:

BiTreeNode* InsertleftNode(BiTreeNode* curr, DataType x) //左节点插入 
{
    BiTreeNode* s, * t;
    if (curr == NULL) return NULL;
    t = curr->leftchild;
    s = (BiTreeNode*)malloc(sizeof(BiTreeNode));
    s->data = x;
    s->leftchild = NULL;
    s->rightchild = NULL;
    curr->leftchild = s;
    return s;
}

BiTreeNode* InsertrightNode(BiTreeNode* curr, DataType x) //右节点插入 
{
    BiTreeNode* s, * t;
    if (curr == NULL) return NULL;
    t = curr->rightchild;
    s = (BiTreeNode*)malloc(sizeof(BiTreeNode));
    s->data = x;
    s->leftchild = NULL;
    s->rightchild = NULL;
    curr->rightchild = s;
    return s;
}

2、 在 main 函数中,调用 PrintfBiTree 函数时,应该直接使用函数名,而不是在函数名前加上 void

修改后的 main 函数如下:

int main()
{
    BiTreeNode* root, * p;
    Initiate(&root);
    p = InsertleftNode(root, 'A');
    p = InsertleftNode(p, 'B');
    p = InsertleftNode(p, 'D');
    p = InsertrightNode(p, 'G');
    p = InsertrightNode(root->leftchild, 'C');
    InsertleftNode(p, 'E');
    p = InsertrightNode(p, 'F');
    p = InsertrightNode(p, 'H');
    InsertrightNode(p, 'I');
    //建立节点为10二叉树 

    PrintfBiTree(root, 1); // 调用 PrintfBiTree 函数
    return 0;
}

经过以上修改后,你的代码应该就能正确输出二叉树了。


如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7454280
  • 这篇博客也不错, 你可以看下二叉排序树插入,查找,删除操作
  • 除此之外, 这篇博客: 第二次作业中的 二、💥 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 跳转到目录

    关于数组描述错误的是:
    A.数组是一组相同类型元素的集合
    B.数组的下标是从1开始的
    C.数组的下标是从0开始
    D.数组如果初始化,可以不指定数组的大小


  • 您还可以看一下 刘华老师的二级-C语言程序设计课程中的 指针小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    我可以提供一些基本的思路和示例代码帮助您理解如何正确打印二叉树:

    1. 树的结构体

    首先需要定义一个树的结构体:

    typedef struct Node{
        int data;
        struct Node* left;
        struct Node* right;
    }Node, *pNode;
    
    1. 构造树

    假设您已经有了一颗二叉树:

    Node* root;
    //构造树过程...
    
    1. 层次遍历二叉树

    层次遍历二叉树需要用到队列,可以使用STL中的queue。将根节点先入队,每出队一个节点就将它的左右子节点依次入队,直到队列为空。

    void bfs(Node* root){
        queue<Node*> q;
        q.push(root);
        while(!q.empty()){
            pNode node = q.front();
            q.pop();
            printf("%d ", node->data);
            if(node->left){
                q.push(node->left);
            }
            if(node->right){
                q.push(node->right);
            }
        }
    }
    
    1. 输出二叉树

    在层次遍历的基础上,可以输出一个可见的二叉树形状,需要记录节点所在层数,根据层数打印相应数量的空格。下面是示例代码:

    void printTree(Node* root){
        queue<pNode> q;
        vector<vector<int>> res;
        q.push(root);
        int level = 0;
        while(!q.empty()){
            int size = q.size();
            res.push_back(vector<int>());
            for(int i = 0; i < size; i++){
                pNode node = q.front();
                q.pop();
                if(node){
                    res[level].push_back(node->data);
                    q.push(node->left);
                    q.push(node->right);
                } else {
                    res[level].push_back(-1);
                    q.push(NULL);
                    q.push(NULL);
                }
            }
            level++;
        }
        int depth = res.size() - 1;
        for(int i = 0; i <= depth; i++){
            for(int j = 0; j < pow(2, depth - i) - 1; j++){
                printf(" ");
            }
            for(int k = 0; k < res[i].size(); k++){
                if(res[i][k] == -1){
                    printf(" ");
                } else {
                    printf("%d", res[i][k]);
                }
                for(int j = 0; j < pow(2, depth - i + 1) - 1; j++){
                    printf(" ");
                }
            }
            printf("\n");
        }
    }
    

    这样就可以正确打印二叉树了。如果您遇到了具体的问题,可以提出来让我详细帮您解答。