当前位置: 首页 > news >正文

树的非递归遍历(层序)

层序是采用队列的方式来遍历的

就比如说上面这颗树

他层序的就是:1 24 356

 

void LevelOrder(BTNode* root)
{Que q;QueueInit(&q);if (root){QueuePush(&q, root);}while (!QueueEmpty(&q)){BTNode* front = QueueFront(&q);QueuePop(&q);printf("%d ", front->val);if (front->left){QueuePush(&q, front->left);}if (front->right){QueuePush(&q, front->right);}}}

打印NULL ,这里front也会把NULL带进去

void LevelOrder(BTNode* root)
{Que q;QueueInit(&q);if (root){QueuePush(&q, root);}while (!QueueEmpty(&q)){BTNode* front = QueueFront(&q);QueuePop(&q);if (front){printf("%d ", front->val);QueuePush(&q, front->left);QueuePush(&q, front->right);}else{printf("N ");}}}

 完整代码(栈和队列是复制前几篇的代码)

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int BinTreeType;
struct BinTreeNode
{struct BinTreeNode* left;struct BinTreeNode* right;BinTreeType val;}; 
typedef struct BinTreeNode BTNode;BTNode* BuyBTNode(BinTreeType val);
BTNode* CreateTree();
void PreOrder(BTNode* root);
void InOrder(BTNode* root);
void PostOrder(BTNode* root);
int TreeSize(BTNode* root);
int MaxDepth(BTNode* root);
int TreeLevel(BTNode* root, int k);
BTNode* TreeFind(BTNode* root, int x);
int BinaryTreeLeafSize(BTNode* root);

 

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#include"BinTree.h"
typedef BTNode* QueueData;
struct QueueNode
{QueueData val;struct QueueNode* next;
};
typedef struct QueueNode QNode;
typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Que;
void QueueInit(Que* pq);//队列初始化
void QueueDestroy(Que* pq);//队列销毁void QueuePush(Que* pq,QueueData x);//入队列
void QueuePop(Que* pq);//出队列QueueData QueueBack(Que* pq);
QueueData QueueFront(Que* pq);
bool QueueEmpty(Que* pq);
int QueueSize(Que* pq);

#define _CRT_SECURE_NO_WARNINGS 1
#include"BinTree.h"
BTNode* BuyBTNode(BinTreeType val)
{BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));if (newnode == NULL){perror("malloc fail!");exit(1);}newnode->val = val;newnode->left = NULL;newnode->right = NULL;return newnode;
}
BTNode* CreateTree()
{BTNode* n1 = BuyBTNode(1);BTNode* n2 = BuyBTNode(2);BTNode* n3 = BuyBTNode(3);BTNode* n4 = BuyBTNode(4);BTNode* n5 = BuyBTNode(5);BTNode* n6 = BuyBTNode(6);n1->left = n2;n1->right = n4;n2->left = n3;n2->right = NULL;n3->left = NULL;n3->right = NULL;n4->left = n5;n4->right = n6;n5->left = NULL;n5->right = NULL;n6->left = NULL;n6->right = NULL;return n1;
}
void PreOrder(BTNode* root)
{if (root == NULL){printf("NULL ");return ;}printf("%d ", root->val);PreOrder(root->left);PreOrder(root->right);
}
void InOrder(BTNode* root)
{if (root == NULL){printf("NULL ");return;}InOrder(root->left);printf("%d ", root->val);InOrder(root->right);
}
void PostOrder(BTNode* root)
{if (root == NULL){printf("NULL ");return;}PostOrder(root->left);PostOrder(root->right);printf("%d ", root->val);
}
int TreeSize(BTNode* root)
{if (root == NULL){return 0;}else{return TreeSize(root->left) + TreeSize(root->right)+1;}}int MaxDepth(BTNode* root)
{if (root == NULL){return 0;}int leftDepth = MaxDepth(root->left);int rightDepth = MaxDepth(root->left);if (leftDepth > rightDepth){return leftDepth + 1;}else{return rightDepth + 1;}
}
int TreeLevel(BTNode* root, int k)
{assert(k > 0);if (root == NULL){return 0;}if (k == 1){return 1;}return TreeLevel(root->left, k - 1) + TreeLevel(root->right, k - 1);
}
BTNode* TreeFind(BTNode* root, int x)
{if (root == NULL){return NULL;}if (root->val == x){return root;}BTNode* ret1 = TreeFind(root->left, x);if (ret1){return ret1;}return TreeFind(root->right, x); }
int BinaryTreeLeafSize(BTNode* root)
{if (root == NULL){return 0;}if (BinaryTreeLeafSize(root->left) == NULL && BinaryTreeLeafSize(root->right) == NULL){return 1;	} return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}

 

