#include <stdio.h>
#include <stdlib.h>
/*
*writer :J
*woker :J_1
*/
typedef struct CsNode{ //结构体
char date;
struct Node * firstChild,* nextSibling;
}CsNode, *CsTree;
CsNode* CreateTree()
{
CsNode* bt = NULL;
char ch;
ch = getchar();
if(ch != '#')
{
bt = (CsNode*)malloc(sizeof(CsNode));
bt->date = ch;
bt->firstChild = CreateTree();
bt->nextSibling = CreateTree();
}
return bt;
}
void Inorder(CsNode* head,int *count)
{
if(head != NULL)
{
if(head->firstChild == NULL)
{
(*count)++;
}
Inorder(head->firstChild,count);
//进行数据的增加,
Inorder(head->nextSibling,count);
}
}
void Rootdu(CsNode *head,int *num)
{
head = head->firstChild;
while(head)
{
(*num)++;
head = head->nextSibling;
}
}
*writer :J
*woker :J_1
*/
typedef struct CsNode{ //结构体
char date;
struct Node * firstChild,* nextSibling;
}CsNode, *CsTree;
CsNode* CreateTree()
{
CsNode* bt = NULL;
char ch;
ch = getchar();
if(ch != '#')
{
bt = (CsNode*)malloc(sizeof(CsNode));
bt->date = ch;
bt->firstChild = CreateTree();
bt->nextSibling = CreateTree();
}
return bt;
}
void Inorder(CsNode* head,int *count)
{
if(head != NULL)
{
if(head->firstChild == NULL)
{
(*count)++;
}
Inorder(head->firstChild,count);
//进行数据的增加,
Inorder(head->nextSibling,count);
}
}
void Rootdu(CsNode *head,int *num)
{
head = head->firstChild;
while(head)
{
(*num)++;
head = head->nextSibling;
}
}
void PreOrder(CsNode *T,int* maxDge)
{
int count = 0;
if(T != NULL)
{
Rootdu(T,&count);
if(count > *maxDge) *maxDge = count;
PreOrder(T->firstChild,maxDge);
PreOrder(T->nextSibling,maxDge);
}
}
int main(int argc, char *argv[])
{
int count = 0;
int number = 0;
CsNode* head;
head = CreateTree();
Inorder(head,&count);
//printf("%d ",count);
PreOrder(head,&number);
int num = 0;
Rootdu(head,&num);
printf("%d %d %d",count,num,number);
return 0;
}
{
int count = 0;
if(T != NULL)
{
Rootdu(T,&count);
if(count > *maxDge) *maxDge = count;
PreOrder(T->firstChild,maxDge);
PreOrder(T->nextSibling,maxDge);
}
}
int main(int argc, char *argv[])
{
int count = 0;
int number = 0;
CsNode* head;
head = CreateTree();
Inorder(head,&count);
//printf("%d ",count);
PreOrder(head,&number);
int num = 0;
Rootdu(head,&num);
printf("%d %d %d",count,num,number);
return 0;
}
一棵树采用孩子兄弟表示法存储,每个结点的值为一个字母,要求:
(1)编写算法,输入该树对应的二叉树的先序序列,二叉链表指针为空的位置输入“#”,建立二叉链表;
(2)编写算法,计算并输出该树的度。 (注意:是树的度)
Input
输入该树对应的二叉树的先序序列,二叉链表的空指针的位置输入“#”
Output
树的度
Sample Input
ABC##DE#F#G####
Sample Output
3