#include"bintree.h"
#include<malloc.h>
#include<stdlib.h>
void print(Item item)
{
printf("%d ",item);
}
BiTree InitBiTree(BiTNode *root)
{
BiTree tree = root;
return tree;
}
BiTNode *MakeNode(Item item,BiTNode *lchild,BiTNode *rchild)
{
BiTNode * pnode = (BiTNode *)malloc(sizeof(BiTNode));
if(pnode)
{
pnode->data = item;
pnode->lchild = lchild;
pnode->rchild = rchild;
}
return pnode;
}
void FreeNode(BiTNode *pnode)
{
if(pnode!=NULL)
free(pnode);
}
void ClearBiTree(BiTree tree)
{
BiTNode * pnode = tree;
if(pnode->lchild!=NULL)
ClearBiTree(pnode->lchild);
if(pnode->rchild!=NULL)
ClearBiTree(pnode->rchild);
FreeNode(pnode);
}
void DestroyBiTree(BiTree tree)
{
if(tree)
ClearBiTree(tree);
}
int IsEmpty(BiTree tree)
{
if(tree==NULL)
return 0;
else
return 1;
}
int GetDepth(BiTree tree)
{
int cd,ld,rd;
cd = ld = rd = 0;
if(tree)
{
ld = GetDepth(tree->lchild);
rd = GetDepth(tree->rchild);
cd = (ld > rd ? ld : rd);
return cd+1;
}
else
return 0;
}
BiTree GetRoot(BiTree tree)
{
return tree;
}
Item GetItem(BiTNode *pnode)
{
return pnode->data;
}
void SetItem(BiTNode *pnode,Item item)
{
pnode->data = item;
}
BiTree SetLChild(BiTree parent,BiTree lchild)
{
parent->lchild = lchild;
return lchild;
}
BiTree SetRChild(BiTree parent,BiTree rchild)
{
parent->rchild = rchild;
return rchild;
}
BiTree GetLChild(BiTree tree)
{
if(tree)
return tree->lchild;
return NULL;
}
BiTree GetRChild(BiTree tree)
{
if(tree)
return tree->rchild;
return NULL;
}
BiTree InsertChild(BiTree parent,int lr,BiTree child)
{
if(parent)
{
if( lr == 0 && parent->lchild == NULL)
{
parent->lchild = child;
return child;
}
if( lr == 1 && parent->rchild == NULL)
{
parent->rchild = child;
return child;
}
}
return NULL;
}
void DeleteChild(BiTree parent,int lr)
{
if(parent)
{
if( lr == 0 && parent->lchild != NULL)
{
parent->lchild = NULL;
FreeNode(parent->lchild);
}
if( lr == 1 && parent->rchild != NULL)
{
parent->rchild = NULL;
FreeNode(parent->rchild);
}
}
}
void PreOrderTraverse(BiTree tree,void(*visit)())
{
BiTNode * pnode = tree;
if(pnode)
{
visit(pnode->data);
PreOrderTraverse(pnode->lchild,visit);
PreOrderTraverse(pnode->rchild,visit);
}
}
void InOrderTraverse(BiTree tree,void(*visit)())
{
BiTNode * pnode = tree;
if(pnode)
{
InOrderTraverse(pnode->lchild,visit);
visit(pnode->data);
InOrderTraverse(pnode->rchild,visit);
}
}
void PostOrderTraverse(BiTree tree,void(*visit)())
{
BiTNode * pnode = tree;
if(pnode)
{
PostOrderTraverse(pnode->lchild,visit);
PostOrderTraverse(pnode->rchild,visit);
visit(pnode->data);
}
}
//交换左右子数节点
void exange(BiTree root)
{
BiTree p;
if (root)
{
exange(root->lchild);
exange(root->rchild);
p = root->lchild;
root->lchild = root->rchild;
root->rchild = p;
}
}
//二叉树深度
int depthpreorder(BiTree root)
{
int lh,rh;
int max;
if (NULL != root)
{
lh = depthpreorder(root->lchild);
rh = depthpreorder(root->rchild);
max = (lh > rh) ? lh : rh;
return 1 + max;
}
else
return 0;
}
//求二叉树宽度,结合width函数,n值由主程序设为1。
int levelcount(BiTree t, int a[], int h)
{
if (t != NULL)
{
a[h] = a[h] + 1;
levelcount(t->lchild, a, h+1);
levelcount(t->rchild, a, h+1);
}
}
//求二叉树宽度,结合levelcount函数
int width(BiTree t)
{
int a[16],i,wid;
for (i = 0; i < 16; i++)
a[i] = 0;
levelcount(t,a,1);
wid = a[0];
for (i = 1; i < 16; i++)
if(a[i]>wid)
wid = a[i];
return wid;
}
//统计叶子节点总数
int sum_leaves(BiTree t)
{
if(t == NULL)
return 0;
if (t->lchild == NULL && t->rchild == NULL)
return 1;
return sum_leaves(t->lchild)+sum_leaves(t->rchild);
}
//打印叶子节点
void print_leaves(BiTree t)
{
if (t != NULL)
{
if(t->rchild == NULL && t->lchild == NULL)
printf("current leaf : %d\n",t->data);
print_leaves(t->lchild);
print_leaves(t->rchild);
}
}
main()
{
int depth = 0;
int wid = 0;
int leaves = 0;
int *max = NULL;
BiTNode * n1 = MakeNode(10,NULL,NULL);
BiTNode * n2 = MakeNode(20,NULL,NULL);
BiTNode * n3 = MakeNode(30,n1,n2);
BiTNode * n4 = MakeNode(40,NULL,NULL);
BiTNode * n5 = MakeNode(50,NULL,NULL);
BiTNode * n6 = MakeNode(60,n4,n5);
BiTNode * n7 = MakeNode(70,NULL,NULL);
BiTNode * n8 = MakeNode(80,NULL,NULL);
BiTNode * n9 = MakeNode(19,NULL,NULL);
BiTNode * n10 = MakeNode(20,NULL,NULL);
BiTNode * n11 = MakeNode(11,NULL,NULL);
BiTNode * n12 = MakeNode(12,NULL,NULL);
BiTNode * n13 = MakeNode(13,NULL,NULL);
BiTree tree = InitBiTree(n7);
SetLChild(tree,n3);
SetRChild(tree,n6);
SetLChild(n4,n8);
SetRChild(n4,n9);
SetLChild(n5,n10);
SetRChild(n5,n11);
SetLChild(n2,n12);
SetRChild(n2,n13);
//printf("tree depth : %d",GetDepth(tree));
printf("\npreorder : ");
PreOrderTraverse(tree,print);
printf("\nafter exange :\n");
exange(tree);
PreOrderTraverse(tree,print);
depth = depthpreorder(tree);
printf("\ndepth : %d\n",depth);
wid = width(tree);
printf("width : %d\n",wid);
leaves = sum_leaves(tree);
printf("leaves : %d\n",leaves);
print_leaves(tree);
maxvalue(tree, &max);
printf("max value : %d\n",*max);
//printf("\nInorder : ");
//InOrderTraverse(tree,print);
//printf("\npostorder : ");
//PostOrderTraverse(tree,print);
//DeleteChild(tree,1);
//printf("\npostorder : ");
//PostOrderTraverse(tree,print);
//DestroyBiTree(tree);
//if(IsEmpty(tree))
//printf("\nbinary tree is null, Destory done\n");
}