二叉树实现
二叉树结构定义
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include"Queue.h"
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType data;
struct BinaryTreeNode* leftChild;
struct BinaryTreeNode* rightChild;
}BTNode;
创建结点
BTNode* BuyNode(BTDataType x)
{
BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
if (newnode == NULL)
exit(-1);
newnode->data = x;
newnode->leftChild = newnode->rightChild = NULL;
return newnode;
}
模拟创建二叉树,用于测试
BTNode* CreatBinaryTree()
{
BTNode* nodeA = BuyNode('A');
BTNode* nodeB = BuyNode('B');
BTNode* nodeC = BuyNode('C');
BTNode* nodeD = BuyNode('D');
BTNode* nodeE = BuyNode('E');
BTNode* nodeF = BuyNode('F');
BTNode* nodeG = BuyNode('G');
nodeA->leftChild = nodeB;
nodeA->rightChild = nodeC;
nodeB->leftChild = nodeD;
nodeB->rightChild = nodeE;
nodeC->rightChild = nodeF;
nodeC->leftChild = nodeG;
return nodeA;
}
二叉树前序遍历
// 二叉树前序遍历
void PreOrder(BTNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
printf("%c ", root->data);
PreOrder(root->leftChild);
PreOrder(root->rightChild);
}
二叉树中序遍历
// 二叉树中序遍历
void InOrder(BTNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
InOrder(root->leftChild);
printf("%c ", root->data);
InOrder(root->rightChild);
}
二叉树后序遍历
// 二叉树后序遍历
void PostOrder(BTNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
PostOrder(root->leftChild);
PostOrder(root->rightChild);
printf("%c ", root->data);
}
二叉树节点个数
// 二叉树节点个数
int BinaryTreeSize(BTNode* root)
{
// return root == NULL ? 0 : BinaryTreeSize(root->left)+ BinaryTreeSize(root->right) + 1;
if (root == NULL)
return 0;
return BinaryTreeSize(root->leftChild) + BinaryTreeSize(root->rightChild) + 1;
}
二叉树叶子节点个数
// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root)
{
if (root == NULL)
return 0;
if (root->leftChild == NULL && root->rightChild == NULL)
return 1;
return BinaryTreeLeafSize(root->leftChild) + BinaryTreeLeafSize(root->rightChild);
}
二叉树深度/高度
// 二叉树深度/高度
int BinaryTreeDepth(BTNode* root)
{
if (root == NULL)
return 0;
int leftDepth = BinaryTreeDepth(root->leftChild)+ 1;
int rightDepth = BinaryTreeDepth(root->rightChild)+ 1;
if (leftDepth > rightDepth)
return leftDepth;
else
return rightDepth;
//int leftDepth = BinaryTreeDepth(root->left);
//int rightDepth = BinaryTreeDepth(root->right);
//return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
二叉树第k层节点个数
// 二叉树第k层节点个数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
if (root == NULL||k<1)
return 0;
if (k == 1)
return 1;
return BinaryTreeLevelKSize(root->leftChild, k - 1) + BinaryTreeLevelKSize(root->rightChild, k - 1);
}
二叉树查找值为x的节点
// 二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
if (root == NULL)
return NULL;
if (root->data == x)
return root;
BTNode* left = BinaryTreeFind(root->leftChild, x);
if (left)
return left;
BTNode* right = BinaryTreeFind(root->rightChild, x);
return right;
}
二叉树层次遍历
//二叉树层次遍历
void BinaryTreeLevelOrder(BTNode* root)
{
if (root == NULL)
return;
Queue q;
QueueInit(&q);
//先入根结点
QueuePush(&q, root);
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
printf("%c ", front->data);
QueuePop(&q);
if (front->leftChild != NULL)
{
//入队列
QueuePush(&q, front->leftChild);
}
if (front->rightChild != NULL)
{
//入队列
QueuePush(&q, front->rightChild);
}
}
printf("\n");
QueueDestroy(&q);
}
二叉树销毁
// 二叉树销毁
void BinaryTreeDestroy(BTNode* root)
{
if (root == NULL)
{
return;
}
BinaryTreeDestroy(root->leftChild);
BinaryTreeDestroy(root->rightChild);
free(root);
}
判断二叉树是否是完全二叉树
// 判断二叉树是否是完全二叉树
bool BinaryTreeComplete(BTNode* root)
{
if (root == NULL)
return true;
Queue q;
QueueInit(&q);
//先入根结点
QueuePush(&q, root);
BTNode* front = NULL;
//遇到空就退出
while (front = QueueFront(&q))
{
QueuePop(&q);
//不管是否为空都入队列
QueuePush(&q, front->leftChild);
QueuePush(&q, front->rightChild);
}
//检查后面的是否都为空,存在一个不为空的,则不是完全二叉树
while (!QueueEmpty(&q))
{
front = QueueFront(&q);
if (front != NULL)
{
QueueDestroy(&q);
return false;
}
QueuePop(&q);//出队列
}
//没有返回,那么全为空
QueueDestroy(&q);
return true;
}
二叉树测试
void testTree()
{
BTNode* root = CreatBinaryTree();
PreOrder(root);
printf("\n");
InOrder(root);
printf("\n");
PostOrder(root);
printf("\n");
BinaryTreeLevelOrder(root);
printf("\n");
printf("BinaryTreeSize:%d\n", BinaryTreeSize(root));
printf("BinaryTreeLeafSize:%d\n", BinaryTreeLeafSize(root));
printf("BinaryTreeLevelKSize:%d\n", BinaryTreeLevelKSize(root,2));
BTNode* find = BinaryTreeFind(root, 'C');
printf("BinaryTreeFind:%c\n",find->data );
printf("BinaryTreeDepth:%d\n", BinaryTreeDepth(root));
BinaryTreeDestroy(root);
}
void testTree2()
{
BTNode* root = CreatBinaryTree();
printf("BinaryTreeComplete:%d ", BinaryTreeComplete(root));
BinaryTreeDestroy(root);
}
int main()
{
testTree();
testTree2();
return 0;
}