树的孩子兄弟储存法及其上部分操作
2016-11-13 23:07
232 查看
写这个博客的原因很单纯,武大的考研真题的代码题遇见了,然后衍生出一大堆的问题。为了确保考试的时候万无一失,还是自己亲自动手把最靠谱的代码写出来,做个记录。
简单说一下树,它的定义是一个递归定义,那么这个特性也就很容易让它和递归函数相结合来解决一些实际问题。
准备了一棵树,然后右边对应的是它的孩子兄弟表示法做成的二叉树
View Code
关联代码:
对于问题三:我是真的没把递推的方法想出来,函数递归也是变得相对简单了。
相关代码
完整代码
简单说一下树,它的定义是一个递归定义,那么这个特性也就很容易让它和递归函数相结合来解决一些实际问题。
准备了一棵树,然后右边对应的是它的孩子兄弟表示法做成的二叉树
#include <stdio.h> #include <stdlib.h> #define MAX 100 char * p; typedef struct BTree{ char str; struct BTree * lchild; struct BTree * rchild; }BTree; BTree* creat_tree(){ BTree* temp; while(*p==' ')p++; if(*p=='#'){ p++; return NULL; } if(*p!=' '){ temp = (BTree *)malloc(sizeof(BTree)); temp->str=*p++; temp->lchild=creat_tree(); temp->rchild=creat_tree(); } return temp; } void pre_visit(BTree* node){ printf("%c",node->str); if(node->lchild!=NULL)pre_visit(node->lchild); if(node->rchild!=NULL)pre_visit(node->rchild); } int count_node(BTree * node){ int count=1; if(node==NULL||node->lchild==NULL)return 0; else{ BTree *temp=node->lchild; while(temp->rchild!=NULL){count++;temp=temp->rchild;} } return count; } int pre_visit_count(BTree* node){ if(node==NULL)return 0; int count1=count_node(node); int count2,count3,max; count2=count3=0; if(node->lchild!=NULL)count2=pre_visit_count(node->lchild); if(node->rchild!=NULL)count3=pre_visit_count(node->rchild); max=count1>count2?count1:count2; max=max>count3?max:count3; return max; } int count_leaf(BTree* node){ int count = 0; if (node->lchild==NULL)count++; if(node->lchild!=NULL)count+=count_leaf(node->lchild); if(node->rchild!=NULL)count+=count_leaf(node->rchild); return count; } int main() { char tree[MAX];p=tree; BTree * head; printf("Please input the tree(use char and #)\n要求按照先序遍历的方式输入,加上#进行区分\n例如123# #4##5##:\n"); //scanf("%s",tree); gets(tree); if(*p!='\0'&&*p!=' '&&*p!='#'){ head=(BTree *)malloc(sizeof(BTree)); head->str=*p++; //printf("head is %c",head->str); head->lchild=creat_tree(); head->rchild=creat_tree(); } printf("tree is :\n"); pre_visit(head); //在二叉树构造出来的前提下,开始处理孩子兄弟表示法 //问题一:求出使用孩子兄弟表示法的树的度 printf("那么在前边我设置了两个函数,一个用来计算每个节点的度,另一个用来计算所有度中的最大值\n"); printf("测试用例124#5#6##3###\n"); printf("二叉树对应的树的度为:%d",pre_visit_count(head)); printf("\n树中叶子节点的数量是:\n"); printf("%d",count_leaf(head)); ; return 0; }
View Code
关联代码:
int count_leaf(BTree* node){ int count = 0; if (node->lchild==NULL)count++; if(node->lchild!=NULL)count+=count_leaf(node->lchild); if(node->rchild!=NULL)count+=count_leaf(node->rchild); return count; }
对于问题三:我是真的没把递推的方法想出来,函数递归也是变得相对简单了。
相关代码
int height(BTree* node){ int maxh=0,theight,count; if(node==NULL)return 0; if(node->lchild==NULL)return 1; BTree *temp=node->lchild; while(temp!=NULL){ maxh=(maxh>(count=height(temp))?maxh:count); temp=temp->rchild; } theight=1+maxh; return theight; }
完整代码
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
char * p;
typedef struct BTree{
char str;
struct BTree * lchild;
struct BTree * rchild;
}BTree;
BTree* creat_tree(){
BTree* temp;
while(*p==' ')p++;
if(*p=='#'){
p++;
return NULL;
}
if(*p!=' '){
temp = (BTree *)malloc(sizeof(BTree));
temp->str=*p++;
temp->lchild=creat_tree();
temp->rchild=creat_tree();
}
return temp;
}
void pre_visit(BTree* node){
printf("%c",node->str);
if(node->lchild!=NULL)pre_visit(node->lchild);
if(node->rchild!=NULL)pre_visit(node->rchild);
}
int count_node(BTree * node){
int count=1;
if(node==NULL||node->lchild==NULL)return 0;
else{
BTree *temp=node->lchild;
while(temp->rchild!=NULL){count++;temp=temp->rchild;}
}
return count;
}
int pre_visit_count(BTree* node){
if(node==NULL)return 0;
int count1=count_node(node);
int count2,count3,max;
count2=count3=0;
if(node->lchild!=NULL)count2=pre_visit_count(node->lchild);
if(node->rchild!=NULL)count3=pre_visit_count(node->rchild);
max=count1>count2?count1:count2;
max=max>count3?max:count3;
return max;
}
int count_leaf(BTree* node){ int count = 0; if (node->lchild==NULL)count++; if(node->lchild!=NULL)count+=count_leaf(node->lchild); if(node->rchild!=NULL)count+=count_leaf(node->rchild); return count; }
int height(BTree* node){ int maxh=0,theight,count; if(node==NULL)return 0; if(node->lchild==NULL)return 1; BTree *temp=node->lchild; while(temp!=NULL){ maxh=(maxh>(count=height(temp))?maxh:count); temp=temp->rchild; } theight=1+maxh; return theight; }int main()
{
char tree[MAX];p=tree;
BTree * head;
printf("Please input the tree(use char and #)\n要求按照先序遍历的方式输入,加上#进行区分\n例如123# #4##5##:\n");
//scanf("%s",tree);
gets(tree);
if(*p!='\0'&&*p!=' '&&*p!='#'){
head=(BTree *)malloc(sizeof(BTree));
head->str=*p++;
//printf("head is %c",head->str);
head->lchild=creat_tree();
head->rchild=creat_tree();
}
printf("tree is :\n");
pre_visit(head);
//在二叉树构造出来的前提下,开始处理孩子兄弟表示法
//问题一:求出使用孩子兄弟表示法的树的度
printf("那么在前边我设置了两个函数,一个用来计算每个节点的度,另一个用来计算所有度中的最大值\n");
printf("测试用例124#5#6##3###\n");
printf("二叉树对应的树的度为:%d",pre_visit_count(head));
printf("\n树中叶子节点的数量是:\n");
printf("%d",count_leaf(head));
printf("\n树的高度为:\n");
printf("%d",height(head));
return 0;
}
相关文章推荐
- linux创建文件树,孩子兄弟树(或广义表),创建文件树及其访问
- linux创建文件树,孩子兄弟树(或广义表),创建文件树及其訪问
- 根据输入的带标号的广义表形式的字符序列,建立该树的孩子-兄弟链表储存结构
- C语言实现单链表的基本操作及其部分面试题
- JQuery父节点兄弟节点孩子节点的操作
- 树的孩子兄弟表示法及遍历操作
- 软件设计师2006年11月下午试题5(C语言 树及其孩子-兄弟表示)
- C语言实现单链表的基本操作及其部分面试题
- VC++常用数据类型及其操作详解
- 在 Eclipse Workbench 之外使用 Eclipse GUI,第 3 部分: 添加操作、菜单和工具栏
- VC++常用数据类型及其操作详解
- windows提供的对于文件进行操作的API函数及其功能(转载)
- [转] 应聘Java笔试时可能出现问题及其答案(第四部分)
- 使用.net开发手机管理软件 (九) 短信部分——PDU简介及其格式
- VC++常用数据类型及其操作详解[徐兆元]
- const int * pi 、int const * pi与int * const pi及其操作
- DB2数据库部分日常实用操作
- DELPHI 孩子兄弟表示法 (递归实现)
- INI文件格式及其操作代码
- 【风】的ACE笔记(10)-ACE文件操作(部分转载)