2021-08-25王道 数据结构 第5章 树与二叉树 p141 第4题

第5章 树与二叉树

5.3 二叉树的遍历和线索二叉树

综合应用题 第4题

在这里插入图片描述

#include   
#include                         //malloc所在头文件
#include  
#define MaxSize 100
using namespace std;
typedef int ElemType;
typedef struct BiTNode{//声明树 
	ElemType data; //数据域 
	struct BiTNode *lchild,*rchild;//左右孩子指针 
}BiTNode,*BiTree;

typedef struct{ //声明队列 
	BiTNode* queue[MaxSize];                      
	int front,rear;                 
}SqQueue;

void InitQueue(SqQueue &Q)//初始化队列 
{
	Q.rear=Q.front=0;
}
int EnQueuel(SqQueue &Q,BiTNode* x)//入队 
{
	if(Q.rear==MaxSize)
	return 0;
	Q.queue[Q.rear]=x;
	Q.rear++;
	return 1;
}
BiTNode* DeQueuel(SqQueue &Q)//出队 
{
	BiTNode* x;
	if(Q.front==Q.rear)
	return 0;
	x=Q.queue[Q.front];
	Q.front++;
	return x;
}
bool QueueEmpty(SqQueue &Q)//判断队列是否为空 
{
	if(Q.front==Q.rear) 
	return true;
	else
	return false;
} 

typedef struct {//声明栈 
	BiTNode* stack[MaxSize];                      
	int top;                    
}SqStack;
 
void InitStack(SqStack &S)//初始化栈 
{
	S.top=-1;
}

int Push(SqStack &S,BiTNode* x)//入栈 
{
	if(S.top==MaxSize-1)
	{
		cout<<"栈满"<<endl; 
		exit(0);
	}
	S.stack[++S.top]=x;
//	cout<<"i"<
	return 1;
}
BiTNode* Pop(SqStack &S)//出栈 
{
	BiTNode* x;
	if(S.top==-1)
	{
		cout<<"栈空"<<endl;
		return 0;
	}
	x=S.stack[S.top--];
//	cout<<"o"<
	return x;
}
BiTNode* GetTop(SqStack &S)//得到栈顶元素 
{
	BiTNode* s;
	if(S.top==-1)
	{
		cout<<"栈空"<<endl;
		return 0;
	}
	s=S.stack[S.top];
//	cout<<"g"<
	return s;
}

bool StatckEmpty(SqStack &S)//判断栈是否为空 
{
	if(S.top==-1) 
	return true;
	else
	return false;
}  

bool InitBiTree(BiTree &B){//初始化树 
	B=NULL;
	return true;
} 

BiTree BiTreeCreatT(ElemType *a,BiTree &B) {//建立树 
	InitBiTree(B);                     //初始化一个空链表  
	B=(BiTree)malloc(sizeof(BiTNode));
	B->lchild=NULL;
    B->rchild=NULL;
	B->data=a[0];
	BiTNode *p,*s;
	s=B;
	
	p = (BiTNode *)malloc(sizeof(BiTNode));       //申请新的结点 
	p->data = a[1];                        
	p->lchild=NULL;
    p->rchild=NULL;                   
	s->lchild=p;
	
	p = (BiTNode *)malloc(sizeof(BiTNode));       //申请新的结点 
	p->data = a[2];                         
	p->lchild=NULL;
    p->rchild=NULL;                 
	s->rchild=p;
	s=s->lchild;
	
	p = (BiTNode *)malloc(sizeof(BiTNode));       //申请新的结点 
	p->data = a[3];                         
	p->lchild=NULL;
    p->rchild=NULL;                 
	s->lchild=p;
	
	p = (BiTNode *)malloc(sizeof(BiTNode));       //申请新的结点 
	p->data = a[4];                    
	p->lchild=NULL;
    p->rchild=NULL;                  
	s->rchild=p;
	return B;
}

void InvertLevel(BiTree &B,SqQueue &Q,SqStack &S)
{
	BiTNode *s=B;
	if(s)
	{
		EnQueuel(Q,s);
		while(!QueueEmpty(Q))
		{
			s=DeQueuel(Q);
		    Push(S,s);
		    if(s->lchild)
		        EnQueuel(Q,s->lchild);
		    if(s->rchild)
		        EnQueuel(Q,s->rchild);
		}
		while(!StatckEmpty(S))
	    {
		    s=Pop(S);
		    cout<<s->data<<" ";
	    }
		
	} 
	
}

int main() {
  	int a[5]={4,2,5,1,3};
  	
  	BiTree B;
  	BiTreeCreatT(a,B);
  	
  	SqQueue Q;
  	InitQueue(Q);
  	
  	SqStack S;
  	InitStack(S);
  	
  	cout<<"层次遍历:";
    InvertLevel(B,Q,S);
	return 0;
}

2021-08-25王道 数据结构 第5章 树与二叉树 p141 第4题_第1张图片
2021-08-25王道 数据结构 第5章 树与二叉树 p141 第4题_第2张图片

你可能感兴趣的:(考研,数据结构,王道,数据结构)