[cpp]
//文件名:exp7-1.cpp
#include <stdio.h>
typedef char ElemType;
typedef struct node
{
ElemType data; //數據元素
struct node *lchild; //指向左孩子
struct node *rchild; //指向右孩子
} BTNode;
extern void CreateBTNode(BTNode *&b,char *str);
extern BTNode *FindNode(BTNode *b,ElemType x);
extern BTNode *LchildNode(BTNode *p);
extern BTNode *RchildNode(BTNode *p);
extern int BTNodeDepth(BTNode *b);
extern void DispBTNode(BTNode *b);
extern int BTWidth(BTNode *b);
extern int Nodes(BTNode *b);
extern int LeafNodes(BTNode *b);
extern void DestroyBTNode(BTNode *&b);
int main()
{ BTNode *b,*p,*lp,*rp;;
CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
printf("二叉樹的基本運算如下:\n");
printf(" (1)輸出二叉樹:");DispBTNode(b);printf("\n");
printf(" (2)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("無右孩子 ");
}
printf("\n");
printf(" (3)二叉樹b的深度:%d\n",BTNodeDepth(b));
printf(" (4)二叉樹b的寬度:%d\n",BTWidth(b));
printf(" (5)二叉樹b的節點個數:%d\n",Nodes(b));
printf(" (6)二叉樹b的葉子節點個數:%d\n",LeafNodes(b));
printf(" (7)釋放二叉樹b\n");
DestroyBTNode(b);
}
//文件名:exp7-1.cpp
#include <stdio.h>
typedef char ElemType;
typedef struct node
{
ElemType data; //數據元素
struct node *lchild; //指向左孩子
struct node *rchild; //指向右孩子
} BTNode;
extern void CreateBTNode(BTNode *&b,char *str);
extern BTNode *FindNode(BTNode *b,ElemType x);
extern BTNode *LchildNode(BTNode *p);
extern BTNode *RchildNode(BTNode *p);
extern int BTNodeDepth(BTNode *b);
extern void DispBTNode(BTNode *b);
extern int BTWidth(BTNode *b);
extern int Nodes(BTNode *b);
extern int LeafNodes(BTNode *b);
extern void DestroyBTNode(BTNode *&b);
int main()
{ BTNode *b,*p,*lp,*rp;;
CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
printf("二叉樹的基本運算如下:\n");
printf(" (1)輸出二叉樹:");DispBTNode(b);printf("\n");
printf(" (2)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("無右孩子 ");
}
printf("\n");
printf(" (3)二叉樹b的深度:%d\n",BTNodeDepth(b));
printf(" (4)二叉樹b的寬度:%d\n",BTWidth(b));
printf(" (5)二叉樹b的節點個數:%d\n",Nodes(b));
printf(" (6)二叉樹b的葉子節點個數:%d\n",LeafNodes(b));
printf(" (7)釋放二叉樹b\n");
DestroyBTNode(b);
}