#pragma once
#include "Queue.h"
typedef char BTDataType;
typedef struct BinTreeBTNode{
struct BinTreeBTNode* _pLeft;
struct BinTreeBTNode* _pRight;
BTDataType data;
}BTNode, *PBTNode;
void _CreateBinTree(PBTNode* pRoot, BTDataType* array, int size, int* index, BTDataType invaild);
PBTNode BuyBinTreeBTNode(BTDataType data);
void CreateBinTree(PBTNode* pRoot, BTDataType* array, int size, BTDataType invalid);
PBTNode CopyBinTree(PBTNode pRoot);
void DestroyTree(PBTNode* pRoot);
void PreOrder(PBTNode pRoot);
void InOrder(PBTNode pRoot);
void PostOrder(PBTNode pRoot);
int SizeTree(PBTNode pRoot);
int GetLeafcount(PBTNode pRoot);
int Height(PBTNode pRoot);
int GetLayerBTNodeCount(PBTNode pRoot, int k);
void LayerOrder(PBTNode pRoot);
void MirrorBTree(PBTNode pRoot);
void W_MirrorBTRee(PBTNode pRoot);
int IsCompLeteBTree(PBTNode pRoot);
int IsNodeInBTree(PBTNode pRoot, PBTNode pNode);
PBTNode FindNode(PBTNode pRoot, BTDataType data);
PBTNode LeftChild(PBTNode pRoot);
PBTNode RightChild(PBTNode pRoot);
void TestBinTree();
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include "Queue.h"
#include "BTree.h"
PBTNode BuyBinTreeBTNode(BTDataType data){
PBTNode pNewBTNode = (PBTNode)malloc(sizeof(BTNode));
assert(pNewBTNode);
pNewBTNode->_pLeft = NULL;
pNewBTNode->_pRight = NULL;
pNewBTNode->data = data;
return pNewBTNode;
}
void _CreateBinTree(PBTNode* pRoot, BTDataType* array, int size, int* index, BTDataType invaild){
assert(index);
if (*index < size && invaild != array[*index]){
*pRoot = BuyBinTreeBTNode(array[*index]);
++(*index);
_CreateBinTree(&(*pRoot)->_pLeft, array, size, index, invaild);
++(*index);
_CreateBinTree(&(*pRoot)->_pRight, array, size, index, invaild);
}
}
void CreateBinTree(PBTNode* pRoot, BTDataType* array, int size, BTDataType invalid){
int index = 0;
_CreateBinTree(pRoot, array, strlen(array), &index, '#');
}
PBTNode CopyBinTree(PBTNode pRoot){
PBTNode pNewBTNode = NULL;
if (pRoot){
pNewBTNode = BuyBinTreeBTNode(pRoot->data);
pNewBTNode->_pLeft = CopyBinTree(pRoot->_pLeft);
pNewBTNode->_pRight = CopyBinTree(pRoot->_pRight);
}
return pNewBTNode;
}
void PreOrder(PBTNode pRoot){
if (pRoot){
printf("%c ", pRoot->data);
PreOrder(pRoot->_pLeft);
PreOrder(pRoot->_pRight);
}
}
void InOrder(PBTNode pRoot){
if (pRoot){
InOrder(pRoot->_pLeft);
printf("%c ", pRoot->data);
InOrder(pRoot->_pRight);
}
}
void PostOrder(PBTNode pRoot){
if (pRoot){
PostOrder(pRoot->_pLeft);
PostOrder(pRoot->_pRight);
printf("%c ", pRoot->data);
}
}
void DestroyTree(PBTNode* pRoot){
assert(pRoot);
if (*pRoot){
DestroyTree(&(*pRoot)->_pLeft);
DestroyTree(&(*pRoot)->_pRight);
free(*pRoot);
*pRoot = NULL;
}
}
int SizeTree(PBTNode pRoot){
if (NULL == pRoot){
return 0;
}
return SizeTree(pRoot->_pLeft) + SizeTree(pRoot->_pRight) + 1;
}
int GetLeafcount(PBTNode pRoot){
if (NULL == pRoot)
return 0;
if (NULL == pRoot->_pLeft && NULL == pRoot->_pRight)
return 1;
return GetLeafcount(pRoot->_pLeft) + GetLeafcount(pRoot->_pRight);
}
int Height(PBTNode pRoot){
int leftHeight = 0;
int rightHeight = 0;
if (NULL == pRoot){
return 0;
}
leftHeight = Height(pRoot->_pLeft);
rightHeight = Height(pRoot->_pRight);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
int GetLayerBTNodeCount(PBTNode pRoot, int k){
if (NULL == pRoot || k <= 0){
return 0;
}
if (1 == k){
return 1;
}
return GetLayerBTNodeCount(pRoot->_pLeft, k - 1) + GetLayerBTNodeCount(pRoot->_pRight, k - 1);
}
void LayerOrder(PBTNode pRoot){
if (NULL == pRoot){
return;
}
PNode pCur = NULL;
Queue q;
QueueInit(&q);
QueuePush(&q, pRoot);
while (!QueueEmpty(&q)){
pCur = QueueFront(&q);
printf("%c ", pCur->_data->data);
if (pCur->_data->_pLeft)
QueuePush(&q, pCur->_data->_pLeft);
if (pCur->_data->_pRight)
QueuePush(&q, pCur->_data->_pRight);
QueuePop(&q);
}
QueueDestroy(&q);
printf("\n");
}
void MirrorBTree(PBTNode pRoot){
if (pRoot){
PBTNode p = pRoot->_pLeft;
pRoot->_pLeft = pRoot->_pRight;
pRoot->_pRight = p;
printf("%c ", pRoot->data);
MirrorBTree(pRoot->_pLeft);
MirrorBTree(pRoot->_pRight);
}
}
void W_MirrorBTRee(PBTNode pRoot){
Queue q;
if (NULL == pRoot)
return;
QueueInit(&q);
QueuePush(&q, pRoot);
while (!QueueEmpty(&q)){
PNode pCur = QueueFront(&q);
printf("%c ", pCur->_data->data);
PBTNode p = pCur->_data->_pLeft;
pCur->_data->_pLeft = pCur->_data->_pRight;
pCur->_data->_pRight = p;
if (pCur->_data->_pLeft){
QueuePush(&q, pCur->_data->_pLeft);
}
if (pCur->_data->_pRight){
QueuePush(&q, pCur->_data->_pRight);
}
QueuePop(&q);
}
QueueDestroy(&q);
}
int IsCompLeteBTree(PBTNode pRoot){
Queue q;
int flag = 0;
if (NULL == pRoot){
return 1;
}
QueueInit(&q);
QueuePush(&q, pRoot);
while (!(QueueEmpty(&q))){
PNode pCur = QueueFront(&q);
if (flag){
if (pCur->_data->_pLeft || pCur->_data->_pRight)
return 0;
}
else{
if (pCur->_data->_pLeft && pCur->_data->_pRight){
QueuePush(&q, pCur->_data->_pLeft);
QueuePush(&q, pCur->_data->_pRight);
flag = 0;
}
else if (pCur->_data->_pLeft){
QueuePush(&q, pCur->_data->_pLeft);
flag = 1;
}
else if (pCur->_data->_pRight){
return 0;
}
else{
return 1;
}
}
QueuePop(&q);
}
return 1;
}
PBTNode FindNode(PBTNode pRoot, BTDataType data){
PBTNode pNode = NULL;
if (NULL == pRoot){
return NULL;
}
if (pRoot->data == data){
return pRoot;
}
if (pNode = FindNode(pRoot->_pLeft, data)){
return pNode;
}
return FindNode(pRoot->_pRight, data);
}
int IsNodeInBTree(PBTNode pRoot, PBTNode pNode){
int flag = 0;
if (NULL == pRoot || NULL == pNode){
return 0;
}
if (pRoot == pNode){
return 1;
}
if (flag = IsNodeInBTree(pRoot->_pLeft, pNode)){
return flag;
}
if (flag = IsNodeInBTree(pRoot->_pRight, pNode)){
return flag;
}
return 0;
}
PBTNode LeftChild(PBTNode pRoot){
return(NULL == pRoot) ? NULL : pRoot->_pLeft;
}
PBTNode RightChild(PBTNode pRoot){
return(NULL == pRoot) ? NULL : pRoot->_pRight;
}
void TestBinTree(){
char* pStr = "ABD###CE##F";
PBTNode pRoot = NULL;
PBTNode pNewBTNode = NULL;
CreateBinTree(&pRoot, pStr, strlen(pStr), '#');
pNewBTNode = CopyBinTree(pRoot);
PreOrder(pRoot);
printf("\n");
InOrder(pRoot);
printf("\n");
PostOrder(pRoot);
printf("\n");
printf("TreeSize = %d\n", SizeTree(pRoot));
printf("TreeLeafCount = %d\n", GetLeafcount(pRoot));
printf("TreeHeight = %d\n", Height(pRoot));
printf("TreeLayer = %d\n", GetLayerBTNodeCount(pRoot, 2));
LayerOrder(pRoot);
printf("\n");
MirrorBTree(pRoot);
printf("\n");
W_MirrorBTRee(pRoot);
printf("\n");
printf("IsCompLeteBTree = %d\n", IsCompLeteBTree(pRoot));
printf("IsNodeInBTree = %d\n", IsNodeInBTree(pRoot,pRoot->_pRight));
DestroyTree(&pRoot);
}
int main(){
TestBinTree();
return 0;
}
#pragma once
#include "BTree.h"
extern struct BTNode;
typedef PBTNode DataType;
typedef struct Node
{
struct Node* _pNext;
DataType _data;
}Node, *PNode;
typedef struct Queue
{
PNode _pHead;
PNode _pTail;
}Queue;
void QueueInit(Queue* q);
void QueuePush(Queue* q, DataType data);
void QueuePop(Queue* q);
int QueueEmpty(Queue* q);
int QueueSize(Queue* q);
PNode QueueFront(Queue* q);
PNode QueueBack(Queue* q);
PNode BuyNode(DataType data);
void QueueDestroy(Queue* q);
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>
#include "Queue.h"
void QueueInit(Queue* q)
{
assert(q);
q->_pHead = NULL;
q->_pTail = NULL;
}
void QueuePush(Queue* q, DataType data)
{
assert(q);
if (NULL == q->_pHead)
{
q->_pHead = q->_pTail = BuyNode(data);
}
else
{
q->_pTail->_pNext = BuyNode(data);
q->_pTail = q->_pTail->_pNext;
}
}
PNode BuyNode(DataType data)
{
PNode pNewNode = (PNode)malloc(sizeof(Node));
assert(pNewNode);
pNewNode->_data = data;
pNewNode->_pNext = NULL;
return pNewNode;
}
void QueuePop(Queue* q)
{
assert(q);
if (NULL == q->_pHead)
{
return;
}
else if (q->_pHead == q->_pTail)
{
free(q->_pHead);
q->_pHead = q->_pTail = NULL;
}
else
{
PNode pDel = q->_pHead;
q->_pHead = pDel->_pNext;;
free(pDel);
}
}
int QueueEmpty(Queue* q)
{
assert(q);
if (NULL == q->_pHead)
return 1;
return 0;
}
int QueueSize(Queue* q)
{
assert(q);
int count = 0;
PNode pCur;
pCur = q->_pHead;
while (pCur)
{
count++;
pCur = pCur->_pNext;
}
return count;
}
PNode QueueFront(Queue* q)
{
assert(q && q->_pHead);
return q->_pHead;
}
PNode QueueBack(Queue* q)
{
assert(q && q->_pHead);
return q->_pTail;
}
void QueueDestroy(Queue* q)
{
PNode pCur = q->_pHead;
while (pCur)
{
q->_pHead = pCur->_pNext;
free(pCur);
pCur = q->_pHead;
}
q->_pTail = NULL;
}