当前位置:网站首页>二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
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;
}
边栏推荐
- 数据库内容输出有问题怎么解决
- Kityformula editor configure font size and spacing
- fatal: unsafe repository is owned by someone else 的解决方法
- CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
- HUSTPC2022
- Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
- Tmall product details interface (APP, H5 end)
- [QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
- . Net core logging system
- CDN 在游戏领域的应用
猜你喜欢

微信小程序使用towxml显示公式

【NOI模拟赛】刮痧(动态规划)

forEach的错误用法,你都学废了吗

Mavn 搭建 Nexus 私服

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

Table responsive layout tips

Fundamentals of software testing

Ad20 cannot select the solution of component packaging in PCB editor
![[C language] explain the initial and advanced levels of the pointer and points for attention (1)](/img/61/1619bd2e959bae1b769963f66bab4e.png)
[C language] explain the initial and advanced levels of the pointer and points for attention (1)

taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
随机推荐
[apipost] tutorial
Xilinx Vivado set *.svh as SystemVerilog Header
编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
C # delay, start the timer in the thread, and obtain the system time
JMeter script parameterization
taobao. logistics. dummy. Send (no logistics delivery processing) interface, Taobao store delivery API interface, Taobao order delivery interface, Taobao R2 interface, Taobao oau2.0 interface
传感器数据怎么写入电脑数据库
[c voice] explain the advanced pointer and points for attention (2)
AtCoder Beginner Contest 254
可视化搭建页面工具的前世今生
HUSTPC2022
Yolov6 training: various problems encountered in training your dataset
jmeter脚本参数化
Printf function and scanf function in C language
Huawei interview question: no palindrome string
【题解】Educational Codeforces Round 82
Have you learned the wrong usage of foreach
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
C#代码审计实战+前置知识
mathjax 入门(web显示数学公式,矢量的)