头文件:BinaryTree.h
#pragma once
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>
#include "Queue.h"
#include "Stack.h"
//struct TreeNode
//{
// // data;
// TNDataType data;
// //struct TreeNode* childs[N];
// SeqList childs; //->struct TreeNode*
//};
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode* _left;//左子树
struct BinaryTreeNode* _right;//右子树
}BTNode;
//创建树的节点
BTNode* BuyBTNode(BTDataType x);
//创建二叉树
BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi);//a是一个前序遍历的数组
//销毁二叉树
void BinaryTreeDestory(BTNode* root);
//二叉树的节点个数
int BinaryTreeSize(BTNode* root);
//二叉树的叶节点个数
int BinaryTreeLeafSize(BTNode* root);
//二叉树第k层节点的个数
int BinaryTreeLevelKSize(BTNode* root, int k);
//查找节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
//深度
int BinaryTreeDepth(BTNode* root);
//判断是否为完全二叉树
int BinaryTreeComplete(BTNode* root);
//层序遍历
void BinaryTreeLevelOrder(BTNode* root);
//前序遍历递归
void BinaryTreePrevOrder(BTNode* root);
//中序遍历递归
void BinaryTreeInOrder(BTNode* root);
//后序遍历递归
void BinaryTreePostOrder(BTNode* root);
//前序遍历非递归
void BinaryTreePrevOrderNonR(BTNode* root);
//中序遍历非递归
void BinaryTreeInOrderNonR(BTNode* root);
//后序遍历非递归
void BinaryTreePostOrderNonR(BTNode* root);
源文件:BinaryTree.c
#include "BinaryTree.h"
//创建树的节点
BTNode* BuyBTNode(BTDataType x)
{
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
node->_left = NULL;
node->_right = NULL;
node->_data = x;
return node;
}
//创建二叉树
BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi)
{
if (a[*pi] != '#')
{
BTNode* root = BuyBTNode(a[*pi]);//创建结点
++(*pi);
root->_left = BinaryTreeCreate(a, n, pi);
++(*pi);
root->_right = BinaryTreeCreate(a, n, pi);
return root;//返回根结点位置
}
else
{
return NULL;
}
}
//销毁二叉树
void BinaryTreeDestory(BTNode* root)
{
if (root==NULL)
{
return;
}
BinaryTreeDestory(root->_left);
BinaryTreeDestory(root->_right);
free(root);
root = NULL;
}
//二叉树的节点个数
int BinaryTreeSize(BTNode* root)
{
if (root == NULL)
return 0;
return BinaryTreeSize(root->_left)+ BinaryTreeSize(root->_right) + 1;
}
//二叉树的叶节点个数
int BinaryTreeLeafSize(BTNode* root)
{
if (root == NULL)
return 0;
if (root->_left == NULL&& root->_right == NULL)
return 1;
return BinaryTreeLeafSize(root->_left)+ BinaryTreeLeafSize(root->_right);
}
//二叉树第k层节点的个数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
if (root == NULL)
return 0;
if (k == 1)
return 1;
return BinaryTreeLevelKSize(root->_left, k - 1)+ BinaryTreeLevelKSize(root->_right, k - 1);
}
//查找节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
BTNode* res = NULL;
if (root == NULL)
return NULL;
if (root->_data == x)
return root;
if (root->_left != NULL)
{
res = BinaryTreeFind(root->_left, x);
return res;
}
if (root->_right != NULL)
{
res = BinaryTreeFind(root->_right, x);
if (res->_data == x)
return res;
}
}
//深度
int BinaryTreeDepth(BTNode* root)
{
int left;
int right;
if (root == NULL)
return 0;
if (root->_left == NULL&&root->_right == NULL)
return 1;
else
{
left = BinaryTreeDepth(root->_left) + 1;
right = BinaryTreeDepth(root->_right) + 1;
return left > right ? left : right;
}
}
//层序遍历
void BinaryTreeLevelOrder(BTNode* root)
{
Queue q;
QueueInit(&q);
if (root)
QueuePush(&q, root);
while (QueueEmpty(&q) != 0)
{
BTNode* front = QueueFront(&q);
printf("%c ", front->_data);
QueuePop(&q);
if (front->_left)
{
QueuePush(&q, front->_left);
}
if (front->_right)
{
QueuePush(&q, front->_right);
}
}
QueueDestory(&q);
printf("\n");
}
//判断是否为完全二叉树
int BinaryTreeComplete(BTNode* root)
{
assert(root);
int flag = 1;
Queue q;
QueueInit(&q);
QueuePush(&q, root);
while (QueueEmpty(&q))
{
BTNode* tmp = QueueFront(&q);
QueuePop(&q);
if (tmp->_left)
{
if (flag == 0)
{
return 0;
}
QueuePush(&q, tmp->_left);
}
else
{
flag = 0;
}
if (tmp->_right)
{
if (flag == 0)
{
return 0;
}
QueuePush(&q, tmp->_right);
}
else
{
flag = 0;
}
}
return 1;
}
//前序遍历递归
void BinaryTreePrevOrder(BTNode* root)
{
if (root == NULL)
return;
printf("%c ", root->_data);
BinaryTreePrevOrder(root->_left);
BinaryTreePrevOrder(root->_right);
}
//中序遍历递归
void BinaryTreeInOrder(BTNode* root)
{
if (root == NULL)
return;
BinaryTreeInOrder(root->_left);
printf("%c ", root->_data);
BinaryTreeInOrder(root->_right);
}
//后序遍历递归
void BinaryTreePostOrder(BTNode* root)
{
if (root == NULL)
return;
BinaryTreePostOrder(root->_left);
BinaryTreePostOrder(root->_right);
printf("%c ", root->_data);
}
//前序遍历非递归
void BinaryTreePrevOrderNonR(BTNode* root)
{
BTNode* cur = root;
BTNode* top;
Stack s;
StackInit(&s);
while (cur || StackEmpty(&s)!= 0)
{
//访问左路节点,左路节点进栈
while (cur)
{
printf("%c ", cur->_data);
StackPush(&s, cur);
cur = cur->_left;
}
//栈里面出来节点,表示左子树已经访问过
top = StackTop(&s);
StackPop(&s);
cur = top->_right;
}
printf("\n");
}
//中序遍历非递归
void BinaryTreeInOrderNonR(BTNode* root)
{
BTNode* top;
BTNode* cur = root;
Stack s;
StackInit(&s);
while (cur || StackEmpty(&s)!= 0)
{
while (cur)
{
StackPush(&s, cur);
cur = cur->_left;
}
top = StackTop(&s);
printf("%c ", top->_data);
StackPop(&s);
cur = top->_right;
}
printf("\n");
}
//后序遍历非递归
void BinaryTreePostOrderNonR(BTNode* root)
{
Stack s;
StackInit(&s);
BTNode* cur = root;
BTNode* top;
BTNode* last = NULL;
while (cur || StackEmpty(&s) != 0)
{
while (cur)
{
StackPush(&s, cur);
cur = cur->_left;
}
top = StackTop(&s);
if (top->_right == NULL || top->_right == last)
{
printf("%c ", top->_data);
last = top;
StackPop(&s);
}
else
{
cur = top->_right;
}
}
printf("\n");
}
测试:
#include"BinaryTree.h"
void TestBinaryTree()
{
char array[] = { 'A', 'B', 'D', '#', '#', '#', 'C',
'E', '#', '#', 'F', '#', '#' };
size_t i = 0;
BTNode* tree = BinaryTreeCreate(array, sizeof(array) / sizeof(BTDataType), &i);
printf("前序遍历为: ");
BinaryTreePrevOrder(tree);
printf("\n");
printf("中序遍历为: ");
BinaryTreeInOrder(tree);
printf("\n");
printf("后序遍历为: ");
BinaryTreePostOrder(tree);
printf("\n");
printf("层序遍历为: ");
BinaryTreeLevelOrder(tree);
printf("树的节点个数为: %d\n", BinaryTreeSize(tree));
printf("树的叶节点个数为: %d\n", BinaryTreeLeafSize(tree));
printf("树的第k层节点个数为: %d\n", BinaryTreeLevelKSize(tree, 2));
printf("树的深度为: %d\n", BinaryTreeDepth(tree));
printf("是否为二叉树: %d\n", BinaryTreeComplete(tree));
printf("非递归前序遍历为: ");
BinaryTreePrevOrderNonR(tree);
printf("非递归中序遍历为: ");
BinaryTreeInOrderNonR(tree);
printf("非递归后序遍历为: ");
BinaryTreePostOrderNonR(tree);
BinaryTreeDestory(tree);
}
int main()
{
TestBinaryTree();
system("pause");
return 0;
}