二叉树的层序遍历,c/c++描述

本文介绍了如何通过循环队列进行二叉树的层序遍历,详细讲解了如何利用队列结构存储并输出节点及其子节点,适用于理解二叉树结构和队列操作在树形数据结构中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  按一层一层,从上往下,从左到右的顺序遍历二叉树。称为层序遍历。这里用到了队列。一端插入,另一端删除。对于二叉树链表,我们易于实现纵向访问,从祖先节点,到父母节点,到子孙节点,但对于一个节点来讲,它有多少个同行的兄弟,堂兄弟节点,却是不容易知道的。所以我们要用有记忆功能的循环队列来配合访问二叉树,因为顺序队列里的数组成员可以保存一整行二叉树节点。再把它们逐个输出,在逐个输出队列里本行节点时,确认它们的子节点情况,并也保存在这个队列里。
  完整代码如下:

#include<iostream>
#include<stdio.h>
using namespace std;
#define STACKDEPTH 15
#define QUEUELENTH 15
struct BiTreeNode {
	char value;
	BiTreeNode* leftChild;
	BiTreeNode* rightChild;
};
struct QueueNodes {
	BiTreeNode* nodes[QUEUELENTH];
	int indexHead = 0;
	int indexTail = 0;
};
void createBTree(BiTreeNode *&biTreeRoot,char *&ptChar) {
	struct {
		BiTreeNode* ptsBiTree[STACKDEPTH];
		int indexTop = -1;
	}sequStack;

	BiTreeNode* ptNew = NULL;
	char s ;
	int leftRight;//1 is left   2 is right
	while (*ptChar != '\0') {
		s = *ptChar;
		if ('A' <= s && s <= 'Z') {
			ptNew = new BiTreeNode;
			ptNew->value = s;
			ptNew->leftChild = ptNew->rightChild = NULL;

			if (biTreeRoot == NULL)
				biTreeRoot = ptNew;
			else if (leftRight == 1)
				sequStack.ptsBiTree[sequStack.indexTop]->leftChild = ptNew;
			else if (leftRight == 2)
				sequStack.ptsBiTree[sequStack.indexTop]->rightChild = ptNew;
		}
		else if (s == '(') {
			sequStack.indexTop++;
			sequStack.ptsBiTree[sequStack.indexTop] = ptNew;
			leftRight = 1;
		}
		else if (s == ',')
			leftRight = 2;
		else if (s == ')')
			sequStack.indexTop--;
	
		ptChar++;
	}
}
void displayBTree(BiTreeNode *&biTreeRoot) {   // 本查找方法是先序遍历
	if (biTreeRoot == NULL)
		return;//if binary tree does not exsit,return
	cout << biTreeRoot->value;
	if (biTreeRoot->leftChild != NULL || biTreeRoot->rightChild != NULL) {
		cout << '(';
		displayBTree(biTreeRoot->leftChild);
		if (biTreeRoot->rightChild != NULL) {
			cout << ',';
			displayBTree(biTreeRoot->rightChild);
		}
		cout << ')';
	}
}
void levelOrder(BiTreeNode *&biTreeRoot) {
	if (biTreeRoot == NULL)
		return;
	QueueNodes queue;
	BiTreeNode* pt;
	queue.indexTail++;
	queue.nodes[queue.indexTail] = biTreeRoot;
	while (queue.indexHead != queue.indexTail) {
		queue.indexHead = (queue.indexHead + 1) % QUEUELENTH;
		pt = queue.nodes[queue.indexHead];
		cout << pt->value<<' ';
		
		if (pt->leftChild != NULL) {
			queue.indexTail = (queue.indexTail + 1) % QUEUELENTH;
			queue.nodes[queue.indexTail] = pt->leftChild;
		}
	
		if (pt->rightChild != NULL) {
			queue.indexTail = (queue.indexTail + 1) % QUEUELENTH;
			queue.nodes[queue.indexTail] = pt->rightChild;
		}
	}
}
int main() {
	char array[] = "A(B(D(,G)),C(E,F))";
	char* ptChar = array;  //c++里只能这样分开定义,要不会出错。
	BiTreeNode* biTreeRoot = NULL;

	createBTree(biTreeRoot,ptChar);
	cout << "the char array is :";
	for (int i = 0; array[i] != '\0'; i++)
		cout << array[i];
	cout<< endl<< "binary tree is    :";
	displayBTree(biTreeRoot);

	cout << endl << "level traverse : "; levelOrder(biTreeRoot);

	return 0;
}

  测试结果如下:
在这里插入图片描述
对应二叉树为:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值