二叉树的先序非递归遍历方式

本文介绍了一种二叉树的非递归前序遍历方法,并通过栈来辅助实现。首先定义了二叉树及栈的相关结构,接着实现了非递归遍历的完整流程,包括初始化栈、节点压栈与出栈等关键步骤。同时对比了递归遍历方法,验证了非递归遍历的正确性。
#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);
}

非递归遍历----思路:

 在函数中创建一个栈------用于防入节点的   为后续查找右子树的节点做准备

计入函数式 先进入一个大循环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;

}

在这次的试验中为了验证结果的正确性  使用了递归方式的先序遍历过程  最后的结果表明没有错误  完成!!!!!!!!!!!!!!!!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值