1.头文件
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
typedef int Status;
#define OK 0
#define ERROR -1
typedef struct tree{
char data;
struct tree* lchild; // 左子树
struct tree* rchild; // 右子树
}*binaryTree, treeNode;
#define PRETRAVERSE {printf("PRETRAVERSE: ");preOrder(root); printf(", LINE: %d\n", __LINE__);}
#define INTRAVERSE {printf("INTRAVERSE: ");inOrder(root); printf(", LINE: %d\n", __LINE__);}
#define POSTTRAVERSE {printf("POSTTRAVERSE: ");postOrder(root);printf(", LINE: %d\n", __LINE__);}
#define FREE printf("FREE: %s-%d\n",(freeBinaryTree(&root) == 0)?"SUCCESS":"NULL", __LINE__);
/*
创建结点
@param data 结点数据
@return 结点指针
*/
treeNode* createNode(char data);
/*
二叉树创建
@param root 二叉树根结点
@return OK:成功, ERROR:失败
*/
Status createBinaryTree(binaryTree* root);
/*
二叉树前序遍历
@param root 二叉树根结点
@return OK:成功, ERROR:失败
*/
Status preOrder(binaryTree root);
/*
二叉树后序遍历
@param root: 二叉树根结点
@return OK:成功, ERROR:失败
*/
Status postOrder(binaryTree root);
/*
二叉树中序遍历
@param root: 二叉树根结点
@return OK:成功, ERROR:失败
*/
Status inOrder(binaryTree root);
/*
二叉树深度
@param root: 二叉树根结点
@return 二叉树深度
*/
int binaryTreeDepth(binaryTree root);
/*
二叉树各度数的结点个数
@param root: 二叉树根结点
@return OK:成功, ERROR:失败
*/
Status binaryTreeDegree(binaryTree root, int* n0, int* n1, int* n2);
/*
二叉树的释放
@param root: 二叉树根结点
@return OK:成功, ERROR:失败
*/
Status freeBinaryTree(binaryTree* root);
#endif
2.源文件
#include "binarytree.h"
int main(int argc, char const *argv[])
{
binaryTree root = NULL;
createBinaryTree(&root);
PRETRAVERSE;
INTRAVERSE;
POSTTRAVERSE;
printf("DEPTH: %d\n", binaryTreeDepth(root));
int n0 = 0, n1 = 0, n2 = 0;
if(!binaryTreeDegree(root, &n0, &n1, &n2))
printf("n0:%d, n1:%d, n2:%d\n", n0, n1, n2);
FREE;
return 0;
}
treeNode* createNode(char data)
{
treeNode* node = (treeNode*)malloc(sizeof(treeNode));
if(node == NULL)
return NULL;
node->data = data;
node->lchild = NULL;
node->rchild = NULL;
return node;
}
Status createBinaryTree(binaryTree* root)
{
if(*root != NULL)
return ERROR;
char c = 0;
printf("Please input data( '#' stands for end):");
scanf(" %c", &c);
if(c == '#')
return OK;
else
{
treeNode* newnode = createNode(c);
if(newnode == NULL)
return ERROR;
*root = newnode;
if (createBinaryTree(&newnode->lchild) != OK) // 递归创建左子树
return ERROR;
if (createBinaryTree(&newnode->rchild) != OK) // 递归创建右子树
return ERROR;
return OK;
}
}
Status preOrder(binaryTree root)
{
if(root == NULL)
return ERROR;
printf("%c", root->data);
preOrder(root->lchild);
preOrder(root->rchild);
return OK;
}
Status postOrder(binaryTree root)
{
if(root == NULL)
return ERROR;
postOrder(root->lchild);
postOrder(root->rchild);
printf("%c", root->data);
return OK;
}
Status inOrder(binaryTree root)
{
if(root == NULL)
return ERROR;
inOrder(root->lchild);
printf("%c", root->data);
inOrder(root->rchild);
return OK;
}
int binaryTreeDepth(binaryTree root)
{
if(root == NULL)
return 0;
else
{
int ldepth = binaryTreeDepth(root->lchild);
int rdepth = binaryTreeDepth(root->rchild);
return (ldepth > rdepth) ? (ldepth + 1) : (rdepth + 1);
}
}
Status binaryTreeDegree(binaryTree root, int* n0, int* n1, int* n2)
{
if(NULL == root)
return OK;
if(root->rchild && root->rchild)
(*n2)++;
else if(!root->rchild && !root->rchild)
(*n0)++;
else
(*n1)++;
binaryTreeDegree(root->lchild,n0,n1,n2);
binaryTreeDegree(root->rchild,n0,n1,n2);
return OK;
}
Status freeBinaryTree(binaryTree* root)
{
if(NULL == *root)
return OK;
freeBinaryTree(&(*root)->lchild);
freeBinaryTree(&(*root)->rchild);
free(*root);*root=NULL;
return OK;
}