您的位置:首页 > 其它

树的孩子兄弟储存法及其上部分操作

2016-11-13 23:07 232 查看
写这个博客的原因很单纯,武大的考研真题的代码题遇见了,然后衍生出一大堆的问题。为了确保考试的时候万无一失,还是自己亲自动手把最靠谱的代码写出来,做个记录。

简单说一下树,它的定义是一个递归定义,那么这个特性也就很容易让它和递归函数相结合来解决一些实际问题。

准备了一棵树,然后右边对应的是它的孩子兄弟表示法做成的二叉树

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: