当前位置:网站首页>Recursive traversal and non recursive traversal of binary tree (C language version)
Recursive traversal and non recursive traversal of binary tree (C language version)
2022-06-10 11:05:00 【dddd_ jj】
Go straight to the code , It can run directly .
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#include <stdbool.h>
/** Binary tree data structure definition **/
struct treenode
{
char val;
struct treenode *left;
struct treenode *right;
}treenode;
// Build a binary tree
struct treenode* build_tree(char* s,int i)
{
if(i>=strlen(s))return NULL;
if(s[i]=='#')return NULL;
//printf("%d\n",i);
struct treenode* root=(struct treenode*)malloc(sizeof(treenode));
root->val=s[i];
if(2*i+1<strlen(s))root->left=build_tree(s,2*i+1);
else root->left=NULL;
if(2*i+2<strlen(s))root->right=build_tree(s,2*i+2);
else root->right=NULL;
return root;
}
// First root recursion traversal
void preOrder(struct treenode* root)
{
if(root==NULL)return ;
printf("%c",root->val);
if(root->left!=NULL)preOrder(root->left);
if(root->right!=NULL)preOrder(root->right);
}
// Middle root recursive traversal
void inOrder(struct treenode* root)
{
if(root==NULL)return ;
inOrder(root->left);
printf("%c",root->val);
inOrder(root->right);
}
// Post root recursive traversal
void postOrder(struct treenode* root)
{
if(root==NULL)return ;
postOrder(root->left);
postOrder(root->right);
printf("%c",root->val);
}
// First root non recursive traversal
//ABC#DE#
void Pre1Order(struct treenode *bt)
{
struct treenode **s;
struct treenode *p;
int top=-1;
// Create a stack ;
s=(struct treenode **)malloc((100+1)*sizeof(struct treenode *));
// Initialization stack ;
s[++top]=bt;
// Non recursive preamble traversal ;
while(top!=-1)
{
p=s[top--];
printf("%c",p->val); // The characteristics of the stack , First in, then out ;
if(p->right)
s[++top]=p->right;
if(p->left)
s[++top]=p->left;
}
free(s);
}
void In1Order(struct treenode *bt)
{
struct treenode **s;
struct treenode *p,*q;
int top=-1;
// Create a stack ;
s=(struct treenode **)malloc((100+1)*sizeof(struct treenode *));
// Non recursive middle order traversal ;
if(bt)
{
while(bt) // Traverse the left subtree until the left child of the node is empty ;
{
s[++top]=bt; // Put all the left children in the stack ;
bt=bt->left; // Point to the next left subtree ;
}
while(top!=-1) // When the stack is empty, the loop ends ;
{
p=s[top--];// At first, it will be the most p Point to the left child in the lower left corner , And move to the parent node of the node ;
printf("%c",p->val); // Output the node in the lower left corner ;
while(p->right) // Traverse whether the node has a right node after moving ;
{
s[++top]=p->right; // Stack the right subtree of this node ;
q=p->right; // This right subtree node is assigned to q;
while(q->left) // Judge node q Is there a left subtree ;
{
s[++top]=q->left; // There's a Zuozi tree , Put all the left subtrees connected to this node on the stack ;
q=q->left;
}
break; // End the current cycle , Back to the second while The loop continues with the previous step ;
}
}
}
}
void Post1Order(struct treenode *bt)
{
struct treenode **s;
struct treenode *p;
int top=-1;
// Create a stack ;
s=(struct treenode **)malloc((100+1)*sizeof(struct treenode *));
// Non recursive postorder traversal ;
do
{
while(bt) // Traverse the left subtree until the left child of the left subtree is empty ;
{
s[++top]=bt; // Put all the left children in the stack ;
bt=bt->left; // Point to the next left subtree ;
}
p=NULL;
while(top!=-1)
{
bt=s[top];
if(bt->right==p) //p: Expressed as null, Or the right child node has been accessed ;
{
printf("%c",bt->val); // Output node data field ;
top--; // After output ,top--;
p=bt; //p Record the node you just visited ;
}
else
{
bt=bt->right; // Access the right subtree node ;
break;
}
}
}while(top!=-1);
}
int main()
{
char s[100]={
0};
gets(s);
struct treenode* root=(struct treenode*)malloc(sizeof(struct treenode));
root=build_tree(s,0);
printf(" Recursive traversal :\n");
printf(" First root ");
preOrder(root);
printf("\n");
printf(" Nakagan ");
inOrder(root);
printf("\n");
printf(" Posterior root ");
postOrder(root);
printf("\n");
printf(" Non recursive traversal :\n");
printf(" First root ");
Pre1Order(root);
printf("\n");
printf(" Nakagan ");
In1Order(root);
printf("\n");
printf(" Posterior root ");
Post1Order(root);
printf("\n");
return 0;
}
边栏推荐
- 【SignalR全套系列】之在.Net6中实现SignalR分组通信
- Nucleic acid detection robot
- 使用ApiPost测试接口时需要先登录的接口怎么办(基于Cookie)?
- Digital supply chain collaboration system for digital commerce cloud communication industry: empowering communication enterprises to improve supply business and enhance market competitiveness
- cocoslua在vs2013的调试方法
- Efficiency tools: utools
- Yutai semiconductor rushes to the scientific innovation board: the annual revenue is 830million, and the actual controller is American
- Uniapp implements authorized login
- Hardcore spoiler! With 11 topics and 14 celebrities, dragon dragon dragon community entered Intel meetup and announced the agenda!
- How to state clearly and concisely the product requirements?
猜你喜欢

Colorui color matching details

Implementation code of several recent good papers (with source code download)

在线文档协作工具,是提高工作效率的第一步

Leetcode 2000. Reverse word prefix
Common shell commands - 02 compression and decompression

Review the growth evaluation of central enterprises and listed companies

Conversion between binary, octal, decimal and hexadecimal (integer plus decimal)

Meetup回顾|DevOps&MLOps如何在企业中解决机器学习困境?

更耐用的游戏真无线耳机,电池超大续航持久,英雄G1上手

数商云通讯行业数字化供应链协同系统:赋能通讯企业改善供应业务,增强市场竞争力
随机推荐
从POP3服务器提取电子邮件
Vite's public directory
企评家分不同维度解析:湖南长城科技信息有限公司企业成长性
Academic lecture: masp for multi label active learning
[first hpm6750 evaluation] RT thread development environment construction and hello world
Digital commerce cloud furniture industry supplier evaluation management system: improve the core competitiveness of furniture enterprises and realize digital collaborative management
珠海高远电能科技有限公司30%股权转让,来自塔米狗分享
How to state clearly and concisely the product requirements?
Why is your next computer a computer? Explore different remote operations
Google Earth Engine(GEE)——国家标识符网格数据集
面经题目01
C#大作业——学生信息管理系统
【通信协议】UART,I2C,SPI复习
常用颜色RGB、灰度值、取色值、透明度。
【黄啊码】如何确保php上传的图片是安全的?
NeRF:用深度学习完成3D渲染任务的蹿红
Kubernetes setting master schedulable and non schedulable
PV operation daily question - ticket sales
诺思格医药通过注册:年营收6亿 实控人武杰为美国籍
链表、栈、队列