求二叉树有多少个度为2、0的结点
2013-12-23 19:28
253 查看
#include<stdio.h> #include<malloc.h> //2013-12-23 //乾卦 #define MAX 256 typedef struct Node { char data; struct Node *rchild; struct Node *lchild; }BTNode; //创建二叉树 void CreatBT(BTNode *&root,char *str) { int top=0; int j=0; BTNode *st[MAX]; BTNode *p=NULL; int flg=0; root=NULL; while(str[j]!='\0') { if(str[j]=='(') { top++; st[top]=p; flg=1; } else if(str[j]==')') { top--; } else if(str[j]==',') { flg=2; } else { p=(BTNode*)malloc(sizeof(BTNode)); if(!p) { printf("申请空间失败!\n"); return ; } p->data=str[j]; p->lchild=NULL; p->rchild=NULL; if(!root) { root=p; } else { if(flg==1) st[top]->lchild=p; if(flg==2) st[top]->rchild=p; } } j++; } return ; } //求二叉树有多少个度为2的结点 int Func(BTNode *root) { int flg=0; int count=0; BTNode* queue[MAX]; BTNode* p=NULL; int rear=-1,front=-1; if(!root) return -1; rear++; queue[rear]=root; while(rear!=front) { front++; p=queue[front]; flg=0; if(p->lchild) { //进队 rear++; queue[rear]=p->lchild; flg++; } if(p->rchild) { rear++; queue[rear]=p->rchild; flg++; } if(2==flg) count++; } return count; } void main() { int i; BTNode *ROOT=NULL; CreatBT(ROOT,"A(B(D(,G)),C(E,F))"); i=Func(ROOT); printf("一共有%d个度为2的结点\n",i); //完全二叉树 1+2+1=4 CreatBT(ROOT,"A(B(D(H,I),E),C(F,G))"); i=Func(ROOT); printf("一共有%d个度为2的结点\n",i); //高度为4的满二叉树 1+2+4=7 CreatBT(ROOT,"A(B(D(H,I),E(J,K)),C(F(L,M),G(N,O)))"); i=Func(ROOT); printf("一共有%d个度为2的结点\n",i); //0 CreatBT(ROOT,"A(,B(C(,D(E))))"); i=Func(ROOT); printf("一共有%d个度为2的结点\n",i); //0 CreatBT(ROOT,"A(B,)"); i=Func(ROOT); printf("一共有%d个度为2的结点\n",i); // CreatBT(ROOT,"A(B(D,E),C(F(H(J,K),I),G))"); i=Func(ROOT); printf("一共有%d个度为2的结点\n",i); return ; }
#include<stdio.h> #include<malloc.h> //2013-12-24 //乾卦 #define MAX 256 typedef struct Node { char data; struct Node *rchild; struct Node *lchild; }BTNode; //创建二叉树 void CreatBT(BTNode *&root,char *str) { int top=0; int j=0; BTNode *st[MAX]; BTNode *p=NULL; int flg=0; root=NULL; while(str[j]!='\0') { if(str[j]=='(') { top++; st[top]=p; flg=1; } else if(str[j]==')') { top--; } else if(str[j]==',') { flg=2; } else { p=(BTNode*)malloc(sizeof(BTNode)); if(!p) { printf("申请空间失败!\n"); return ; } p->data=str[j]; p->lchild=NULL; p->rchild=NULL; if(!root) { root=p; } else { if(flg==1) st[top]->lchild=p; if(flg==2) st[top]->rchild=p; } } j++; } return ; } int SumLeaf(BTNode *root) { int flg=0; int count1=0,count2=0,count_sum=0; BTNode* queue[MAX]; BTNode* p=NULL; int rear=-1,front=-1; if(!root) return -1; rear++; queue[rear]=root; while(rear!=front) { front++; p=queue[front]; //计算总结点数 count_sum++; flg=0; if(p->lchild) { //进队 rear++; queue[rear]=p->lchild; flg++; } if(p->rchild) { rear++; queue[rear]=p->rchild; flg++; } if(1==flg) { count1++; } if(2==flg) { count2++; } } //总结点-结点数为1的-结点数为2的 return (count_sum-count2-count1); } void main() { int i; BTNode *ROOT=NULL; CreatBT(ROOT,"A(B(D(,G)),C(E,F))"); i=SumLeaf(ROOT); printf("一共有%d个度为0的结点\n",i); //高度为4的满二叉树 1+2+4=7 CreatBT(ROOT,"A(B(D(H,I),E(J,K)),C(F(L,M),G(N,O)))"); i=SumLeaf(ROOT); printf("一共有%d个度为0的结点\n",i); return ; }
比上面简单的算法:
#include<stdio.h> #include<malloc.h> //2013-12-24 //乾卦 #define MAX 256 typedef struct Node { char data; struct Node *rchild; struct Node *lchild; }BTNode; //创建二叉树 void CreatBT(BTNode *&root,char *str) { int top=0; int j=0; BTNode *st[MAX]; BTNode *p=NULL; int flg=0; root=NULL; while(str[j]!='\0') { if(str[j]=='(') { top++; st[top]=p; flg=1; } else if(str[j]==')') { top--; } else if(str[j]==',') { flg=2; } else { p=(BTNode*)malloc(sizeof(BTNode)); if(!p) { printf("申请空间失败!\n"); return ; } p->data=str[j]; p->lchild=NULL; p->rchild=NULL; if(!root) { root=p; } else { if(flg==1) st[top]->lchild=p; if(flg==2) st[top]->rchild=p; } } j++; } return ; } //求二叉树有多少个度为0的结点 int _SumLeaf(BTNode *root) { int flg=0; int count=0; BTNode* queue[MAX]; BTNode* p=NULL; int rear=-1,front=-1; if(!root) return -1; rear++; queue[rear]=root; while(rear!=front) { front++; p=queue[front]; flg=0; if(p->lchild) { //进队 rear++; queue[rear]=p->lchild; flg++; } if(p->rchild) { rear++; queue[rear]=p->rchild; flg++; } if(0==flg) count++; } return count; } void main() { int i; BTNode *ROOT=NULL; CreatBT(ROOT,"A(B(D(,G)),C(E,F))"); i=_SumLeaf(ROOT); printf("一共有%d个度为0的结点\n",i); //高度为4的满二叉树 CreatBT(ROOT,"A(B(D(H,I),E(J,K)),C(F(L,M),G(N,O)))"); i=_SumLeaf(ROOT); printf("一共有%d个度为0的结点\n",i); return ; }
相关文章推荐
- LeetCode 96. Unique Binary Search Trees Python Solution, Catalan数 结点数为n的不同形态的二叉树一共有多少种
- 设一棵完全二叉树共有700个结点,则该二叉树共有多少个叶子结点?
- N个结点能组成多少种二叉树
- n个结点共可构造多少种二叉树?
- n个结点共可构造多少种二叉树?
- CF507C 给定一种二叉树的走法,问到第n个叶子结点之前,有多少个结点被走过
- N个结点可构成多少不同的二叉树
- Catalan公式:含有n个结点的不相似的二叉树有多少棵树
- 二叉树两结点的最低共同父结点
- 求中序遍历序列的第一个结点值(二叉树)
- 二叉树的下一个结点
- 南邮 OJ 1019 计算二叉树的高度和结点数
- 在顺序表中插入和删除一个结点平均移动多少个结点
- 求二叉树中所有结点的个数
- 求二叉树两结点最近的共同祖先结点
- 二叉树的最近公共祖先、两个最远节点、第K层结点个数、出现次数超过一半的元素
- 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
- 面试题88:二叉树中结点的最大距离
- 剑指offer_输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径
- 【数据结构_树_Tree_1105】交换二叉树的孩子结点