• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C语言数据结构二叉树简单应用

c语言 搞代码 4年前 (2022-01-06) 21次浏览 已收录 0个评论

这篇文章主要介绍了C语言数据结构二叉树简单应用的相关资料,需要的朋友可以参考下

 C语言数据结构二叉树简单应用

在计算机科学中,二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree),接下来我就在这里给大家介绍一下二叉树在算法中的简单使用:

我们要完成总共有

(1)二叉树的创建

(2)二叉树的先中后序递归遍历

(3)统计叶子结点的总数

(4)求树的高度

(5)反转二叉树

(6)输出每个叶子结点到根节点的路径

(7)输出根结点到每个叶子结点的路径。

定义二叉树结点类型的结构体

 typedef struct node{ char data; struct node *Lchild; struct node *Rchild; }BiTNode,*BiTree; int cnt=0;//统计叶子节点个数 

二叉树的创建

 BiTNode *Create(){ //二叉树的先序建立 char ch; BiTNode *s; ch=getchar(); if(ch=='#')erchashu return NULL; s=(BiTNode *)malloc(sizeof(BiTNode)); s->data=ch; s->Lchild=Create(); s->Rchild=Create(); return s; } 

二叉树的先序、中序、后序递归遍历

 void PreOrder(BiTree root){   //前序遍历 if(root){ printf("%c ",root->data); PreOrder(root->Lchild); PreOrder(root->Rchild); } } void InOrder(BiTree root){   //中序遍历 if(root){ InOrder(root->Lchild); printf("%c ",root->data); InOrder(root->Rchild); } } void PostOrder(BiTree root){    //后序遍历 if(root){ PostOrder(root->Lchild); PostOrder(root->Rchild); printf("%c ",root->data); } } 

统计叶子结点个数:

 void LeafCountNode(BiTree root){  //统计叶子结点个数 if(root){ if(!root->Lchild && !root->Rchild) cnt++; LeafCountNode(root->Lchild); LeafCountNode(root->Rchild); } } 

输出各个叶子结点值:

 void IInOrder(BiTree root){ //输出各个叶子结点值 if(root){ IInOrder(root->Lchild); if(!root->Lchild && !root->Rchild) printf("%c ",root->data); IInOrder(root->Rchild); } } 

求树的高度:

 int PostTreeDepth(BiTree root){       //求树的高度 int h1,h2,h; if(root==NULL){ return 0; } else{ h1=PostTreeDepth(root->Lchild); h2=PostTreeDepth(root->Rchild); h=(h1>h2?h1:h2)+1; return h; } } 

反转二叉树:

 void MirrorTree(BiTree root){        //二叉树镜像树 BiTree t; if(root==NULL) return; else{ t=root->Lchild; root->Lchild=root->Rchild; root->Rchild=t; MirrorTree(root->Lchild); MirrorTree(root->Rchild); } } 

输出每个叶子结点到根节点的路径:

 void OutPutPath(BiTree root,char path[],int len){      //输出每个叶子结点到根节点的路径 if(root){ if(!root->Lchild && !root->Rchild){ printf("%c ",root->data); for(int i=len-1;i>=0;i--) printf("%c ",path[i]); printf("\n"); } path[len]=root->data; OutPutPath(root->Lchild,path,len+1); OutPutPath(root->Rchild,path,len+1); } } 

输出根到每个叶子结点的路径:

 void PrintPath(BiTree root,char path[],int l){     //输出根到每个叶子结点的路径 int len=l-1; if(root){ if(root->Lchild==NULL && root->Rchild==NULL){ path[len]=root->data; for(int i=9;i>=len;i--) printf("%c ",path[i]); printf("\n"); } path[len]=root->data; PrintPath(root->Lchild,path,len); PrintPath(root->Rchild,path,len); } } 

测试代码:

 int main(void){ int h,len; char path[20]; BiTree root; root=Create(); // PreOrder(root); // printf("\n"); // InOrder(root); // printf("\n"); // PostOrder(root); // printf("\n"); // LeafCountNode(root); // printf("叶子结点个数为:%d\n",cnt); // IInOrder(root); h=PostTreeDepth(root); printf("树的高度为:High=%d\n",h); // PrintTree(root,0); // MirrorTree(root); // PrintTree(root,0); // OutPutPath(root,p<a style="color:transparent">来源gao($daima.com搞@代@#码(网</a>ath,0); // PrintPath(root,path,10); return 0; } 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

以上就是C语言数据结构二叉树简单应用的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C语言数据结构二叉树简单应用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址