⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 9 stepvisit.cpp

📁 数据结构的必做编程题
💻 CPP
字号:


#include <iostream.h>
#include <malloc.h>			
#include <stdio.h>			
#include <stdlib.h>
#define N 50
#define OVERFLOW 0
#define NULL 0

typedef struct BiTNode
{
	char data;
	struct BiTNode *lchild;
	struct BiTNode *rchild;		
}*BiTree;		

typedef struct queue
{
	BiTree elem;
	int front;
	int tail;
}queue;

void CreateBiTree(BiTree &T)	//先序扩展序列建立二叉树的递归算法 
{  
	char ch;
	scanf("%c",&ch);
	if (ch=='*') 
	{
		T = NULL;		
	}
	else 
	{ 
		if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))
		{
			exit(OVERFLOW);
		}
		T->data=ch;
		CreateBiTree(T->lchild); 
		CreateBiTree(T->rchild); 
	}  
} 


queue* CreateQueue () 
 {							// 构造一个空队列Q
	queue *q;
	q=(queue *) malloc(sizeof (queue));
	if (!q) exit (OVERFLOW);	
    q->elem = (BiTree) malloc(N*sizeof (struct BiTNode));
    if (!q->elem) exit (OVERFLOW);		              
    q->front = q->tail = 0;
     return q;
 }


BiTree SubQueue(queue *q)
{
	BiTree p;
	p=&(q->elem[q->front]);
	(q->front)++;
	return p;
}


void AddQueue(queue *q,BiTree T)
{
	q->elem[q->tail]=*T;
	q->tail++;
	return;
}


void main()	//层次遍历二叉树的非递归算法的主程序
{
	queue *q;
    BiTree p;
    BiTree b;		
	printf("请按先序扩展序列输入二叉树,空用*表示\n");
	CreateBiTree(b);
    printf("\n层次遍历二叉树\n");	
	q=CreateQueue();				
	AddQueue(q,b);					
	while(q->front!=q->tail)		
		{
			p=SubQueue(q);
			printf("%c",p->data);
			if(p->lchild) AddQueue(q,p->lchild);
			if(p->rchild) AddQueue(q,p->rchild);
		}
	
	printf("\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -