-
Notifications
You must be signed in to change notification settings - Fork 46
二叉树高度,宽度,叶子结点,非叶子结点的计算 #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
换个更妥当的函数命名:
CountNonLeaf,继续保持动名词结构的函数命名惯例。