Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions 2017-1/zkx/二叉树必选3和可选5/addtree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include "addtree.h"

int j=0;
Status CreateBiTree(BiTree *T, TElemType *p)
{
TElemType ch;

ch = p[j];
j++; //��������ʽ�����ַ�����������Ϊ����
if (ch == '#') //�պ���
{
*T = NULL;
}
else
{
if (!(*T = (BiTree)malloc(sizeof(BiTNode))))
{
return OVERFLOW;
}
(*T)->data = ch; // ���ɸ����
CreateBiTree(&(*T)->lchild, p); // ����������
CreateBiTree(&(*T)->rchild, p); // ����������
}
return OK;
}

ElemType Depth(BiTree T) //��������������
{
if(T == NULL)
{
return OVERFLOW;
}
return Depth(T->lchild) > Depth(T->rchild)
? (Depth(T->lchild) + 1) : (Depth(T->rchild) + 1);
}

int count[50]; //��Ÿ�������
int max=0; //������
int i=0; //��������

int Width(BiTree T) //���������������
{
if(T == NULL)
return 0;
i++;
count[i]++;
if(max < count[i])
max = count[i];
Width(T->lchild);
Width(T->rchild);
i--; //���ظ����
return max;
}

ElemType CountLeaf(BiTree T) //���������Ҷ�ӽ�����
{
ElemType m, n; //�ֱ�Ϊ��������������Ҷ�ӽ�����
if(T == NULL)
return 0;
if(T->lchild == NULL && T->rchild == NULL) //���Һ��Ӷ�Ϊ�գ�˵���ýڵ�ΪҶ�ӽ��
{
return 1;
}
else
{
m = CountLeaf(T->lchild);
n = CountLeaf(T->rchild);
return m+n;
}
}

ElemType Count(BiTree T) //�������������
{
ElemType m, n;
if (T == NULL)
return 0;
if (T->lchild == NULL && T->rchild == NULL) //���Һ��Ӷ�Ϊ�գ�˵���ýڵ�ΪҶ�ӽ��
{
return 1;
}
else
{
m = Count(T->lchild);
n = Count(T->rchild);
return m + n + 1;
}
}

ElemType NotCountLeaf(BiTree T) //��������Ҷ�ӽ�����
{
return Count(T)-CountLeaf(T);
}

int main()
{
BiTree T = NULL;
ElemType w1, w2; //��1��2�Ŀ���

TElemType str1[50] = "ABDG###EH##I#K##C#F##";
TElemType str2[50] = "ABD#F###CE###";

printf("---------���������������߶ȡ������ȡ�Ҷ�ӽ�㡢��Ҷ�ӽ��---------\n\n");

printf("��һ���������� ");
puts(str1);
CreateBiTree(&T,str1);
printf("�߶�Ϊ�� %d",Depth(T)-1);
w1 = Width(T);
printf("\n�������� %d", w1);
printf("\nҶ�ӽ�����Ϊ�� %d", CountLeaf(T));
printf("\n��Ҷ�ӽ�����Ϊ�� %d\n\n", NotCountLeaf(T));

j=0; //���¼���
printf("�ڶ����������� ");
puts(str2);
CreateBiTree(&T,str2);
printf("�߶�Ϊ�� %d",Depth(T)-1);
w2 = Width(T);
printf("\n�������� %d", w2-w1);
printf("\nҶ�ӽ�����Ϊ�� %d", CountLeaf(T));
printf("\n��Ҷ�ӽ�����Ϊ�� %d\n\n", NotCountLeaf(T));
return 0;
}
32 changes: 32 additions & 0 deletions 2017-1/zkx/二叉树必选3和可选5/addtree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>

typedef char TElemType;
typedef int ElemType;

typedef struct BiTNode
{
TElemType data;
struct BiTDode *lchild,*rchild;
}BiTNode,*BiTree;

typedef enum
{
OK,
OVERFLOW,
ERROR
}Status;

Status CreateBiTree(BiTree *T, TElemType *p);

ElemType Depth(BiTree T);

ElemType Width(BiTree T);

ElemType CountLeaf(BiTree T);

ElemType Count(BiTree T);

ElemType NotCountLeaf(BiTree T);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

换个更妥当的函数命名:CountNonLeaf,继续保持动名词结构的函数命名惯例。