二叉树的先序非递归遍历

#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)  依次进行遍历给二叉树的左子树  直到找到最后的一个节点是空的时候退出内层循环

          每次取出栈顶的元素  当弹出的结果的右孩子不是空的时候  再次进入内层的循环中依次查找新的子树的左孩子

          推出大循环的条件是 当从栈中弹出的元素为空的时候 当最后弹出栈的结果是空的时候  结束整个遍历过程

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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.先序遍历非递归算法#define maxsize 100typedef struct{ Bitree Elem[maxsize]; int top;}SqStack;void PreOrderUnrec(Bitree t){ SqStack s; StackInit(s); p=t; while (p!=null || !StackEmpty(s)) { while (p!=null) //遍历左子树 { visite(p->data); push(s,p); p=p->lchild; }//endwhile if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历 { p=pop(s); p=p->rchild; }//endif }//endwhile }//PreOrderUnrec2.中序遍历非递归算法#define maxsize 100typedef struct{ Bitree Elem[maxsize]; int top;}SqStack;void InOrderUnrec(Bitree t){ SqStack s; StackInit(s); p=t; while (p!=null || !StackEmpty(s)) { while (p!=null) //遍历左子树 { push(s,p); p=p->lchild; }//endwhile if (!StackEmpty(s)) { p=pop(s); visite(p->data); //访问根结点 p=p->rchild; //通过下一次循环实现右子树遍历 }//endif }//endwhile}//InOrderUnrec3.后序遍历非递归算法#define maxsize 100typedef enum{L,R} tagtype;typedef struct { Bitree ptr; tagtype tag;}stacknode;typedef struct{ stacknode Elem[maxsize]; int top;}SqStack;void PostOrderUnrec(Bitree t){ SqStack s; stacknode x; StackInit(s); p=t; do { while (p!=null) //遍历左子树 { x.ptr = p; x.tag = L; //标记为左子树 push(s,x); p=p->lchild; } while (!StackEmpty(s) && s.Elem[s.top].tag==R) { x = pop(s); p = x.ptr; visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点 } if (!StackEmpty(s)) { s.Elem[s.top].tag =R; //遍历右子树 p=s.Elem[s.top].ptr->rchild; } }while (!StackEmpty(s));}//PostOrderUnrec
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值