用STL中的queue实现
//
// Created by Cauchyshy on 2023/5/23.
//
// 层次遍历(广度遍历)从上到下 从左到右一层一层遍历
// 逻辑 利用队列 没有这个就没有直接转换成简洁递归了 有栈可以转简洁递归
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct treeNode {
int a; // 数据成员
struct treeNode *pFather; // 父节点
struct treeNode *pLeft; // 左孩子
struct treeNode *pRight; // 右孩子
};
void look(struct treeNode *root) {
queue<struct treeNode *> que;
if (root != NULL) {
que.push(root);
}
while (!que.empty()) {
printf("%d ", que.front()->a);
if (que.front()->pLeft != NULL) que.push(que.front()->pLeft);
if (que.front()->pRight != NULL) que.push(que.front()->pRight);
que.pop();
}
}
int main() {
struct treeNode t1 = { 1 };
struct treeNode t2 = { 2 };
struct treeNode t3 = { 3 };
struct treeNode t4 = { 4 };
struct treeNode t5 = { 5 };
struct treeNode t6 = { 6 };
struct treeNode t7 = { 7 };
struct treeNode t8 = { 8 };
struct treeNode t9 = { 9 };
struct treeNode t10 = { 10 };
// 链接
t1.pLeft = &t2;
t1.pRight = &t3;
t1.pFather = NULL;
t2.pLeft = &t4;
t2.pRight = &t5;
t2.pFather = &t1;
t3.pRight = &t6;
t3.pLeft = NULL;
t3.pFather = &t1;
t4.pLeft = NULL;
t4.pRight = NULL;
t4.pFather = &t2;
t5.pLeft = &t7;
t5.pRight = &t8;
t5.pFather = &t2;
t6.pLeft = &t9;
t6.pRight = &t10;
t6.pFather = &t3;
t7.pLeft = NULL;
t7.pRight = NULL;
t7.pFather = &t5;
t8.pLeft = NULL;
t8.pRight = NULL;
t8.pFather = &t5;
t9.pLeft = NULL;
t9.pRight = NULL;
t9.pFather = &t6;
t10.pLeft = NULL;
t10.pRight = NULL;
t10.pFather = &t6;
look(&t1);
return 0;
}
尾动队列数组实现
//
// Created by Cauchyshy on 2023/5/23.
//
// 层次遍历(广度遍历)从上到下 从左到右一层一层遍历
// 逻辑 利用队列 没有这个就没有直接转换成简洁递归了 有栈可以转简洁递归
#include <iostream>
#include <cstdio>
using namespace std;
struct treeNode {
int a; // 数据成员
struct treeNode *pFather; // 父节点
struct treeNode *pLeft; // 左孩子
struct treeNode *pRight; // 右孩子
};
#define QUEUS_SIZE 16
// 移动元素型数组队列
struct treeNode *queue[QUEUS_SIZE]; // 队列 只装树的节点
int queueHead = 0; // 队头 默认第一个元素
int queueTail = -1; // 队尾
void push(struct treeNode *node) {
queueTail++; // 尾巴下标+1
queue[queueTail] = node; // 入队
}
// 移动数组元素
void move(void) {
for (int i = 1; i <= queueTail; ++i) { // 队头出掉 后面元素向前移动
queue[i - 1] = queue[i];
}
}
struct treeNode *pop(void) {
if (queueHead > queueTail)
return NULL; // 无元素 直接返回
struct treeNode *temp = queue[queueHead];
move();
queueTail--; // 队尾减一 因为向前移动了
return temp;
}
void look(struct treeNode *root) {
if (root != NULL) {
push(root);
}
while (!(queueHead > queueTail)) {
printf("%d ", queue[queueHead]->a);
if (queue[queueHead]->pLeft != NULL) push(queue[queueHead]->pLeft);
if (queue[queueHead]->pRight != NULL) push(queue[queueHead]->pRight);
pop();
}
}
int main() {
struct treeNode t1 = { 1 };
struct treeNode t2 = { 2 };
struct treeNode t3 = { 3 };
struct treeNode t4 = { 4 };
struct treeNode t5 = { 5 };
struct treeNode t6 = { 6 };
struct treeNode t7 = { 7 };
struct treeNode t8 = { 8 };
struct treeNode t9 = { 9 };
struct treeNode t10 = { 10 };
// 链接
t1.pLeft = &t2;
t1.pRight = &t3;
t1.pFather = NULL;
t2.pLeft = &t4;
t2.pRight = &t5;
t2.pFather = &t1;
t3.pRight = &t6;
t3.pLeft = NULL;
t3.pFather = &t1;
t4.pLeft = NULL;
t4.pRight = NULL;
t4.pFather = &t2;
t5.pLeft = &t7;
t5.pRight = &t8;
t5.pFather = &t2;
t6.pLeft = &t9;
t6.pRight = &t10;
t6.pFather = &t3;
t7.pLeft = NULL;
t7.pRight = NULL;
t7.pFather = &t5;
t8.pLeft = NULL;
t8.pRight = NULL;
t8.pFather = &t5;
t9.pLeft = NULL;
t9.pRight = NULL;
t9.pFather = &t6;
t10.pLeft = NULL;
t10.pRight = NULL;
t10.pFather = &t6;
look(&t1);
return 0;
}
头动队列层序遍历
//
// Created by Cauchyshy on 2023/5/23.
//
// 层次遍历(广度遍历)从上到下 从左到右一层一层遍历
// 逻辑 利用队列 没有这个就没有直接转换成简洁递归了 有栈可以转简洁递归
// 浪费空间型 头出一个后面不移动 定义头下边遍历 做逻辑移动 让下一个元素代表队头即可
#include <iostream>
#include <cstdio>
using namespace std;
struct treeNode {
int a; // 数据成员
struct treeNode *pFather; // 父节点
struct treeNode *pLeft; // 左孩子
struct treeNode *pRight; // 右孩子
};
#define QUEUS_SIZE 16
// 移动元素型数组队列
struct treeNode *queue[QUEUS_SIZE]; // 队列 只装树的节点
int queueHead = 0; // 队头 默认第一个元素
int queueTail = -1; // 队尾
void push(struct treeNode *node) {
queueTail++; // 尾巴下标+1
queue[queueTail] = node; // 入队
}
struct treeNode *pop(void) {
if (queueHead > queueTail)
return NULL; // 无元素 直接返回
queueHead++;
return queue[queueHead - 1];
}
void look(struct treeNode *root) {
if (root != NULL) {
push(root);
}
while (!(queueHead > queueTail)) {
printf("%d ", queue[queueHead]->a);
if (queue[queueHead]->pLeft != NULL) push(queue[queueHead]->pLeft);
if (queue[queueHead]->pRight != NULL) push(queue[queueHead]->pRight);
pop();
}
}
int main() {
struct treeNode t1 = { 1 };
struct treeNode t2 = { 2 };
struct treeNode t3 = { 3 };
struct treeNode t4 = { 4 };
struct treeNode t5 = { 5 };
struct treeNode t6 = { 6 };
struct treeNode t7 = { 7 };
struct treeNode t8 = { 8 };
struct treeNode t9 = { 9 };
struct treeNode t10 = { 10 };
// 链接
t1.pLeft = &t2;
t1.pRight = &t3;
t1.pFather = NULL;
t2.pLeft = &t4;
t2.pRight = &t5;
t2.pFather = &t1;
t3.pRight = &t6;
t3.pLeft = NULL;
t3.pFather = &t1;
t4.pLeft = NULL;
t4.pRight = NULL;
t4.pFather = &t2;
t5.pLeft = &t7;
t5.pRight = &t8;
t5.pFather = &t2;
t6.pLeft = &t9;
t6.pRight = &t10;
t6.pFather = &t3;
t7.pLeft = NULL;
t7.pRight = NULL;
t7.pFather = &t5;
t8.pLeft = NULL;
t8.pRight = NULL;
t8.pFather = &t5;
t9.pLeft = NULL;
t9.pRight = NULL;
t9.pFather = &t6;
t10.pLeft = NULL;
t10.pRight = NULL;
t10.pFather = &t6;
look(&t1);
return 0;
}
链表队列层序遍历
//
// Created by Cauchyshy on 2023/5/23.
//
// 层次遍历(广度遍历)从上到下 从左到右一层一层遍历
// 逻辑 利用队列 没有这个就没有直接转换成简洁递归了 有栈可以转简洁递归
// 无空头 尾添加 头出
#include <iostream>
#include <cstdio>
#include <malloc.h>
using namespace std;
struct treeNode {
int a; // 数据成员
struct treeNode *pFather; // 父节点
struct treeNode *pLeft; // 左孩子
struct treeNode *pRight; // 右孩子
};
struct queue {
struct treeNode *node; // 指向树的节点
struct queue *next; // 队节点后指针
};
struct queue *queueHead; // 队头 空头
struct queue *queuetail; // 队尾
void push(struct treeNode* node) {
if (NULL == node) {
return ;
}
// 申请空间 并赋值
struct queue *temp = (struct queue*)malloc(sizeof(struct queue));
if (NULL == temp) {
return ;
}
temp->node = node;
temp->next = NULL;
// 链接 判断头尾指针
if (NULL == queueHead) {
queueHead = temp;
queuetail = temp;
} else {
queuetail->next = temp;
queuetail = queuetail->next; // 赋值完尾巴移动
}
}
struct treeNode *pop(void) {
if (NULL == queuetail) {
return NULL;
}
struct queue *tmp = queueHead;
struct treeNode *tp = tmp->node;
queueHead = queueHead->next; // 记录完头后面走
if (NULL == queueHead) {
queuetail = NULL; // 万一为空了
}
free(tmp);
return tp;
}
void look(struct treeNode *root) {
if (root != NULL) {
push(root);
}
while (1) {
struct treeNode *temp = pop();
printf("%d ", temp->a);
if (temp->pLeft != NULL) push(temp->pLeft);
if (temp->pRight != NULL) push(temp->pRight);
if (queueHead == NULL) break;
// pop();
}
}
int main() {
struct treeNode t1 = { 1 };
struct treeNode t2 = { 2 };
struct treeNode t3 = { 3 };
struct treeNode t4 = { 4 };
struct treeNode t5 = { 5 };
struct treeNode t6 = { 6 };
struct treeNode t7 = { 7 };
struct treeNode t8 = { 8 };
struct treeNode t9 = { 9 };
struct treeNode t10 = { 10 };
// 链接
t1.pLeft = &t2;
t1.pRight = &t3;
t1.pFather = NULL;
t2.pLeft = &t4;
t2.pRight = &t5;
t2.pFather = &t1;
t3.pRight = &t6;
t3.pLeft = NULL;
t3.pFather = &t1;
t4.pLeft = NULL;
t4.pRight = NULL;
t4.pFather = &t2;
t5.pLeft = &t7;
t5.pRight = &t8;
t5.pFather = &t2;
t6.pLeft = &t9;
t6.pRight = &t10;
t6.pFather = &t3;
t7.pLeft = NULL;
t7.pRight = NULL;
t7.pFather = &t5;
t8.pLeft = NULL;
t8.pRight = NULL;
t8.pFather = &t5;
t9.pLeft = NULL;
t9.pRight = NULL;
t9.pFather = &t6;
t10.pLeft = NULL;
t10.pRight = NULL;
t10.pFather = &t6;
look(&t1);
return 0;
}