求救 测试了这nulltrp不会解决

#include #include #define MaxQueueSize 20 typedef char Data; typedef struct Node { Data sj; struct Node* rchild; struct Node* lchild; }Tree; typedef struct { Data queue[MaxQueueSize]; int rear; //队尾指针 int front; //队头指针 int count; //计数器 }SeqCQueue; void QueueInitiate(SeqCQueue* Q) { Q->rear = 0; Q->front = 0; Q->count = 0; } void QueueAppend(SeqCQueue* Q, Data x) { if (Q->count > 0 && Q->rear == Q->front) { printf("队列已满无法插入! \n"); //return 0; } else { Q->queue[Q->rear] = x; Q->rear = (Q->rear + 1) % MaxQueueSize; Q->count++; //return 1; } } void QueueDelete(SeqCQueue* Q, Data* d) { if (Q->count == 0) { printf("队列已空无数据元素出队列! \n"); //return 0; } else { *d = Q->queue[Q->front]; Q->front = (Q->front + 1) % MaxQueueSize; Q->count--; //return 1; } } //初始化建立二叉树的头结点 void Initiate(Tree** root) { *root = (Tree*)malloc(sizeof(Tree)); (*root)->lchild = NULL; (*root)->rchild = NULL; } Tree* InsertLeftNode(Tree* curr, Data x) { Tree* s, *t; if (curr == NULL) return NULL; t = curr->lchild; //保存原curr所指结点的左子树指针 s = (Tree*)malloc(sizeof(Tree)); s->sj = x; s->lchild = t; //新插入结点的左子树为原curr的左子树 s->rchild = NULL; curr->lchild = s; //新结点为curr的左子树 return curr->lchild; } Tree* InsertRightNode(Tree* curr, Data x) { Tree* s, *t; if (curr == NULL) return NULL; t = curr->rchild; //保存原curr所指结点的右子树指针 s = (Tree*)malloc(sizeof(Tree)); s->sj = x; s->rchild = t; //新插入结点的右子树为原curr的右子树 s->lchild = NULL; curr->rchild = s; //新结点为curr的右子树 return curr->rchild; } int chanci(Tree* tree, SeqCQueue* q, Data* d) { if ((tree->sj )== NULL) { return 0; } else { //void QueueAppend(SeqCQueue * Q, Data x); QueueAppend(q, tree->sj); //void QueueDelete(SeqCQueue * Q, Data * d); QueueDelete(q, d); printf("%c ", *d);int temp1; temp1 = chanci(tree->lchild, q, d); int temp2; temp2 = chanci(tree->rchild, q, d); return 1 + temp1+ temp2; } } //输出树T的深度 int deep(Tree* tree) { if (tree == NULL || tree->sj == NULL) { return 0; } else { return 1 + deep(tree->lchild); } } //叶子 int yezi(Tree* tree) { if (tree == NULL || tree->sj == NULL) { return 1; } else { return 0 + yezi(tree->lchild) + yezi(tree->rchild); } } int noyezi(Tree* tree) { if (tree->sj = NULL) { return 0; } else { return 1 + yezi(tree->lchild) + yezi(tree->rchild); } } int main() { //建立一个二叉树 Tree* root, *p; SeqCQueue* Q; SeqCQueue Qdate; Q = &Qdate; Data d; void Initiate(Tree **root); void QueueInitiate(SeqCQueue * Q); QueueInitiate(Q); Initiate(&root); root->sj = ' '; p = InsertLeftNode(root, 'A'); p = InsertLeftNode(p, 'B'); InsertRightNode(p, 'E'); InsertRightNode(p->rchild, 'K'); InsertLeftNode(p->rchild, 'J'); p = InsertLeftNode(p, 'D'); InsertLeftNode(p, 'H'); p = InsertRightNode(p, 'I'); p = InsertRightNode(root->lchild, 'C'); InsertLeftNode(p, 'F'); InsertRightNode(p, 'G'); printf("%c\n", root->lchild->sj); printf("%c\n", root->lchild->lchild->sj); //层次遍历 int chanci(Tree *tree, SeqCQueue *q, Data *d); int n; n = chanci(root->lchild, Q, &d); printf("%d\n", n); /*//输出树T的深度; int deep(Tree * tree); int m; m = deep(root->lchild); printf("深%d\n", m); //输出树T的叶子结点和非叶子结点 int yezi(Tree * tree); int noyezi(Tree * tree); int y, no; y = yezi(root->lchild); printf("叶子%d ", y); no = noyezi(root->lchild); printf("非叶子%d ", no);*/ } chanci函数中tree->sj那有个nulltrp 还有递归拿不能正常把二叉树全部递归

