bittree.h 文件
#ifndef _BIT_TREE_H_
#define _BIT_TREE_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//树的定义是递归定义的,节点下面又拆分为左子树和右子树
//树是由节点组成,除了叶子节点,其他节点又是相对的根节点,
//相对的根节点下有相对的左右子树
//树的遍历的本质就是走一圈,每一个非叶子节点就是相对的根节点,都经过3次
//第一次访问为先序,第二次访问为中序,第三次访问为后序
#ifndef bool
#define bool int
#define true 1
#define false 0
#endif
//数的头首地址就可以用第一个节点的地址表示
typedef struct BitNode
{
int data;
struct BitNode *lchild,*rchild;
}BitNode,*BitTree;
#ifdef __cplusplus
extern "C"{
#endif
extern void preOrder(BitNode* root);
extern void midOrder(BitNode* root);
extern void lastOrder(BitNode* root);
extern void countLeafNodes(BitNode* root,int* sum);
extern int getDepth(BitNode* root);
extern BitNode* copyBitTree(BitNode* root);
#ifdef __cplusplus
}
#endif
#endif
bittree.c 文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bittree.h"
//树的先序遍历。递归算法。
void preOrder(BitNode* root)
{
if (root == NULL)
{
return;
}
//打印根节点的数据
printf("先序遍历:%d \n",root->data);
//遍历左子树
if (root->lchild !=