当前位置:网站首页>二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
2022-07-02 12:00:00 【MASJLE】
#include<stdio.h>
#include<malloc.h>
#define QUEUE_SIZE 5
typedef struct BTNode {
char element;
BTNode* left;
BTNode* right;
}BTNode,*BTNodePtr;
typedef struct BTNodePtrQueue {
BTNodePtr* nodePtrs;
int front;
int rear;
}BTNodePtrQueue, *QueuePtr;
QueuePtr initQueue() {
QueuePtr resultQueuePtr = (QueuePtr)malloc(sizeof(struct BTNodePtrQueue));
resultQueuePtr->nodePtrs = (BTNodePtr*)malloc(QUEUE_SIZE * sizeof(BTNodePtr));
resultQueuePtr->front = 0;
resultQueuePtr->rear = 1;
resultQueuePtr;
}
bool isQueueEmpty(QueuePtr paraQueuePtr) {
if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear) {
return true;
}
return false;
}
void enqueue(QueuePtr paraQueuePtr, BTNodePtr paraBTNodePtr) {
printf("front = %d, rear = %d\n",paraQueuePtr->front,paraQueuePtr->rear);
if ((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE) {
printf("Error, trying to enqueue %c. queue full.\n",paraBTNodePtr->element);
return;
}
paraQueuePtr->nodePtrs[paraQueuePtr->rear] = paraBTNodePtr;
paraQueuePtr->rear = (paraQueuePtr->rear + 1) % QUEUE_SIZE;
printf("enqueue %c ends.\n",paraBTNodePtr->element);
}
BTNodePtr dequeue(QueuePtr paraQueuePtr) {
if (isQueueEmpty(paraQueuePtr)) {
printf("Error, empty queue\n");
return NULL;
}
paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;
printf("dequeue %c ends.\n",paraQueuePtr->nodePtrs[paraQueuePtr->front]->element);
return paraQueuePtr->nodePtrs[paraQueuePtr->front];
}
BTNodePtr constructBTNode(char paraChar) {
BTNodePtr resultPtr = (BTNodePtr)malloc(sizeof(BTNode));
resultPtr->element = paraChar;
resultPtr->right = NULL;
resultPtr->left = NULL;
return resultPtr;
}
BTNodePtr stringToBTree(char* paraString) {
int i;
char ch;
QueuePtr tempQueuePtr = initQueue();
BTNodePtr resultHeader;
BTNodePtr tempParent, tempLeftChild, tempRightChild;
i = 0;
ch = paraString[i];
resultHeader = constructBTNode(ch);
enqueue(tempQueuePtr, resultHeader);
while (!isQueueEmpty(tempQueuePtr)) {
tempParent = dequeue(tempQueuePtr);
i++;
ch = paraString[i];
if (ch == '#') {
tempParent->left = NULL;
}
else {
tempLeftChild = constructBTNode(ch);
enqueue(tempQueuePtr, tempLeftChild);
tempParent->left = tempLeftChild;
}
i++;
ch = paraString[i];
if (ch == '#') {
tempParent->right = NULL;
}
else {
tempRightChild = constructBTNode(ch);
enqueue(tempQueuePtr, tempRightChild);
tempParent->right = tempRightChild;
}
}
return resultHeader;
}
void levelWise(BTNodePtr paraTreePtr) {
char tempString[100];
int i = 0;
QueuePtr tempQueuePtr = initQueue();
BTNodePtr tempNodePtr;
enqueue(tempQueuePtr, paraTreePtr);
while (!isQueueEmpty(tempQueuePtr)) {
tempNodePtr = dequeue(tempQueuePtr);
tempString[i] = tempNodePtr->element;
i++;
if (tempNodePtr->left != NULL) {
enqueue(tempQueuePtr, tempNodePtr->left);
}
if (tempNodePtr->right != NULL) {
enqueue(tempQueuePtr, tempNodePtr->right);
}
}
tempString[i] = ' ';
printf("LevelWise:-> %s\n",tempString);
}
void preorder(BTNodePtr tempPtr) {
if (tempPtr == NULL) {
return;
}
printf("%c",tempPtr->element);
preorder(tempPtr->left);
preorder(tempPtr->right);
}
void inorder(BTNodePtr tempPtr) {
if (tempPtr == NULL) {
return;
}
inorder(tempPtr->left);
printf("%c",tempPtr->element);
inorder(tempPtr->right);
}
void postorder(BTNodePtr tempPtr) {
if (tempPtr == NULL) {
return;
}
postorder(tempPtr->left);
postorder(tempPtr->right);
printf("%c",tempPtr->element);
}
int main() {
BTNodePtr tempHeader;
tempHeader = constructBTNode('c');
printf("There is only one node. preorder visit:");
preorder(tempHeader);
printf("\r\n");
char* tempString = "acde#bf######";
tempHeader = stringToBTree(tempString);
printf("Preorder:-> ");
preorder(tempHeader);
printf("\r\n");
printf("Inorder:-> ");
inorder(tempHeader);
printf("\r\n");
printf("Postorder:-> ");
postorder(tempHeader);
printf("\r\n");
printf("LevelWise: \n");
levelWise(tempHeader);
printf("\r\n");
return 1;
}
边栏推荐
- MFC timer usage
- STM32 standard firmware library function name (I)
- Implementation of n queen in C language
- TiDB跨数据中心部署拓扑
- C thread transfer parameters
- 【题解】Educational Codeforces Round 82
- GeoServer offline map service construction and layer Publishing
- Bit by bit of OpenCV calling USB camera
- 學習使用php實現公曆農曆轉換的方法代碼
- 【C语言】详解指针的初阶和进阶以及注意点(1)
猜你喜欢

buuctf-pwn write-ups (7)
![[noi Simulation Competition] scraping (dynamic planning)](/img/ee/27a07f80207a2925f5065e633eb39f.png)
[noi Simulation Competition] scraping (dynamic planning)

Mavn 搭建 Nexus 私服

Obsidian installs third-party plug-ins - unable to load plug-ins

LeetCode 2320. 统计放置房子的方式数

Add vector formula in rich text editor (MathType for TinyMCE, visual addition)

btrace-(字节码)动态跟踪工具
[email protected] : The platform “win32“ is incompatible with this module."/>info [email protected] : The platform “win32“ is incompatible with this module.

为什么只会编程的程序员无法成为优秀的开发者?

LeetCode - 搜索二维矩阵
随机推荐
Why can't programmers who can only program become excellent developers?
【题解】Educational Codeforces Round 82
Internet Explorer officially retired
Find the maximum inscribed circle of the contour
STM32 standard firmware library function name (I)
Solve the problem that El radio group cannot be edited after echo
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
Large top heap, small top heap and heap sequencing
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
Base64 coding can be understood this way
qml 弹窗框架,可定制
fatal: unsafe repository is owned by someone else 的解决方法
Dragonfly low code security tool platform development path
.NET Core 日志系统
实用调试技巧
Base64 编码原来还可以这么理解
Xilinx Vivado set *. svh as SystemVerilog Header
C code audit practice + pre knowledge
LeetCode 209. 长度最小的子数组
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)