二叉树的链式存储算法库采用程序的多文件组织形式,包括两个文件:
1.头文件:btree.h,包含定义二叉树的链式存储数据结构的代码、宏定义、要实现算法的函数的声明;
#ifndef BTREE_H_INCLUDED
#define BTREE_H_INCLUDED
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *lchild;
struct node *rchild;
} BTNode;
void CreateBTNode(BTNode *&b,char *str);
BTNode *FindNode(BTNode *b,ElemType x);
BTNode *LchildNode(BTNode *p);
BTNode *RchildNode(BTNode *p);
int BTNodeDepth(BTNode *b);
void DispBTNode(BTNode *b);
void DestroyBTNode(BTNode *&b);
#endif // BTREE_H_INCLUDED
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
2.源文件:btree.cpp,包含实现各种算法的函数的定义
#include <stdio.h>
#include <malloc.h>
#include "btree.h"
void CreateBTNode(BTNode *&b,char *str)
{
BTNode *St[MaxSize],*p=NULL;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while (ch!='\0')
{
switch(ch)
{
case '(':
top++;
St[top]=p;
k=1;
break;
case ')':
top--;
break;
case ',':
k=2;
break;
default:
p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;
p->lchild=p->rchild=NULL;
if (b==NULL)
b=p;
else
{
switch(k)
{
case 1:
St[top]->lchild=p;
break;
case 2:
St[top]->rchild=p;
break;
}
}
}
j++;
ch=str[j];
}
}
BTNode *FindNode(BTNode *b,ElemType x)
{
BTNode *p;
if (b==NULL)
return NULL;
else if (b->data==x)
return b;
else
{
p=FindNode(b->lchild,x);
if (p!=NULL)
return p;
else
return FindNode(b->rchild,x);
}
}
BTNode *LchildNode(BTNode *p)
{
return p->lchild;
}
BTNode *RchildNode(BTNode *p)
{
return p->rchild;
}
int BTNodeDepth(BTNode *b)
{
int lchilddep,rchilddep;
if (b==NULL)
return(0);
else
{
lchilddep=BTNodeDepth(b->lchild);
rchilddep=BTNodeDepth(b->rchild);
return (lchilddep>rchilddep)? (lchilddep+1):(rchilddep+1);
}
}
void DispBTNode(BTNode *b)
{
if (b!=NULL)
{
printf("%c",b->data);
if (b->lchild!=NULL || b->rchild!=NULL)
{
printf("(");
DispBTNode(b->lchild);
if (b->rchild!=NULL) printf(",");
DispBTNode(b->rchild);
printf(")");
}
}
}
void DestroyBTNode(BTNode *&b)
{
if (b!=NULL)
{
DestroyBTNode(b->lchild);
DestroyBTNode(b->rchild);
free(b);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
3.在建立算法库过程中,为了完成测试,再同一项目(project)中建立一个源文件(如main.cpp),编制main函数,完成相关的测试工作。
int main()
{
BTNode *b,*p,*lp,*rp;;
printf(" (1)创建二叉树:");
CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
printf("\n");
printf(" (2)输出二叉树:");
DispBTNode(b);
printf("\n");
printf(" (3)查找H节点:");
p=FindNode(b,'H');
if (p!=NULL)
{
lp=LchildNode(p);
if (lp!=NULL)
printf("左孩子为%c ",lp->data);
else
printf("无左孩子 ");
rp=RchildNode(p);
if (rp!=NULL)
printf("右孩子为%c",rp->data);
else
printf("无右孩子 ");
}
else
printf(" 未找到!");
printf("\n");
printf(" (4)二叉树b的深度:%d\n",BTNodeDepth(b));
printf(" (5)释放二叉树b\n");
DestroyBTNode(b);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
注:在main函数中,创建的用于测试的二叉树如下——