马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
后序遍历我还没有整清楚
#include <stdio.h>
#include <stdlib.h>
typedef char Elemtype;
typedef struct Node //建立二叉树属性
{
Elemtype litter;
struct Node *left;
struct Node *right;
}Node, *Nodle;
typedef struct Diss //建立链表站
{
Node *time;
struct Diss *next;
}Diss, *Dog;
void prin_explain();
void push_stree(Dog *F, Node *Tim);
void get_stree(Nodle *T); //创建二叉树
void prin_stree(Node *T); //前序打印二叉树
void prin_stree_two(Node *T); //中序打印二叉树
//void prin_stree_three(Node *T); //后序打印二叉树
void prin_stree_foure(Node *T); //测试打印二叉树
void push_stree(Dog *F, Node *Tim) //前叉建立栈
{
Diss *temp;
temp = (Diss *)malloc(sizeof(Diss ));
temp->time = Tim;
if (*F == NULL)
{
*F = temp;
temp->next = NULL;
}
else
{
temp->next = *F;
*F = temp;
}
}
void prin_stree_four(Node *T)
{
if (T != NULL)
{
prin_stree_four(T->left);
prin_stree_four(T->right);
printf("%c", T->litter);
}
}
/*void prin_stree_three(Node *T)
{
Diss *F, *N;
F = (Diss *)malloc(sizeof(Diss ));
F = NULL;
while (1)
{
if (F == NULL && T==NULL)
{
break;
}
while (T != NULL)
{
push_stree(&F, T);
T = T->left;
}
T = F->time->right;
N = F;
F = F->next;
free(N);
}
}*/
void prin_stree_two(Node *T)
{
Diss *F, *N;
F = (Diss *)malloc(sizeof(Diss ));
F = NULL;
while (1)
{
if (F == NULL && T==NULL)
{
break;
}
while (T != NULL)
{
push_stree(&F, T);
T = T->left;
}
printf("%c", F->time->litter);
T = F->time->right;
N = F;
F = F->next;
free(N);
}
}
void prin_stree(Node *T)
{
Diss *F, *N;
F = (Diss *)malloc(sizeof(Diss ));
F = NULL;
while (1)
{
if (F == NULL && T==NULL)
{
break;
}
while (T != NULL)
{
printf("%c", T->litter);
push_stree(&F, T);
T = T->left;
}
T = F->time->right;
N = F;
F = F->next;
free(N);
}
}
void get_stree(Nodle *T)
{
Diss *F = NULL, *N;
Elemtype ch, cg;
Node *Temp;
while (1)
{
if (F == NULL && *T != NULL) //结束本程序条件
{
break;
}
do
{
scanf("%c", &ch);
if (ch == ' ')
{
Temp = NULL;
}
else //获得节点并输入数据
{
Temp = (Node *)malloc(sizeof(Node ));
Temp->litter = ch;
}
if (*T == NULL)
{
*T = Temp;
}
else //与头部节点连接
{
if (cg ==' ')
{
F->time->right = Temp; //向右变量中插入数据
N = F;
F = F->next;
free(N); //释放栈中数据;空间回收
}
else
{
F->time->left = Temp; //用栈作为静态变量,节省空间
}
}
cg = ch; //标记符用于记录上一次循环ch的状态
if (Temp != NULL) //向栈中输入变量
{
push_stree(&F, Temp);
}
} while (ch != ' ');
}
}
void prin_explain()
{
printf("1、非递归创建二叉树\n");
printf("2、前序非递归打印二叉树\n");
printf("3、中序非递归打印二叉树\n");
printf("4、测试递归打印二叉树\n");
printf("9、¥ 结束本程序 ¥\n");
}
int main()
{
int number;
Node *T = NULL;
prin_explain();
do
{
scanf("%d", &number);
getchar();
switch (number)
{
case 1:
{
get_stree(&T);
}
break;
case 2:
{
prin_stree(T);
putchar('\n');
}
break;
case 3:
{
prin_stree_two(T);
putchar('\n');
}
break;
case 4:
{
prin_stree_four(T);
putchar('\n');
}
break;
case 9:
//结束本程序
break;
default:
printf("您所输入的数据有误请从新输入!\n");
break;
}
}while (number != 9);
return 0;
}
|