二叉树的实现
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include "Queue.h"
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode* _left;
struct BinaryTreeNode* _right;
} BTNode;
void PrevOrder(BTNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
printf("%c ", root->_data);
PrevOrder(root->_left);
PrevOrder(root->_right);
}
void InOrder(BTNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
InOrder(root->_left);
printf("%c ", root->_data);
InOrder(root->_right);
}
void PostOrder(BTNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
PostOrder(root->_left);
PostOrder(root->_right);
printf("%c ", root->_data);
}
BTNode* CreateNode(BTDataType x)
{
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
if (node == NULL)
{
printf("申请内存失败\n");
exit(-1);
}
node->_data = x;
node->_left = NULL;
node->_right = NULL;
return node;
}
//求二叉树有多少个结点
//int TreeSize(BTNode* root)
//{
// if (root == NULL)
// {
// return 0;
// }
// static int size = 0;
// size++;
// TreeSize(root->_left);
// TreeSize(root->_right);
// return size;
//}
//void TreeSize(BTNode* root, int* psize)
//{
// if (root == NULL)
// {
// return 0;
// }
// (*psize)++;
// TreeSize(root->_left,psize);
// TreeSize(root->_right,psize);
//}
int TreeSize(BTNode* root)
{
if (root == NULL)
{
return 0;
}
return 1 + TreeSize(root->_left) + TreeSize(root->_right);
}
int TreeLeafSize(BTNode* root)
{
if (root == NULL)
{
return 0;
}
else if (root->_left == NULL && root->_right == NULL)
return 1;
return TreeLeafSize(root->_left) + TreeLeafSize(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);
}
//二叉树查找值为x的结点
BTNode* BinaryTreeFind(BTNode* root, int x)
{
if (root == NULL)
return NULL;
if (root->_data == x)
return root;
BTNode* node = BinaryTreeFind(root->_left, x);
if (node->_data == x)
return node;
node = BinaryTreeFind(root->_right, x);
if (node->_data == x)
return node;
return NULL;
}
//二叉树的层序遍历
void BinaryTreeLevelOrder(BTNode* root)
{
Queue q;
QueueInit(&q);
if (root == NULL)
return;
QueuePush(&q, root);
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
printf("%c ", front->_data);
if (front->_left)
{
QueuePush(&q, front->_left);
}
if (front->_right)
{
QueuePush(&q, front->_right);
}
}
printf("\n");
}
//判断二叉树是否是完全二叉树,使用队列来实现
//是返回1,不是返回0
int BinaryTreeComplete(BTNode* root)
{
Queue q;
QueueInit(&q);
if (root == NULL)
return 1;
QueuePush(&q, root);
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front == NULL)
{
break;
}
QueuePush(&q, front->_left);
QueuePush(&q, front->_right);
}
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front)
{
QueueDestory(&q);
return 0;
}
}
return 1;
}
void DestoryTree(BTNode* root)
{
if (root == NULL)
return;
//后序销毁
DestoryTree(root->_left);
DestoryTree(root->_right);
free(root);
}
int main()
{
BTNode* A = CreateNode('A');
BTNode* B = CreateNode('B');
BTNode* C = CreateNode('C');
BTNode* D = CreateNode('D');
BTNode* E = CreateNode('E');
A->_left = B;
A->_right = C;
B->_left = D;
B->_right = E;
PrevOrder(A);
printf("\n");
InOrder(A);
printf("\n");
PostOrder(A);
printf("\n");
//printf("TreeSize=%d\n", TreeSize(A));
//int sizea = 0;
//TreeSize(A, &sizea);
//printf("TreeSize=%d\n",sizea);
printf("TreeSize=%d\n", TreeSize(A));
printf("TreeSize=%d\n", TreeLeafSize(A));
printf("BinaryTreeLevelKSize=%d\n", BinaryTreeLevelKSize(A,3));
BinaryTreeLevelOrder(A);
printf("BinaryTreeComplete:%d\n", BinaryTreeComplete(A));
DestoryTree(A);
return 0;
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//声明一下
extern struct BinaryTreeNode;
//链表实现
typedef struct BinaryTreeNode* QDataType;
typedef struct QueueNode
{
struct QueueNode* _next;
QDataType _data;
}QueueNode;
typedef struct Queue
{
QueueNode* _head;
QueueNode* _tail;
}Queue;
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
//返回1是空,返回0是非空
int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);