#define _CRT_SECURE_NO_WARNINGS 1
#include"queue.h"
void QueueInit(Que* pq)
{assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}
void QueueDestroy(Que* pq)
{QNode* pcur = pq->phead;while (pcur){QNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;	
}void QueuePush(Que* pq,QueueData x)
{assert(pq);QNode* newnode = (Que*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(1);}newnode->val = x;newnode->next = NULL;if (pq->ptail){pq->ptail->next = newnode;pq->ptail = newnode;}else{pq->phead = pq->ptail = newnode;}pq->size++;
}
void QueuePop(Que* pq)
{assert(pq->phead != NULL);if (pq->phead->next == NULL){free(pq->phead);pq->phead =pq->ptail= NULL;}else{QNode* next = pq->phead->next;free(pq->phead); pq->phead = next;}pq->size--;
}QueueData QueueFront(Que* pq)
{assert(pq!=NULL);assert(pq->phead != NULL);return pq->phead->val;
}
QueueData QueueBack(Que* pq)
{assert(pq!=NULL);assert(pq->ptail != NULL);return pq->ptail->val;
}
bool QueueEmpty(Que* pq)
{return pq->phead == NULL;
}
int QueueSize(Que* pq)
{assert(pq);return pq->size;}

 

#define _CRT_SECURE_NO_WARNINGS 1
#include"BinTree.h"
#include"queue.h"
void LevelOrder(BTNode* root)
{Que q;QueueInit(&q);if (root){QueuePush(&q, root);}while (!QueueEmpty(&q)){BTNode* front = QueueFront(&q);QueuePop(&q);if (front){printf("%d ", front->val);QueuePush(&q, front->left);QueuePush(&q, front->right);}else{printf("N ");}}}
int main()
{BTNode* root= CreateTree();//PreOrder(root);printf("\n");LevelOrder(root);}

 

http://www.lryc.cn/news/350922.html

相关文章:

  • 解决SpringBoot使用@Transactional进行RestTemplate远程调用导致查询数据记录为null的bug
  • pl/sql基础语法操作
  • Vue 父组件向子组件传递数据
  • 二十五、openlayers官网示例CustomOverviewMap解析——实现鹰眼地图、预览窗口、小窗窗口地图、旋转控件
  • K8S Secret管理之SealedSecrets
  • Gone框架介绍25 - Redis模块参考文档
  • SpringBoot前置知识02-spring注解发展史
  • C++ TCP发送Socket数据
  • 鸿蒙HarmonyOS开发中的易混点归纳-持续补充中
  • ue引擎游戏开发笔记(45)——添加游戏音效
  • 202472读书笔记|《首先你要快乐,其次都是其次》——快乐至上,允许一切发生
  • 8.STL中Vector容器的常见操作(附习题)
  • 5.23小结
  • 文心一言 VS 讯飞星火 VS chatgpt (265)-- 算法导论20.1 4题
  • Flutter 中的 EditableText 小部件:全面指南
  • H800基础能力测试
  • 2024/5/24 Day38 greedy 435. 无重叠区间 763.划分字母区间 56. 合并区间
  • 【python】使用函数名而不加括号是什么情况?
  • 全文检索ElasticSearch简介
  • Github上传时报错The file path is empty的解决办法
  • Adobe Bridge BR v14.0.3 安装教程 (多媒体文件组织管理工具)
  • 嵌入式学习——3——TCP-UDP 数据交互,握手,挥手
  • 【LeetCode】【3】无重复字符的最长子串(1113字)
  • 溪谷联运SDK功能全面解析
  • Vitis HLS 学习笔记--控制驱动TLP - Dataflow视图
  • 蓝桥杯物联网竞赛_STM32L071KBU6_关于sizo of函数产生的BUG
  • Wpf 使用 Prism 实战开发Day22
  • 遍历列表
  • 创建vue工程、Vue项目的目录结构、Vue项目-启动、API风格
  • 为了更全面地分析开发人员容易被骗的原因和提供更加深入的防范措施