本程序实现有关二叉树的递归算法,包括

1.求二叉树的层次(高度)

2.求二叉树的叶子个数

3.求二叉树的总结点个数

4.求二叉树的度为1的结点个数

5.求二叉树的度为2的结点个数

6.复制二叉树

7.交换二叉树的左右子树

8.利用先序序列建立二叉链表存储的二叉树

(二)基本要求

1.用二叉链表的形式存储二叉树

2.利用C的图形功能显示二叉树形态 为蓝底白树

#include

#include

#include

#include

#include

#define OK 1

#define ERROR 0

#define OVERFLOW -2

#define NULL 0

typedef int Status;

typedef char TElemType;

typedef struct BiTNode{

TElemType data;

struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

Status CreateBiTree(BiTree

*T) //先序创建二叉树

{ char data;

scanf("%c",&data);

if(data=='#') *T=NULL;

else {

*T=(BiTree)malloc(sizeof(BiTNode));

if(!(*T)) exit(OVERFLOW);

(*T)->data=data;

CreateBiTree(&(*T)->lchild);

CreateBiTree(&(*T)->rchild);

}

return

OK;

}

Status DestroyBiTree(BiTree

*T) //销毁二叉树 释放内存

{ if((*T)->data==NULL)return

ERROR;

free(*T);

*T=NULL;

return OK;

}

void Paint(BiTree T,int x,int y,int

g) //

打印二叉树树形(递归实现)

{ char c[2];

int you=0,zuo=0;

if(T)

{setcolor(WHITE);

circle(x,y,10);

c[0]=T->data;

c[1]='\0';

outtextxy(x,y,c);

if(T->lchild!=NULL)

{if(x>g)

zuo=(x-g)/2+g;

if(x

line(x-1,y,zuo,y+80);

}

if(T->rchild!=NULL)

{you=2*x-zuo;

line(x-1,y,you,y+80);

}

Paint(T->lchild,zuo,y+80,x);

Paint(T->rchild,you,y+80,x);

}

}

int level(BiTree

T) //求二叉树的层次(高度)

{

int l1,l2;

if(T==NULL) return 0;

else{l1=level(T->lchild);

l2=level(T->rchild);

return l1>l2?l1+1:l2+1;

}

}

int leaf(BiTree

T) //求二叉树的叶子节点数

{

int n1,n2;

if(T==NULL) return 0;

else

if(T->lchild==NULL&&T->rchild==NULL)return

1;

else {n1=leaf(T->lchild);

n2=leaf(T->rchild);

return n1+n2;

}

}

int nodes(BiTree

T) //求二叉树的总节点数

{

int n1,n2;

if(T==NULL) return 0;

else

if(T->lchild==NULL&&T->rchild==NULL)return

1;

else {n1=nodes(T->lchild);

n2=nodes(T->rchild);

return n1+n2+1;

} }

int node1(BiTree

T) //求度为一的节点数目

{

int n1,n2,n=0;

if(T==NULL) return 0;

else {

if(T->lchild&&T->rchild==NULL||T->lchild==NULL&&T->rchild)

n=1;

n1=node1(T->lchild);

n2=node1(T->rchild);

return n1+n2+n;

}

}

int node2(BiTree

T) //求度为二的节点数目

{

int n=0,n1,n2;

if(T==NULL) return 0;

else {

if(T->lchild&&T->rchild)

n=1;

n1=node2(T->lchild);

n2=node2(T->rchild);

return n+n1+n2;

}

}

void Copy(BiTree T,BiTree

*T2) //复制二叉树

{

if(T==NULL) *T2=NULL;

else{*T2=(BiTree)malloc(sizeof(BiTNode));

(*T2)->data=T->data;

Copy(T->lchild,&(*T2)->lchild);

Copy(T->rchild,&(*T2)->rchild);

}

}

void exchange(BiTree

*T) //交换二叉树的所有左右子树

{

BiTNode *t;

if(*T)

{t=(*T)->lchild;

(*T)->lchild=(*T)->rchild;

(*T)->rchild=t;

exchange(&(*T)->lchild);

exchange(&(*T)->rchild);

}

}

main()

{

BiTree T,T2; int x,y,g;

int gdriver,gmode;

int d,yezi,jiedian,dian1,dian2;

x=320;y=30; g=0;

gdriver=DETECT;

printf("input the BiTree's data(in PreOrder and

<=63ge):\n"); //创建

if(!CreateBiTree(&T))printf("kongjian OVERFLOW! can

not create!\n");

else printf("cunchu success!\n");

printf("press any key to contunue:\n");

getch();

printf("level of the tree is

:\n"); //求层数

d=level(T);

printf("%d\n",d);

printf("\n press any key to continue:\n");

getch();

printf("leaf number of the tree is

:\n"); //求叶子

yezi=leaf(T);

printf("%d\n",yezi);

printf("\n press any key to continue:\n");

getch();

printf("nodes number of the tree is

:\n"); //求总节点

jiedian=nodes(T);

printf("%d\n",jiedian);

printf("\n press any key to continue:\n");

getch();

printf("du wei 1 d jiedian node1 of the tree is

:\n"); //求度为一的节点数 dian1=node1(T);

printf("%d\n",dian1);

printf("\n press any key to continue:\n");

getch();

printf("node2 of the tree is

:\n"); //求度为二的节点

dian2=node2(T);

printf("%d\n",dian2);

printf("\n press any key to continue:\n");

getch();

printf("Now copy the

tree:\n"); //复制树

Copy(T,&T2);

printf("\n press any key to continue:\n");

getch();

printf("now,print the BiTree like a

tree:\n"); //打印树

printf("press any key to see the tree:\n");

getch();

initgraph(&gdriver,&gmode,"C:\\TC");

setbkcolor(BLUE);

cleardevice();

Paint(T,x,y,g);

getch();

closegraph();

printf("press any key to continue:\n");

getch();

printf("Now change the tree d right and

left:\n"); //交换左右子树

exchange(&T);

printf("\n press any key to continue:\n");

getch();

printf("now,print the BiTree like a tree:\n");

printf("press any key to see the

tree:\n"); //打印树形以确认交换

getch();

initgraph(&gdriver,&gmode,"C:\\TC");

setbkcolor(BLUE);

cleardevice();

Paint(T,x,y,g);

getch();

closegraph();

printf("press any key to continue:\n");

getch();

if(DestroyBiTree(&T))printf("the BiTree

destroyed!\n"); //销毁树

else printf("NULL!can not destroy!\n");

printf("press any key to

continue:\n"); //结束程序

getch();

}

C语言有关树的编程题,有关二叉树的递归算法 C语言编程相关推荐

  1. c语言编程题素数和,程序设计入门——C语言 第4周编程练习 1 素数和(5分)

    题目内容: 我们认为2是第一个素数,3是第二个素数,5是第三个素数,依次类推. 现在,给定两个整数n和m,0 输入格式: 两个整数,第一个表示n,第二个表示m. 输出格式: 一个整数,表示第n个素数到 ...

  2. c语言编程题考试自动评分系统,C语言编程题考试自动评分系统简介.ppt

    C语言编程题考试自动评分系统简介 主要内容 教材简介 学习方法 机考系统简介 教材简介 教学理念的更新 教材简介 教材简介 国内55所院校使用 新版增加的内容 强化知识点.算法.编程方法与技巧 [编程 ...

  3. c语言编程题输入两个直角边,C语言编程 直角三角形已知两边求第三边

    C语言编程题 求大神解答 好久没写C了,不知道对不对:intsum=0;intindex=0;for(;indexsum+=a[2][index];}returnsum; C语言编程题,求分段函数 刚 ...

  4. c语言写程序思路考研题,快速解题 | 在考场C语言编程题

    原标题:快速解题 | 在考场C语言编程题 对于考<C语言程序设计>的小伙伴们来说,程序设计题是很多同学觉得相对较难的一个版块.其题目虽然不算多,但在考研150分的试卷中,却占据了较大的比重 ...

  5. 全国计算机二级编程题100道,计算机二级C语言100道编程题

    国家计算机二级100道编程题 #include #include int fun(int score[], int m, int below[]) { int i,k=0; float av=0.0; ...

  6. 传热学c语言节点编程题_哈工大苏小红C语言编程题目第二周的答案第一二题12...

    本人亲自测试过,答案完全正确!!! 1输出逆序数(3分) 题目内容:从键盘任意输入一个3位整数,编程计算并输出它的逆序数(忽略整数前的正负号).例如,输入-123,则忽略负号,由123分离出其百位1. ...

  7. 沈阳工业大学c语言编程题,金融工程专业《C语言程序设计》启发式教学探讨

    一.引言 C语言是一门高级程序设计语言,掌握C语言不仅有利于提高学生的思维能力,而且有利于培养学生的编程能力,对学习其他计算机语言课程具有很大的帮助.因此,<C语言程序设计>既是计算机各类 ...

  8. c语言藏头诗编程题,Res - 2009夏学期C语言上机练习参考答案汇总

    编写程序,输入一个月份,输出对应的英文名称,要求用指针数组表示12个月的英文名称. 若输入月份错误,输出提示信息. 输入输出示例:括号内为说明 输入: 3 5 9 14 输出: May Septemb ...

  9. 趣味编程题——漏掉的账目(C语言编写)

           某财务部门结账时发现总金额不对头.很可能是从明细上漏掉了某1笔或几笔.如果已知明细账目清单,能通过编程找到漏掉的是哪1笔或几笔吗? 如果有多种可能,则输出所有可能的情况. 我们规定:用户 ...

最新文章

  1. 农牧行业销售经理生存手册(二)
  2. 数据仓库与数据挖掘的一些基本概念
  3. 极大似然法估计与极大验后法估计
  4. 机器学习物语(3):回归问题
  5. 自定义负载均衡策略:
  6. 阿里 Goldeneye 四个环节落地智能监控:预测、检测、报警及定位
  7. spring boot mybatis 整合_MyBatis学习:MyBatis和Spring整合
  8. 交互式python解释器_从python脚本中调用python交互式解释器
  9. 原生的强大DOM选择器querySelector - querySelector和querySelectorAll
  10. 树莓派导入h5模型出错OSError: SavedModel file does not exist at: model.h5/{saved_model.pbtxt|saved_model.pb}
  11. HTML元素拖拽功能的实现
  12. Panel面板和三种布局管理器
  13. Excel复制单元格样式
  14. 玩转华为数据中心交换机系列 | 配置MUX VLAN示例(汇聚层设备)
  15. 转:vue+canvas如何实现b站萌系登录界面
  16. 【OpenPCDet】Kitti数据集下训练PointPillars并评估可视化
  17. Fiddler跟F12
  18. ElecSuper ESN4485 MOS场效应晶体管
  19. 基于Redis实现查找附近的人
  20. Doxygen 一个程序的文件产生工具,可将程序中的特定批注转换成为说明文件

热门文章

  1. linux 自动务份,Linux下设置ADSL自动拨号上网
  2. asp判断是否移动端_asp判断用户端是电脑访
  3. Python 打造专属照片墙
  4. 华为软件开发云落户青岛,未来将打造“一核两翼”公有云战略
  5. 类Photon游戏服务器引擎Matchvs简介
  6. HDU3999 The order of a Tree【二叉搜索树 + 前序遍历】
  7. HDU3999-The order of a Tree
  8. 哪个技术火就选哪个?切记知乎驱动的技术选型不靠谱!
  9. SQLcl这个可爱的小工具,来了解一下呀~
  10. C++——深拷贝和浅拷贝