树的层次遍历也建立和先序遍历与建立

11 篇文章 0 订阅
10 篇文章 1 订阅

 层次遍历建立树和层次遍历输出树

/*利用顺序队列,层次建立二叉树*/
 
#include <stdio.h>
#define MAXSIZE 100
 
struct tnode   //树的数据结构
{
	char data;
	tnode *lchild, *rchild;
};
 
struct queue  //队列的数据结构
{
	tnode *data[MAXSIZE];//存放树的结点的数组 
	int front, rear;
};
 
void creat(queue &q);   //创建一个空队列
void enqueue(queue &q,tnode *t);   //将t入队
tnode *dequeue(queue &q);   //出队,并返回对头元素
bool isempty(queue &q);    //判断队列是否为空
tnode *creatbintree();    //按层次顺序创建一棵二叉树,并返回根节点
void showbintree(tnode *root);  //层次遍历二叉树
 
int main()
{
	tnode *root = NULL;
	root = creatbintree();
	showbintree(root);
	return 0;
}
 
void creat(queue &q)
{
	q.front = q.rear = 0;
}
 
void enqueue(queue &q, tnode *t)
{
	if ((q.rear + 1) % MAXSIZE == q.front)
	{
		printf("队列满,不能入对\n");
		return;
	}
	q.rear = (q.rear + 1) % MAXSIZE;//尾 
	q.data[q.rear] = t;
}
 
tnode *dequeue(queue &q)
{
	tnode *t;//出队时返回的是一个结点 
	q.front = (q.front + 1) % MAXSIZE;//头 
	t= q.data[q.front];	
	return t;
}
 
bool isempty(queue &q)
{
	return (q.front == q.rear);
}
 
 
//创建链表的时候,层序建立 :什么样的遍历方式决定用什么方式进行建立 
//队列进行存储作用:1.方便每次找到父结点。
tnode *creatbintree()
{
	//1.先将根节点入队,当队列不为空时,循环执行以下操作:
	//2.输入左子树的值,不为空,将其入队
	//3.输入右子树的值,不为空,将其入队
	char a;
	tnode *root;//就是头结点 
	queue Q;//创建一个队列
	creat(Q);//,初始化队列 
     scanf("%c",&a);
      getchar();
	if (a == '#')    //如果第一个节点为空,就直接返回空树
		return NULL;
	else
	{
		root = new tnode;//创建一个结点,相当于malloc 
		root->data = a;
		enqueue(Q, root);  //根节点入队
	}
	while (!isempty(Q))   //当队列不为空
	{
		//先输入左孩子的值,再输入右孩子的值
		tnode *p= dequeue(Q);//获得上一次的存入结构指针的地址 -->每次加一取出,
		//因此这也是循环的判断条件 ,说白了就是出队 
		printf("%c:",p->data);//将上一次存入的数输出 
		 scanf("%c",&a);
		  getchar();
		if (a == '#')   //左孩子为空
			p->lchild = NULL;
		else
		{
			p->lchild = new tnode;
			p->lchild->data = a;
			enqueue(Q, p->lchild);  //左孩子入队
		}
		printf("%c:",p->data);
		 scanf("%c",&a);
		  getchar();
		if (a == '#')   //右孩子为空
			p->rchild = NULL;
		else
		{
			p->rchild = new tnode;
			p->rchild->data = a;
			enqueue(Q, p->rchild);   //右孩子入队
		}
 
	}
	return root;
}
 //层序输出 
void showbintree(tnode *root)
{
	//1.先将根节点入队,当队列不为空时,循环执行以下操作:
	//2.出队一个元素,访问它
	//3.若左子树不为空,将其入队
	//4.若右子树不为空,将其入队
	queue Q;
	tnode *p;
	creat(Q);
	if (root == NULL)
		return;
	enqueue(Q, root);//先将根结点入队 
	//不断的入队出队进行层次输出 
	while (!isempty(Q))
	{
		p = dequeue(Q);//出队 
		printf("%c",p->data);
		//无论是什么方式对树进行操作,都是先左再右 
		if(p->lchild)
		  enqueue(Q, p->lchild);
		if(p->rchild)
		  enqueue(Q, p->rchild);
	}
	printf("\n");
}

 

先序遍历建立树和先序遍历输出树 

#include <stdio.h>
#include <stdlib.h>
 
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
//树的结点 
struct TNode{
    ElementType Data;
    BinTree Left;//用指针一看就是结点 
    BinTree Right;
};
 
BinTree CreatBinTree(); 
int PreorderPrintLeaves( BinTree BT );
 
int main()
{
    BinTree BT = CreatBinTree();
    printf(" nodes order are:");
    PreorderPrintLeaves(BT);
    printf("\n");
 
    return 0;
}
BinTree CreatBinTree(){
	char ch;
	TNode * t;
	scanf("%c",&ch);
	getchar();//吃掉回车
	if(ch=='#'){
		return NULL;
	}
	else
	{
		//先序:所以先对父结点进行操作-->递归程序一定从小问题进行分析 
		t=(TNode *)malloc(sizeof(TNode));
		t->Data=ch;
		printf("%c:",ch);
		t->Left = CreatBinTree();
		printf("%c:",ch);
		t->Right =CreatBinTree();
		return t;//返回该节点 
	}
}
//先序输出 
int  PreorderPrintLeaves( BinTree BT )
{
    if(BT)
	{
	   printf(" %c",BT->Data);
        PreorderPrintLeaves(BT->Left);//判断左
        PreorderPrintLeaves(BT->Right);//判断右
    }
            return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小丞啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值