树结点入栈后数据域的值一直保持默认值

0

运行环境:VS2019(C语言编写)  

程序目的:实现二叉树非递归前序中序后序遍历

问题:结点入栈之后,树结点的数据域的值没有更新为新树结点的值,Push入栈操作不完善

/****
C语言实现非递归遍历二叉树
***/
#include<stdio.h>
#include<malloc.h>
typedef struct bitnode {
	struct bitnode* left;
	struct bitnode* right;
	struct bitnode* next;
	int data;
}BiTnode;
void CreateBiTree(BiTnode **root){
	if (*root == NULL) return;
	int value = -1;
	scanf_s("%d", &value);
	if (value == -1) {
		*root = NULL;
	}
	else {
		*root =(BiTnode*)malloc(sizeof(BiTnode));
		if (*root == NULL) return;
		(*root)->data = value;
		printf("左子树:\n");
		CreateBiTree(&(*root)->left);
		printf("右子树:\n");
		CreateBiTree(&(*root)->right);
	}
}
BiTnode *InitStack(BiTnode *S){
	S =(BiTnode*)malloc(sizeof(BiTnode));
	if (S == NULL) return NULL;
	S->left = NULL;
	S->right = NULL;
	S->next = NULL;
	return S;
}
void Push(BiTnode *S,BiTnode *Elem){
	BiTnode* node =(BiTnode*)malloc(sizeof(BiTnode));
	if (node == NULL) return;
	node->next = S;
	node->data=Elem->data;
	node->left = Elem->left;
	node->right = Elem->right;
	S=node;
}
BiTnode *Pop(BiTnode* S){
	if (S == NULL) return NULL;
	BiTnode* tmp = S;
	S = S->next;
	return tmp;
}
bool IsEmpty(BiTnode *S){
	return S->next== NULL?true : false;
}
BiTnode* GetTop(BiTnode* S) {
	return S;
}
/***
前序遍历
**/
void PreOrderTraverse(BiTnode *root){
	if (root == NULL) return;
	BiTnode *S=NULL;
	S=InitStack(S);
	Push(S, root);
	while(!IsEmpty(S)){
		BiTnode* tmp = GetTop(S);
		printf("%d", tmp->data);
		Pop(S);
		if (tmp->right) {
			Push(S, tmp->right);
		}
		if (tmp->left) {
			Push(S, tmp->left);
		}
	}
}
/****
中序遍历
***/
void InOrderTraverse(BiTnode *root){
	if (root == NULL) return;
	BiTnode *S=NULL;
	S=InitStack(S);
	while (root != NULL || !IsEmpty(S)) {
		if (root) {
			Push(S,root);
			root = root->left;
		}
		else {
			/***
			访问左子树后访问根节点
			***/
			root = GetTop(S);
			printf("%d", root->data);
			Pop(S);
			/***
			访问右子树
			**/
			root = root->right;
		}
	}
}
/***
后序遍历
***/
void PostOrderTraverse(BiTnode *root){
	if (!root) return;
	BiTnode* S1 =NULL;
	BiTnode* S2=NULL;
	S1=InitStack(S1);
	S2=InitStack(S2);
	Push(S1, root);
	while (!IsEmpty(S1)){
		BiTnode* tmp = GetTop(S1);
		Push(S2, tmp);
		Pop(S1);
		if (tmp->left) {
			Push(S1, tmp->left);
		}
		if (tmp->right) {
			Push(S1, tmp->right);
		}
	}
	while (!IsEmpty(S2)) {
		BiTnode* tmp = GetTop(S2);
		printf("%d", tmp->data);
		Pop(S2);
	}
}
int main(){
	BiTnode *T;
	CreateBiTree(&T);
	printf("前序遍历:\n");
	PreOrderTraverse(*(&T));
	printf("中序遍历: \n");
	InOrderTraverse(*(&T));
	printf("后序遍历:\n");
	PostOrderTraverse(*(&T));
	return 0;
}

测试数据输入:

2
左子树:
3
左子树:
-1
右子树:
-1
右子树:
2
左子树:
-1
右子树:
-1

前序遍历:
void Push(BiTnode *S,BiTnode *Elem){}内S->data的值一直保持-842150451,没有随着入栈操作的执行改变。

ava
凤栖梧

2020-11-24

0

你的整个逻辑很奇怪。

你不应该为了遍历树而修改树的结构。

同时,你不应该利用树的结构实现堆栈。

抛开逻辑上的问题,你的 Push 代码是错误的:

void Push(BiTnode *S, BiTnode *Elem){
	BiTnode* node = (BiTnode*)malloc(sizeof(BiTnode));
	……
	S = node;
}

你这样并不能修改指针 S 的值,你修改的是形参的值。

如果要修改指针 S 的值,你需要修改形参为指针的指针。或者简单的改用引用类型也可以,例如这样:

void Push(BiTnode *&S, BiTnode *Elem){
	BiTnode* node = (BiTnode*)malloc(sizeof(BiTnode));
	……
	S = node;
}
ava
慢羊羊

2020-11-24

谢谢大佬的解答,问题解决了,我这程序主要是这个问题,也就是副本与本体没有处理好导致的问题。 -  凤栖梧  2020-11-25
0

二叉树不是这种 struct 吧

还有 next 的?

ava
xiongfj ◑◑

2020-11-24

这个添加next是为了方便建立栈,这种结构体感觉确实有点怪怪的 -  凤栖梧  2020-11-24
技术讨论社区