#include<stdio.h>
#include<stdlib.h>
#define ElemType int
//定义栈的存储类型
typedef struct Nodestack *ListStack;
struct Nodestack{
ElemType x;//储存数据
struct Nodestack *next; //指针
}Stack;
//定义树的结点
typedef struct node{ //树的结点
int data; //数据域
struct node* left; //左儿子
struct node* right; //右儿子
} Node,*TreeNode;
//利用树的结点类型指针指向根结点
typedef struct {
TreeNode root; //树根
}*TreeFirstNode,FirstNode;
void Init( TreeFirstNode &tree, int value)//创建树
{
TreeNode node=(TreeNode)malloc(sizeof(Node));//创建一个节点
node->data = value;
node->left = NULL;
node->right = NULL;
tree->root = node;//创建根结点
printf("初始化成功\n");
}
//树的中序遍历
void Show(TreeNode tree)
{
TreeNode temp = tree;
if ( temp!= NULL) //工作指针
{
Show(temp->left);
printf("%d ",temp->data);
Show(temp->right);
}
}
//数的先序遍历
void Show1(TreeNode tree)
{
TreeNode temp = tree; //工作指针
if ( temp!= NULL)
{
printf("%d ",temp->data);
Show(temp->left);
Show(temp->right);
}
}
//树的后序遍历
void Show2(TreeNode tree)
{
TreeNode temp = tree; //工作指针
if ( temp!= NULL)
{
Show(temp->left);
Show(temp->right);
printf("%d ",temp->data);
}
}
//非递归中序遍历
//栈的初始化
void InitStack(ListStack &S){
S = (ListStack)malloc(sizeof(Stack));
S->next = NULL;
//printf("初始化成功!\n");
}
//判断是否为空栈
bool ListStackEmpty(ListStack S){
if(S->next == NULL)
return true;
return false;
}
//将数据压入栈内
/* 用头插法更合适 出栈的时候只需弹出第一个节点即可*/
bool Push(ListStack S ,ElemType data){
ListStack p;
ListStack t;
t = S; //防止s指向发生改变
p = (ListStack)malloc(sizeof(Stack));
p->x = data;
p->next = t->next;
t->next = p;
printf("入栈成功\n");
return true;
}
//将数据弹出栈
bool Pop(ListStack &S,ElemType& t){
if(S->next==NULL){
printf("栈空\n");
return false;
}else{
ListStack p;
p=S->next;
t=p->x;
S->next=p->next;
free(p);
//printf("弹出成功/n");
}
return true;
}
void InOrder2(TreeFirstNode tree,ListStack S){
TreeNode p=tree->root;
InitStack(S);
while(p||ListStackEmpty(S)){
if(p){
Push(S,p->data);
p=p->left;
}
else{
printf("%d ",S->x);
Pop(S,p->data);
p = p->right;
}
}
}
void Insert(TreeFirstNode &tree,int value){
TreeNode temp = tree->root;//工作从树根开始
TreeNode node=(TreeNode)malloc(sizeof(Node));//创建一个节点
node->data = value;
node->left = NULL;
node->right = NULL;
while (temp!= NULL)
{
if (value < temp->data) //小于就进左子树
{
if (temp->left == NULL)
{
temp->left = node;
printf("插入成功\n");
return;
}
else
{ //不空继续判断
temp = temp->left;
}
}
else{ //否则进右子树
if(temp->right == NULL)
{
temp->right = node;
printf("插入成功\n");
return;
}
else{ //不空继续判断
temp = temp->right;
}
}
}
}
int main(){
int value;
int N,i;
ElemType t;
ListStack S;
printf("请输入根结点的值:\n");
scanf("%d",&value);
TreeFirstNode tree=(TreeFirstNode)malloc(sizeof(FirstNode));
Init(tree,value);
printf("请输入要插入树的数据个数(N):");
scanf("%d",&N);
for(i=1;i<N+1;i++){
printf("请输入第%d个数:",i);
scanf("%d",&value);
Insert(tree,value);
}
//Show(tree->root);
InOrder2(tree,S);
return 0;
}

在非递归中序遍历时为什么会不输出
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- lne8734 2021-10-12 18:38关注
第113行while(p||ListStackEmpty(S))改为(p||(!ListStackEmpty(S)))
第119行printf("%d ",S->x);改为printf("%d \n",S->next->x);本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报