二叉树的一些操作

简介:

 

复制代码
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef struct BiNode{
  5     char data;
  6     struct BiNode* LChild;
  7     struct BiNode* RChild;
  8 }BiNode,*BiTree;
  9 
 10 void CreateBiTree(BiTree &T);//创建二叉树
 11 int GetTreeHeight(BiTree T);//返回二叉树高度
 12 int GetAllNode(BiTree T);//返回二叉树结点数
 13 int GetZeroNode(BiTree T);//返回出度为0的结点数
 14 int GetOneNode(BiTree T);//返回出度为1的结点数
 15 int GetTwoNode(BiTree T);//返回出度为2的结点数
 16 BiTree CopyBiTree(BiTree T);//复制二叉树并将副本返回
 17 BiTree GetExchange(BiTree T);//交换二叉树左右子树并返回交换后的二叉树
 18 int IsSimilar(BiTree T1,BiTree T2);//判断两个二叉树是否相似
 19 int GetNodePosition(BiTree T,char c);//获取结点c在二叉树中所在层数
 20 
 21 void main()
 22 {
 23     char clear,c;
 24     BiTree T1,T2,copyT1;
 25     CreateBiTree(T1);
 26     scanf("%c",&clear);
 27 
 28     CreateBiTree(T2);
 29     scanf("%c",&clear);
 30 
 31     printf("Height:%d\n",GetTreeHeight(T1));
 32     printf("All Node:%d\n",GetAllNode(T1));
 33     printf("Zero Node:%d\n",GetZeroNode(T1));
 34     printf("One Node:%d\n",GetOneNode(T1));
 35     printf("Two Node:%d\n",GetTwoNode(T1));
 36 
 37     scanf("%c",&c);
 38     printf("%c at %d\n",c,GetNodePosition(T1,c));
 39 
 40     copyT1 = CopyBiTree(T1);
 41 
 42     if (IsSimilar(T1,T2))
 43     {
 44         printf("Similar!\n");
 45     }
 46     else
 47     {
 48         printf("No Similar!\n");
 49     }
 50 }
 51 
 52 void CreateBiTree(BiTree &T)
 53 {
 54     char c;
 55     scanf("%c",&c);
 56     if (c == ' ')
 57         T = NULL;
 58     else
 59     {
 60         T = (BiNode *)malloc(sizeof(BiNode));
 61         T->data = c;
 62         CreateBiTree(T->LChild);
 63         CreateBiTree(T->RChild);
 64     }
 65 }
 66 int GetTreeHeight(BiTree T)
 67 {
 68     int l,r;
 69     l = r = 0;
 70     if (T->LChild)    l = GetTreeHeight(T->LChild);
 71     if (T->RChild)    r = GetTreeHeight(T->RChild);
 72     if (T->LChild == NULL && T->RChild == NULL)    l=r=1;
 73     return l>r?l:r;
 74 }
 75 int GetAllNode(BiTree T)
 76 {
 77     return T==NULL?0:(GetAllNode(T->LChild)+GetAllNode(T->RChild)+1);
 78 }
 79 int GetZeroNode(BiTree T)
 80 {
 81     if (T==NULL)    return 0;
 82     return (T->LChild==NULL&&T->RChild==NULL)?1:(GetZeroNode(T->LChild)+GetZeroNode(T->RChild));
 83 }
 84 int GetOneNode(BiTree T)
 85 {
 86     int i;
 87     if (T==NULL)    return 0;
 88     i = GetOneNode(T->LChild)+GetOneNode(T->RChild);
 89     if ((T->LChild||T->RChild) && !(T->LChild && T->RChild))    i++;
 90     return i;
 91 }
 92 int GetTwoNode(BiTree T)
 93 {
 94     int i;
 95     if(T == NULL )    return 0;
 96     i = GetTwoNode(T->LChild)+GetTwoNode(T->RChild);
 97     if (T->LChild && T->RChild)    i++;
 98     return i;
 99 }
100 BiTree CopyBiTree(BiTree T)
101 {
102     BiTree cT;
103     if (T == NULL)    return NULL;
104     cT = (BiNode *)malloc(sizeof(BiNode));
105     cT->data = T->data;
106     cT->LChild = CopyBiTree(T->LChild);
107     cT->RChild = CopyBiTree(T->RChild);
108     return cT;
109 }
110 BiTree GetExchange(BiTree T)
111 {
112     BiTree p;
113     if(T == NULL)    return NULL;
114     p = GetExchange(T->LChild);
115     T->LChild = GetExchange(T->RChild);
116     T->RChild = p;
117     return T;
118 }
119 int IsSimilar(BiTree T1,BiTree T2)
120 {
121     int l,r;
122     if (T1 == NULL && T2 == NULL)    return 1;
123     if (T1==NULL&&T2 || T1&&T2==NULL)    return 0;
124     l = IsSimilar(T1->LChild,T2->LChild);
125     r = IsSimilar(T1->RChild,T2->RChild);
126     return l&&r?1:0;
127 }
128 int GetNodePosition(BiTree T,char c)
129 {
130     int l=0,r=0;
131     if (T->data == c)    return 1;
132     if (T->LChild!=NULL)    l=GetNodePosition(T->LChild,c);
133     if (T->RChild!=NULL)    r=GetNodePosition(T->RChild,c);
134     return    l+r?l+r+1:0;
135 }
复制代码

 



本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/archive/2012/10/10/2718988.html,如需转载请自行联系原作者

相关文章
|
4天前
|
人工智能 运维 安全
|
1天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
9天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
796 109
|
3天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
370 9
|
2天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。
|
3天前
|
机器学习/深度学习 传感器 算法
Edge Impulse:面向微型机器学习的MLOps平台——论文解读
Edge Impulse 是一个面向微型机器学习(TinyML)的云端MLOps平台,致力于解决嵌入式与边缘设备上机器学习开发的碎片化与异构性难题。它提供端到端工具链,涵盖数据采集、信号处理、模型训练、优化压缩及部署全流程,支持资源受限设备的高效AI实现。平台集成AutoML、量化压缩与跨硬件编译技术,显著提升开发效率与模型性能,广泛应用于物联网、可穿戴设备与边缘智能场景。
184 127