#include <stdio.h>
#include <stdlib.h>
#define bj 0
typedef struct node_2{
int value;
struct node_2 *left_child;
struct node_2 *right_child;
}BinaryTree;
typedef struct node {
BinaryTree *value;
struct node *next;
}stack;
typedef struct node_1{
int count;
stack *top;
}Stack;
void Stack_init(Stack **p){
*p=(Stack*)malloc(sizeof(Stack));
(*p)->count=0;
(*p)->top=NULL;
}
void Stack_push(Stack *p,BinaryTree* date){
if(p==NULL) return ;
stack *u=NULL;
u=(stack*)malloc(sizeof(stack));
u->value=date;
u->next=p->top;
p->top=u;
p->count++;
}
BinaryTree* Stack_pop(Stack *p){
if(p==NULL|| p->top==NULL) return NULL;
BinaryTree *u=NULL;
stack *t=NULL;
t=p->top;
u=t->value;
p->top=p->top->next;
free(t);
t=NULL;
p->count--;
return u;
}
void create_BinaryTree(BinaryTree **p){
int date;
scanf("%d",&date);
if(date==bj) return ;
*p=(BinaryTree*)malloc(sizeof(BinaryTree));
(*p)->value=date;
(*p)->left_child=NULL;
(*p)->right_child=NULL;
create_BinaryTree(&(*p)->left_child);
create_BinaryTree(&(*p)->right_child);
}
BinaryTree* create_1(){
BinaryTree *p=NULL;
p=(BinaryTree*)malloc(sizeof(BinaryTree));
p->value=1;
p->left_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->left_child->value=2;
p->left_child->left_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->left_child->left_child->value=4;
p->left_child->left_child->left_child=NULL;
p->left_child->left_child->right_child=NULL;
p->left_child->right_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->left_child->right_child->value=5;
p->left_child->right_child->left_child=NULL;
p->left_child->right_child->right_child=NULL;
p->right_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->right_child->value=3;
p->right_child->left_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->right_child->left_child->value=6;
p->right_child->left_child->left_child=NULL;
p->right_child->left_child->right_child=NULL;
p->right_child->right_child=NULL;
return p;
}
void Pretraver(BinaryTree *p){
if(p==NULL) return ;
printf("%d ",p->value);
Pretraver(p->left_child);
Pretraver(p->right_child);
}
if(pTree == NULL)return;
Stack *pStack = NULL;
Stack_init(&pStack);
while(1)
{
while(pTree)
{
printf("%d ",pTree->value);
Stack_push(pStack,pTree);
pTree = pTree->left_child;
}
pTree = Stack_pop(pStack);
if(pTree == NULL)break;
pTree = pTree->right_child;
}
}
int main()
{
BinaryTree *p=NULL;
create_BinaryTree(&p);
Pretraver(p);
printf("\n***************************************************\n");
BinaryTree *q=NULL;
q=create_1();
nice(q);
return 0;
#include <stdlib.h>
#define bj 0
typedef struct node_2{
int value;
struct node_2 *left_child;
struct node_2 *right_child;
}BinaryTree;
typedef struct node {
BinaryTree *value;
struct node *next;
}stack;
typedef struct node_1{
int count;
stack *top;
}Stack;
void Stack_init(Stack **p){
*p=(Stack*)malloc(sizeof(Stack));
(*p)->count=0;
(*p)->top=NULL;
}
void Stack_push(Stack *p,BinaryTree* date){
if(p==NULL) return ;
stack *u=NULL;
u=(stack*)malloc(sizeof(stack));
u->value=date;
u->next=p->top;
p->top=u;
p->count++;
}
BinaryTree* Stack_pop(Stack *p){
if(p==NULL|| p->top==NULL) return NULL;
BinaryTree *u=NULL;
stack *t=NULL;
t=p->top;
u=t->value;
p->top=p->top->next;
free(t);
t=NULL;
p->count--;
return u;
}
void create_BinaryTree(BinaryTree **p){
int date;
scanf("%d",&date);
if(date==bj) return ;
*p=(BinaryTree*)malloc(sizeof(BinaryTree));
(*p)->value=date;
(*p)->left_child=NULL;
(*p)->right_child=NULL;
create_BinaryTree(&(*p)->left_child);
create_BinaryTree(&(*p)->right_child);
}
BinaryTree* create_1(){
BinaryTree *p=NULL;
p=(BinaryTree*)malloc(sizeof(BinaryTree));
p->value=1;
p->left_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->left_child->value=2;
p->left_child->left_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->left_child->left_child->value=4;
p->left_child->left_child->left_child=NULL;
p->left_child->left_child->right_child=NULL;
p->left_child->right_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->left_child->right_child->value=5;
p->left_child->right_child->left_child=NULL;
p->left_child->right_child->right_child=NULL;
p->right_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->right_child->value=3;
p->right_child->left_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->right_child->left_child->value=6;
p->right_child->left_child->left_child=NULL;
p->right_child->left_child->right_child=NULL;
p->right_child->right_child=NULL;
return p;
}
void Pretraver(BinaryTree *p){
if(p==NULL) return ;
printf("%d ",p->value);
Pretraver(p->left_child);
Pretraver(p->right_child);
}
非递归遍历----思路:
在函数中创建一个栈------用于防入节点的 为后续查找右子树的节点做准备
计入函数式 先进入一个大循环while(1) 这看似是一个死循环 但是在循环的内部 我设置了退出当前循环的条件
在大循环中加入小循环 用来遍历子树的左子树的根节点 (依次入栈 并输出相应的节点的数据值) 当找到为空的时候 退出内层的循环 弹出栈顶的元素 再将谈树德元素作为新的子树的根节点 进入内层的循环 一直进行下去————————————————————————————
跳出循环 结束遍历的条件时 新弹出的栈顶元素为空的时候 这时表明 整个先序b遍历的过程就此结束
---------------------------------------------------------------------------------------------------------------------------------
void nice(BinaryTree *pTree)
{if(pTree == NULL)return;
Stack *pStack = NULL;
Stack_init(&pStack);
while(1)
{
while(pTree)
{
printf("%d ",pTree->value);
Stack_push(pStack,pTree);
pTree = pTree->left_child;
}
pTree = Stack_pop(pStack);
if(pTree == NULL)break;
pTree = pTree->right_child;
}
}
int main()
{
BinaryTree *p=NULL;
create_BinaryTree(&p);
Pretraver(p);
printf("\n***************************************************\n");
BinaryTree *q=NULL;
q=create_1();
nice(q);
return 0;
}
在这次的试验中为了验证结果的正确性 使用了递归方式的先序遍历过程 最后的结果表明没有错误 完成!!!!!!!!!!!!!!!!!!!