头文件BiTree.h
typedef int Item; typedef struct node { struct node * lchild; struct node * rchild; Item data; }BiTNode,*BiTree; /*构造一棵新的二叉树*/ BiTree InitBiTree(BiTNode *root); /*生成节点*/ BiTNode *MakeNode(Item item,BiTNode *lchild,BiTNode *rchild); /*释放节点*/ void FreeNode(BiTNode *pnode); /*清空一棵二叉树*/ void ClearBiTree(BiTree tree); /*销毁一棵二叉树*/ void DestroyBiTree(BiTree tree); /*判定是否为空*/ IsEmpty(BiTree tree); /*返回树的深度*/ GetDepth(BiTree tree); /*返回根*/ BiTree GetRoot(BiTree tree); /*返回节点值*/ Item GetItem(BiTNode *pnode); /*设置节点值*/ void SetItem(BiTNode *pnode,Item item); /*设置左子树*/ BiTree SetLChild(BiTree parent,BiTree lchild); /*设置右子树*/ BiTree SetRChild(BiTree parent,BiTree rchild); /*返回左子树*/ BiTree GetLChild(BiTree tree); /*返回右子树*/ BiTree GetRChild(BiTree tree); /*插入新子树*/ BiTree InsertChild(BiTree parent,int lr,BiTree child); /*删除子树*/ void DeleteChild(BiTree parent,int lr); /*先序遍历二叉树*/ PreOrderTraverse(BiTree tree,void(*visit)()); /*中序遍历二叉树*/ InOrderTraverse(BiTree tree,void(*visit)()); /*后序遍历二叉树*/ PostOrderTraverse(BiTree tree,void(*visit)());
#include"BiTree.h" #include<malloc.h> #include<stdlib.h> /*构造一棵新的二叉树*/ 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); } /*判定是否为空*/ 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); } } } /*先序遍历二叉树*/ PreOrderTraverse(BiTree tree,void(*visit)()) { BiTNode * pnode = tree; if(pnode) { visit(pnode->data); PreOrderTraverse(pnode->lchild,visit); PreOrderTraverse(pnode->rchild,visit); } } /*中序遍历二叉树*/ InOrderTraverse(BiTree tree,void(*visit)()) { BiTNode * pnode = tree; if(pnode) { InOrderTraverse(pnode->lchild,visit); visit(pnode->data); InOrderTraverse(pnode->rchild,visit); } } /*后序遍历二叉树*/ PostOrderTraverse(BiTree tree,void(*visit)()) { BiTNode * pnode = tree; if(pnode) { PostOrderTraverse(pnode->lchild,visit); PostOrderTraverse(pnode->rchild,visit); visit(pnode->data); } }
#include"BiTree.h" #include<stdio.h> void print(Item item) { printf("%d ",item); } main() { 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); BiTree tree = InitBiTree(n7); SetLChild(tree,n3); SetRChild(tree,n6); printf("树的深度为:%d",GetDepth(tree)); printf("\n先序遍历如下:"); PreOrderTraverse(tree,print); printf("\n中序遍历如下:"); InOrderTraverse(tree,print); printf("\n后序遍历如下:"); PostOrderTraverse(tree,print); DeleteChild(tree,1); printf("\n后序遍历如下:"); PostOrderTraverse(tree,print); DestroyBiTree(tree); if(IsEmpty(tree)) printf("\n二叉树为空,销毁完毕\n"); }
执行结果如下:
fs@ubuntu:~/qiang/tree$ ./Test 树的深度为:3 先序遍历如下:70 30 10 20 60 40 50 中序遍历如下:10 30 20 70 40 60 50 后序遍历如下:10 20 30 40 50 60 70 二叉树为空,销毁完毕