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

树的基本操作(数据结构)

树的创建

//结构结点 
typedef struct Node
{int data;struct Node *leftchild;struct Node *rightchild;
}*Bitree,BitNode;//初始化树 
void Create(Bitree &T)
{int d;printf("输入结点(按0为空结点):");scanf("%d",&d);if(d!=0){T = (Bitree)malloc(sizeof(BitNode));T->data=d;Create(T->leftchild);Create(T->rightchild);}else{T=NULL;return;}
}

遍历树(递归)

//先序遍历 
void PreOrder(Bitree &T)
{Bitree D;D=T;if (D){printf("%d", D->data);PreOrder(D->leftchild);PreOrder(D->rightchild);}
}//中序遍历
void InOrder(Bitree &T)
{Bitree D;D=T;if (T){InOrder(D->leftchild);printf("%d", D->data);InOrder(D->rightchild);}}//后序遍历
void PostOrder(Bitree &T)
{Bitree D;D=T;	if (T){PostOrder(D->leftchild);PostOrder(D->rightchild);printf("%d", D->data);}
}

非递归遍历

#define MaxSize 100
typedef struct{BitNode *data[MaxSize];int top;
}SqStack;//初始化栈
void InitStack(SqStack &S){S.top = -1;
} //判断栈空
bool StackEmpty(SqStack S){if(S.top==-1) return true;//空栈else return false; 
} //进栈
void Push(SqStack &S, BitNode *x){if(S.top==MaxSize-1) return;//满栈S.top = S.top+1;//指针加1S.data[S.top]=x;//新元素入栈  
} //出栈
BitNode* Pop(SqStack &S){if(S.top==-1) return NULL;//空栈BitNode *x;x= S.data[S.top];S.top = S.top-1; return x;
} 
//非递归遍历
void InOrder2(Bitree &T){SqStack S;InitStack(S);//初始化栈 Bitree P=T;//p是遍历指针 while(P||!StackEmpty(S)){if(P){//一路向左 Push(S,P);//当前结点入栈 P=P->leftchild;//左孩子不为空,一直向左走 } else{P=Pop(S);//出栈,并转向右子树 printf("%d",P->data);//输出出栈元素 P=P->rightchild;//向右子树走 }}
}void PreOrder2(Bitree &T){SqStack S;InitStack(S);//初始化栈 Bitree P=T;//p是遍历指针 while(P||!StackEmpty(S)){if(P){//一路向左 printf("%d",P->data);Push(S,P);//当前结点入栈 P=P->leftchild;//左孩子不为空,一直向左走 } else{P=Pop(S);P=P->rightchild; }}
}

层次遍历

#define MaxSize 100
typedef struct{int front,rear;BitNode *data[MaxSize];
}SqQueue;//初始化队列
void InitQueue(SqQueue &Q){Q.front=Q.rear=0;
} //判断队列是否为空
bool QueueEmpty(SqQueue Q){if(Q.front==Q.rear) return true;//空队列else return false; 
}//入队
bool EnQueue(SqQueue &Q,BitNode *x){if((Q.rear+1)%MaxSize==Q.front){//判断队满return false; }Q.data[Q.rear]=x;//新元素入队 Q.rear = (Q.rear+1)%MaxSize;//队尾指针加一取模 让队列循环使用 return true;
} //出队
BitNode* DeQueue(SqQueue &Q){if(Q.front==Q.rear) return NULL;//队列为空 BitNode *x;x = Q.data[Q.front];Q.front=(Q.front+1)%MaxSize;//对头指针后移 return x;
} //层次遍历
void LevelOrder(Bitree &T){SqQueue Q;InitQueue(Q);Bitree P;EnQueue(Q,T);while(!QueueEmpty(Q)){P=DeQueue(Q);printf("%d ",P->data);if(P->leftchild!=NULL)EnQueue(Q,P->leftchild);if(P->rightchild!=NULL)EnQueue(Q,P->rightchild);}
} 

主函数

int main(){Bitree T;Create(T);printf("前序遍历:");PreOrder(T);printf("\n");printf("中序遍历:");InOrder(T);printf("\n");printf("后序遍历:");PostOrder(T);printf("\n");printf("中序遍历(非):");InOrder2(T);printf("\n");printf("前序遍历(非):"); PreOrder2(T);printf("\n");printf("层次遍历:");LevelOrder(T);return 0;
} 

在这里插入图片描述

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

相关文章:

  • Python复刻游戏《贪吃蛇大作战》
  • SpringCloud之Gateway整合Sentinel服务降级和限流
  • 深度学习——深度卷积神经网络(AlexNet)
  • 提高编程效率-Vscode实用指南
  • ES 数据库
  • 面试经典150题——Day14
  • Pika v3.5.1发布!
  • Kotlin中的数组
  • (3) OpenCV图像处理kNN近邻算法-识别摄像头数字
  • 上海亚商投顾:沪指震荡调整 转基因概念股逆势大涨
  • abap中程序跳转(全)
  • 启动速度提升 10 倍:Apache Dubbo 静态化方案深入解析
  • PCB命名规则-allegro
  • [架构之路-240]:目标系统 - 纵向分层 - 应用层 - 应用层协议与业务应用程序的多样化,与大自然生物的丰富多彩,异曲同工
  • 探索数字时代的核心:服务器如何塑造未来并助你成就大业
  • spring6-资源操作:Resources
  • C语言 内存
  • Java设计模式之备忘录模式
  • 深度学习 | Pytorch深度学习实践
  • Elasticsearch7.9.3保姆级安装教程
  • 深入使用探讨 PuppeteerSharp 抓取 LinkedIn 页面的步骤
  • 联合体(共用体)
  • 从零开始:GitFlow详细教程,轻松掌握分支策略
  • 深度学习硬件介绍
  • 利用向导创建MFC
  • MySQL 8.0 OCP认证精讲视频、环境和题库之五 事务、缓存
  • ACL配置
  • 微信小程序修改van-popup的背景颜色
  • SpringCloud-Nacos
  • 动态规划12(Leetcode221最大正方形)