非递归先序遍历&&非递归中续遍历

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAXSIZE 50  //定义栈中元素的最大个数

typedef struct BiTree{
	int data;
	struct BiTree *lchild;
	struct BiTree *rchild;
}BiTree;

typedef struct{
	BiTree data[MAXSIZE];  // 存放栈中元素
	int top;  //栈顶指针
}SqStack;

//初始化栈
void Init_SqStack(SqStack &S){
	S.top = -1;
	return;
}

//1. Top值不能超过MaxSize
//2. 空栈的判定条件通常定为top == -1,满栈的判定条件通常为top == MaxSize - 1,栈中数据元素个数为top + 1


//判空
bool StackEmpyt(SqStack S){
	if (S.top == -1) return true;
	else return false;
}

//进栈
void Push(SqStack &S, BiTree x){   //&s引用,对原对象操作,s与原对象属性相同的对象
	if (S.top == MAXSIZE - 1){
		printf("Fulled");
		return;
	}
	S.data[++S.top] = x;
}

//出栈
BiTree Pop(SqStack &S){  // 把出栈的值赋给x,所以引用x
	if (S.top == -1){
		printf("Empty");
		return;
	}
	BiTree x = S.data[S.top--];
	return x;
}

//读取栈顶元素
bool GetTop(SqStack S, BiTree &x){  // 把出栈的值赋给x,所以引用x
	if (S.top == -1){
		printf("Empty");
		return false;
	}
	x = S.data[S.top];
	return true;
}

//非递归先序遍历
void PreOrderTravers(BiTree *b){
	SqStack S;
	Init_SqStack(S);
	BiTree *p = b;  // 工作指针
	while (p || !StackEmpyt(S)){
		while (p){  // 先一路中间结点压栈直到最左边的一个结点
			printf("%d ", p->data);
			Push(S, *p);
			p = p->lchild;
		}
		if (!StackEmpyt(S)){
			*p = Pop(S);
			p = p->rchild;
		}
	}
}


// 非递归中序遍历
void PreOrderTraverse(BiTree *b){
	SqStack S;
	Init_SqStack(S);
	BiTree *p = b;  // 工作指针
	while (p || !StackEmpyt(S)){  // 当结点不空或者栈不空,因为一开始栈是空的,没有p这个条件执行不了
		while (p){  // 中序先将结点进栈保存
			Push(S, *p);
			p = p->lchild;
		}
		// 遍历到左下角尽头再出栈访问
		*p = Pop(S);
		printf("%d ", p->data);
		p = p->rchild;
   }
}


  • 0
    点赞
  • 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、付费专栏及课程。

余额充值