请您修改问题,使用代码段控件粘贴代码。要不然这个代码格式真没有办法帮您去看。

函数chanci里第一各if语句要先检测条件tree == NULL。缺少这各条件导致 core dump。运行出了结果。你在继续调试吧。

int chanci(Tree* tree, SeqCQueue* q, Data* d) 

    if (tree == NULL || tree->sj == 0) 
    { 
        return 0; 
    } 
    else 
    { 

.....

#include <stdio.h>
#include <stdlib.h>

#define MaxQueueSize 20

typedef char Data;

typedef struct Node {
    Data sj;
    struct Node * rchild;
    struct Node * lchild;
} Tree;

typedef struct {
    Data queue[MaxQueueSize];
    int rear; //队尾指针 
    int front; //队头指针 
    int count; //计数器 
} SeqCQueue; 

void QueueInitiate(SeqCQueue* Q) 
{ 
    Q->rear = 0; 
    Q->front = 0; 
    Q->count = 0; 
}

void QueueAppend(SeqCQueue* Q, Data x) 
{ 
    if (Q->count > 0 && Q->rear == Q->front) 
    {
        printf("队列已满无法插入! \n"); 
        //return 0; 
    } 
    else 
    { 
        Q->queue[Q->rear] = x;
        Q->rear = (Q->rear + 1) % MaxQueueSize; 
        Q->count++; //return 1; 
    } 
} 

void QueueDelete(SeqCQueue* Q, Data* d) 
{ 
    if (Q->count == 0) 
    {
        printf("队列已空无数据元素出队列! \n"); 
        //return 0; 
    } 
    else 
    { 
        *d = Q->queue[Q->front];
        Q->front = (Q->front + 1) % MaxQueueSize; 
        Q->count--; 
        //return 1; 
    } 
} 

//初始化建立二叉树的头结点 
void Initiate(Tree** root) 
{ 
    *root = (Tree*)malloc(sizeof(Tree)); 
    (*root)->sj = 0;
    (*root)->lchild = NULL; 
    (*root)->rchild = NULL; 
} 

Tree* InsertLeftNode(Tree* curr, Data x) 
{
    Tree* s, *t; 
    
    if (curr == NULL) 
        return NULL; 
    
    t = curr->lchild; //保存原curr所指结点的左子树指针 
    s = (Tree*)malloc(sizeof(Tree)); 
    s->sj = x; 
    s->lchild = t; //新插入结点的左子树为原curr的左子树 
    s->rchild = NULL; 
    curr->lchild = s; //新结点为curr的左子树 
    return curr->lchild; 
    
}

Tree* InsertRightNode(Tree* curr, Data x)
{ 
    Tree* s, *t; 
    if (curr == NULL) 
        return NULL; 
    
    t = curr->rchild; //保存原curr所指结点的右子树指针 
    s = (Tree*)malloc(sizeof(Tree));
    s->sj = x; 
    s->rchild = t; //新插入结点的右子树为原curr的右子树 
    s->lchild = NULL; 
    curr->rchild = s; //新结点为curr的右子树 
    return curr->rchild; 
    
}

int chanci(Tree* tree, SeqCQueue* q, Data* d) 
{ 
    if (tree == NULL || tree->sj == 0) 
    { 
        return 0; 
    } 
    else 
    { 
        //void QueueAppend(SeqCQueue * Q, Data x);
        QueueAppend(q, tree->sj);
        //void QueueDelete(SeqCQueue * Q, Data * d); 
        QueueDelete(q, d); 
        printf("%c ", *d);
        int temp1; 
        temp1 = chanci(tree->lchild, q, d); 
        int temp2; 
        temp2 = chanci(tree->rchild, q, d); 
        return 1 + temp1+ temp2; 
        
    } 
    
} 

//输出树T的深度 
int deep(Tree* tree) 
{ 
    if (tree == NULL || tree->sj == 0) 
    { 
        return 0; 
        
    }
    else 
    { 
        return 1 + deep(tree->lchild); 
        
    } 
    
} 

//叶子 
int yezi(Tree* tree) 
{ 
    if (tree == NULL || tree->sj == 0) 
    {
        return 1; 
        
    } 
    else 
    {
        return 0 + yezi(tree->lchild) + yezi(tree->rchild);
    } 
    
} 

int noyezi(Tree* tree) 
{ 
    if (tree->sj == 0) 
    { 
        return 0;
    } 
    else 
    { 
        return 1 + yezi(tree->lchild) + yezi(tree->rchild);
    } 
    
}

int main() 
{ 
    //建立一个二叉树 
    Tree* root, *p; 
    SeqCQueue* Q; 
    SeqCQueue Qdate;
    Q = &Qdate; 
    Data d; 
    void Initiate(Tree **root); 
    void QueueInitiate(SeqCQueue * Q); 
    QueueInitiate(Q); 
    Initiate(&root); 
    root->sj = ' '; 
    p = InsertLeftNode(root, 'A'); 
    p = InsertLeftNode(p, 'B');
    InsertRightNode(p, 'E'); 
    InsertRightNode(p->rchild, 'K');
    InsertLeftNode(p->rchild, 'J');
    p = InsertLeftNode(p, 'D'); 
    InsertLeftNode(p, 'H');
    p = InsertRightNode(p, 'I');
    p = InsertRightNode(root->lchild, 'C');
    InsertLeftNode(p, 'F'); 
    InsertRightNode(p, 'G');
    printf("%c\n", root->lchild->sj);
    printf("%c\n", root->lchild->lchild->sj);
    //层次遍历 
    int chanci(Tree *tree, SeqCQueue *q, Data *d);
    int n; 
    n = chanci(root->lchild, Q, &d); 
    printf("%d\n", n); 
    /*//输出树T的深度; 
    int deep(Tree * tree); 
    int m; 
    m = deep(root->lchild);
    printf("深%d\n", m); 
    //输出树T的叶子结点和非叶子结点
    int yezi(Tree * tree); 
    int noyezi(Tree * tree); 
    int y, no;
    y = yezi(root->lchild);
    printf("叶子%d ", y); 
    no = noyezi(root->lchild); 
    printf("非叶子%d ", no);
    */ 
    
}

// Output
A
B
A B D H I E J K C F G 11

 

int chanci(Tree* tree, SeqCQueue* q, Data* d)
{
    // 多加了一个判断
    if (tree == NULL || (tree->sj) == NULL)
    {
        return 0;
    }
    else
    {
        //void QueueAppend(SeqCQueue * Q, Data x);
        QueueAppend(q, tree->sj);
        //void QueueDelete(SeqCQueue * Q, Data * d);
        QueueDelete(q, d);
        printf("%c   ", *d);
        int temp1;
        temp1 = chanci(tree->lchild, q, d);
        int temp2;
        temp2 = chanci(tree->rchild, q, d);
        return 1 + temp1+ temp2;
    }
}

多加了一个判断就没有你说的问题了,因为是在 tree->sj 的时候提示的空指针的。

#include <stdio.h>
#include <stdlib.h>
#define MaxQueueSize 20
typedef char Data;
typedef struct Node
{
	Data sj;
	struct Node* rchild;
	struct Node* lchild;
}Tree;

typedef struct 
{
	Data queue[MaxQueueSize];
	int rear;  //队尾指针
	int front;  //队头指针
	int count;  //计数器
}SeqCQueue;

void QueueInitiate(SeqCQueue* Q)
{
	Q->rear = 0;
	Q->front = 0;
	Q->count = 0;
}

void QueueAppend(SeqCQueue* Q, Data x)
{
	if (Q->count > 0 && Q->rear == Q->front)
	{
		printf("队列已满无法插入! \n");
		//return 0;
	}
	else
	{
		Q->queue[Q->rear] = x;
		Q->rear = (Q->rear + 1) % MaxQueueSize;
		Q->count++;
		//return 1;
	}
}

void QueueDelete(SeqCQueue* Q, Data* d)
{
	if (Q->count == 0)
	{
		printf("队列已空无数据元素出队列! \n");
		//return 0;
	}
	else
	{
		*d = Q->queue[Q->front];
		Q->front = (Q->front + 1) % MaxQueueSize;
		Q->count--;
		//return 1;
	}
}
//初始化建立二叉树的头结点
void Initiate(Tree** root)
{
	*root = (Tree*)malloc(sizeof(Tree));
	(*root)->lchild = NULL;
	(*root)->rchild = NULL;
}

Tree* InsertLeftNode(Tree* curr, Data x)
{
	Tree* s, *t;
	if (curr == NULL)
		return NULL;

	t = curr->lchild;                              //保存原curr所指结点的左子树指针
	s = (Tree*)malloc(sizeof(Tree));
	s->sj = x;
	s->lchild = t;   //新插入结点的左子树为原curr的左子树
	s->rchild = NULL;
	curr->lchild = s;  //新结点为curr的左子树
	return curr->lchild;
}

Tree* InsertRightNode(Tree* curr, Data x)
{
	Tree* s, *t;
	if (curr == NULL) return NULL;

	t = curr->rchild;                      //保存原curr所指结点的右子树指针
	s = (Tree*)malloc(sizeof(Tree));
	s->sj = x;
	s->rchild = t;   //新插入结点的右子树为原curr的右子树
	s->lchild = NULL;
	curr->rchild = s;  //新结点为curr的右子树
	return curr->rchild;
}

int chanci(Tree* tree, SeqCQueue* q, Data* d)
{
	if ((tree->sj )== NULL)
	{
		return 0;
	}
	else
	{
		//void QueueAppend(SeqCQueue * Q, Data x);
		QueueAppend(q, tree->sj);
		//void QueueDelete(SeqCQueue * Q, Data * d);
		QueueDelete(q, d);
		printf("%c   ", *d);
		int temp1;
		temp1 = chanci(tree->lchild, q, d);
		int temp2;
		temp2 = chanci(tree->rchild, q, d);
		return 1 + temp1+ temp2;
	}
}

//输出树T的深度
int deep(Tree* tree)
{
	if (tree == NULL || tree->sj == NULL)
	{
		return 0;
	}
	else
	{
		return 1 + deep(tree->lchild);
	}
}
//叶子
int yezi(Tree* tree)
{
	if (tree == NULL || tree->sj == NULL)
	{
		return 1;
	}
	else
	{
		return 0 + yezi(tree->lchild) + yezi(tree->rchild);
	}

}
int noyezi(Tree* tree)
{
	if (tree->sj = NULL)
	{
		return 0;
	}
	else
	{
		return 1 + yezi(tree->lchild) + yezi(tree->rchild);
	}

}
int main()
{
	//建立一个二叉树
	Tree* root, *p;
	SeqCQueue* Q;
	SeqCQueue Qdate;
	Q = &Qdate;
	Data d;
	void Initiate(Tree **root);
	void QueueInitiate(SeqCQueue * Q);
	QueueInitiate(Q);
	Initiate(&root);
	root->sj = ' ';
	p = InsertLeftNode(root, 'A');
	p = InsertLeftNode(p, 'B');
	InsertRightNode(p, 'E');
	InsertRightNode(p->rchild, 'K');
	InsertLeftNode(p->rchild, 'J');
	p = InsertLeftNode(p, 'D');
	InsertLeftNode(p, 'H');
	p = InsertRightNode(p, 'I');
	p = InsertRightNode(root->lchild, 'C');
	InsertLeftNode(p, 'F');
	InsertRightNode(p, 'G');
	printf("%c\n", root->lchild->sj);
	printf("%c\n", root->lchild->lchild->sj);
	//层次遍历
	//int chanci(Tree *tree, SeqCQueue *q, Data *d);
	//int n;
    //n = chanci(root->lchild, Q, &d);
	//printf("%d\n", n);
	//输出树T的深度;
	int deep(Tree * tree);
	int m;
	m = deep(root->lchild);
	printf("深%d\n", m);
	//输出树T的叶子结点和非叶子结点
	int yezi(Tree * tree);
	int noyezi(Tree * tree);
	int y, no;
	y = yezi(root->lchild);
	printf("叶子%d         ", y/2);
	no = noyezi(root->lchild);
	printf("非叶子%d         ", no);

}

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632