二叉树所需要的头文件
//binarytree.h
#ifndef __binarytree_h__
#define __binarytree_h__
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Queue.h"
#include"Stack.h"
typedef int BDatatype;
typedef struct BTNode
{
struct BTNode *_pLeft;
struct BTNode *_pRight;
BDatatype data;
}BTNode;
void CreateBinTree(BTNode **pRoot, char *str, int size, int*index, BDatatype invalid);
void PreOrder(BTNode *pRoot);
void InOrder(BTNode *pRoot);
void PostOrder(BTNode *pRoot);
BTNode * CopyBinTree(BTNode *pRoot);
void DestroyBinTree(BTNode ** pRoot);
int GetBTNodeCount(BTNode *pRoot);
int GetLeafNodeCount(BTNode *pRoot);
int GetKLevelNodeCount(BTNode* pRoot, int K);
int Height(BTNode * pNode);
BTNode * LeftChild(BTNode *pNode);
BTNode * RightChild(BTNode*pNode);
int IsBTNodeInBinTree(BTNode* pRoot,BTNode* pNode);
BTNode* GetBTNode(BTNode* pRoot, int data);
BTNode* GetBTNodeParents(BTNode* pRoot, BTNode* pNode);
void MirrorBinTree(BTNode* pRoot);
void LevelOrder(BTNode* pRoot);
int IsCompleteBinTree(BTNode *pRoot);
void MirrorBinTreeNor(BTNode* pRoot);
void PreOrderNor(BTNode *pRoot);
void PreOrderNor2(BTNode *pRoot);
void InOrderNor(BTNode *pRoot);
void PostOrderNor(BTNode *pRoot);
#endif//__binarytree_h__
//Stack.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdlib.h>
#define MAX 100
typedef struct BTNode* DataType;
typedef struct Stack
{
int top;
DataType stack[MAX];
}Stack;
void StackInit(Stack* s);
void StackPush(Stack* s, DataType data);
void StackPop(Stack* s);
DataType GetStackTop(Stack*s);
int GetStackSize(Stack*s);
int StackEmpty(Stack *s);
//Queue.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#define _CRT_SECURE_NO_WARNINGS 1
typedef struct BTNode* QDataType;
typedef struct LinkList
{
struct LinkLis *next;
QDataType data;
}QLinkList;
typedef struct Queue
{
QLinkList *front;
QLinkList *rear;
int sz;
}Queue;
void QueueInit(Queue* Qlist);
void QueuePush(Queue* Qlist, QDataType data);
void QueuePop(Queue* Qlist);
QDataType QueueFront(Queue* Qlist);
QDataType QueueBack(Queue* Qlist);
int QueueSize(Queue* Qlist);
int QueueEmpty(Queue* Qlist);
具体实现
//binarytree.c
#include "binarytree.h"
void PreOrder(BTNode *pRoot)
{
if (pRoot == NULL)
{
return;
}
printf(" %c ", pRoot->data);
PreOrder(pRoot->_pLeft);
PreOrder(pRoot->_pRight);
}
void InOrder(BTNode *pRoot)
{
if (pRoot == NULL)
{
return;
}
InOrder(pRoot->_pLeft);
printf(" %c ", pRoot->data);
InOrder(pRoot->_pRight);
}
void PostOrder(BTNode *pRoot)
{
if (pRoot == NULL)
{
return;
}
PostOrder(pRoot->_pLeft);
PostOrder(pRoot->_pRight);
printf(" %c ", pRoot->data);
}
void CreateBinTree(BTNode **pRoot, char *str , int size, int*index, BDatatype invalid)
{
if ((*(index) >= size ))
{
return;
}
BTNode* cur = (BTNode*)malloc(sizeof(BTNode));
if (str[(*index)] == invalid)
{
free(cur);
cur = NULL;
(*pRoot) = NULL;
return;
}
else
{
cur->data = str[(*index)];
(*pRoot) = cur;
}
(*index)++;
CreateBinTree(&(*pRoot)->_pLeft, str, size, index, invalid);
(*index)++;
CreateBinTree(&(*pRoot)->_pRight, str, size, index, invalid);
}
BTNode * CopyBinTree(BTNode *pRoot)
{
if (pRoot == NULL)
{
return NULL;
}
BTNode * cur = (BTNode *)malloc(sizeof(BTNode));
cur->data = pRoot->data;
cur->_pLeft = CopyBinTree(pRoot->_pLeft);
cur->_pRight = CopyBinTree(pRoot->_pRight);
return cur;
}
void DestroyBinTree(BTNode ** pRoot)
{
if ((*pRoot) == NULL)
{
return;
}
DestroyBinTree(&(*pRoot)->_pLeft);
DestroyBinTree(&(*pRoot)->_pRight);
free((*pRoot));
(*pRoot) = NULL;
}
int GetBTNodeCount(BTNode *pRoot)
{
if (pRoot == NULL)
{
return 0;
}
return GetBTNodeCount(pRoot->_pLeft) + GetBTNodeCount(pRoot->_pRight) + 1;
}
int GetKLevelNodeCount(BTNode* pRoot, int K)
{
if (K == 0)
{
return 0;
}
if (K == 1)
{
return 1;
}
return GetKLevelNodeCount(pRoot->_pLeft, K - 1) + GetKLevelNodeCount(pRoot->_pLeft, K - 1)-1;
}
int Height(BTNode * pNode)
{
if (pNode == NULL)
{
return 0;
}
if (Height(pNode->_pLeft)>Height(pNode->_pRight))
{
return Height(pNode->_pLeft) + 1;
}
else
{
return Height(pNode->_pRight) + 1;
}
}
int GetLeafNodeCount(BTNode *pRoot)
{
if (pRoot == NULL)
{
return 0;
}
if (pRoot->_pLeft == NULL && pRoot->_pRight == NULL)
{
return 1;
}
return GetLeafNodeCount(pRoot->_pLeft) + GetLeafNodeCount(pRoot->_pRight);
}
BTNode * LeftChild(BTNode *pNode)
{
return pNode->_pLeft;
}
BTNode * RightChild(BTNode*pNode)
{
return pNode->_pRight;
}
int IsBTNodeInBinTree(BTNode* pRoot,BTNode* pNode)
{
if (pRoot == NULL || pNode == NULL)
{
return 0;
}
if (pRoot == pNode)
{
return 1;
}
if (IsBTNodeInBinTree(pRoot->_pLeft, pNode))
{
return 1;
}
if (IsBTNodeInBinTree(pRoot->_pRight, pNode))
{
return 1;
}
return 0;
}
BTNode* GetBTNode(BTNode* pRoot, int Data)
{
if (pRoot == NULL)
{
return NULL;
}
if (pRoot->data == Data)
{
return pRoot;
}
if (GetBTNode(pRoot->_pLeft, Data) != NULL)
{
return GetBTNode(pRoot->_pLeft, Data);
}
else
{
return GetBTNode(pRoot->_pRight, Data);
}
}
BTNode* GetBTNodeParents(BTNode* pRoot, BTNode* pNode)
{
if (pRoot == NULL || pNode == NULL)
{
return NULL;
}
if (pRoot->_pLeft == pNode)
{
return pRoot;
}
if (pRoot->_pRight == pNode)
{
return pRoot;
}
if (GetBTNodeParents(pRoot->_pLeft, pNode) != NULL)
{
return GetBTNodeParents(pRoot->_pLeft, pNode);
}
else
{
return GetBTNodeParents(pRoot->_pRight, pNode);
}
}
void Swap(BTNode** _pLeft, BTNode** _pRight)
{
BTNode* temp = *_pLeft;
*_pLeft = *_pRight;
*_pRight = temp;
}
void MirrorBinTree(BTNode* pRoot)
{
if (pRoot == NULL || pRoot->_pLeft == NULL || pRoot->_pRight == NULL)
{
return ;
}
Swap(&(pRoot->_pLeft), &(pRoot->_pRight));
MirrorBinTree(pRoot->_pLeft);
MirrorBinTree(pRoot->_pRight);
}
void LevelOrder(BTNode* pRoot)
{
Queue q;
if (pRoot == NULL)
{
return ;
}
QueueInit(&q);
QueuePush(&q, pRoot);
while (QueueEmpty(&q))
{
BTNode *cur = QueueFront(&q);
printf(" %c ", cur->data);
if (cur->_pLeft != NULL)
{
QueuePush(&q, cur->_pLeft);
}
if (cur->_pRight != NULL)
{
QueuePush(&q, cur->_pRight);
}
QueuePop(&q);
}
}
int IsCompleteBinTree(BTNode *pRoot)
{
Queue q;
QueueInit(&q);
int IsFlag = 0;
QueuePush(&q, pRoot);
while (QueueEmpty(&q))
{
BTNode *cur = QueueFront(&q);
if (IsFlag)
{
if ((cur->_pLeft != NULL) && (cur->_pRight != NULL))
{
return 0;
}
}
else
{
if (cur->_pLeft && cur->_pRight)
{
QueuePush(&q, cur->_pLeft);
QueuePush(&q, cur->_pRight);
}
else if ((cur->_pLeft)&&((cur->_pRight)==NULL))
{
QueuePush(&q, cur->_pLeft);
IsFlag = 1;
}
else if ((cur->_pRight)&&((cur->_pLeft)==NULL))
{
return 0;
}
else
{
IsFlag = 1;
}
}
QueuePop(&q);
}
return 1;
}
void MirrorBinTreeNor(BTNode* pRoot)
{
if (pRoot == NULL)
{
return;
}
Queue q;
QueueInit(&q);
QueuePush(&q, pRoot);
while (QueueEmpty(&q))
{
BTNode *cur = QueueFront(&q);
if (cur !=NULL && cur->_pLeft != NULL)
{
QueuePush(&q, cur->_pLeft);
}
if (cur != NULL && cur->_pRight != NULL)
{
QueuePush(&q, cur->_pRight);
}
Swap(&(cur->_pLeft), &(cur->_pRight));
QueuePop(&q);
}
}
void PreOrderNor(BTNode *pRoot)
{
Stack s;
StackInit(&s);
StackPush(&s, pRoot);
while (StackEmpty(&s))
{
BTNode *cur = GetStackTop(&s);
StackPop(&s);
while (cur)
{
printf(" %c ", cur->data);
StackPush(&s, cur->_pRight);
cur = cur->_pLeft;
}
}
}
void PreOrderNor2(BTNode *pRoot)
{
Stack s;
StackInit(&s);
StackPush(&s, pRoot);
while (StackEmpty(&s))
{
BTNode* cur = GetStackTop(&s);
StackPop(&s);
printf(" %c ", cur->data);
if (cur->_pRight != NULL)
{
StackPush(&s, cur->_pRight);
}
if (cur->_pLeft != NULL)
{
StackPush(&s, cur->_pLeft);
}
}
}
void InOrderNor(BTNode *pRoot)
{
Stack s;
StackInit(&s);
BTNode *cur = pRoot;
while (StackEmpty(&s) || cur != NULL)
{
while (cur)
{
StackPush(&s, cur);
cur = cur->_pLeft;
}
cur=GetStackTop(&s);
StackPop(&s);
printf(" %c ", cur->data);
cur = cur->_pRight;
}
}
void PostOrderNor(BTNode *pRoot)
{
Stack s;
StackInit(&s);
BTNode *cur = pRoot;
BTNode *Prev = NULL;
BTNode *pTop = NULL;
while (StackEmpty(&s) || cur != NULL)
{
while (cur)
{
StackPush(&s, cur);
cur = cur->_pLeft;
}
pTop = GetStackTop(&s);
if (pTop->_pRight == Prev || pTop->_pRight == NULL)
{
printf(" %c ", pTop->data);
StackPop(&s);
Prev = pTop;
}
else
{
cur = pTop->_pRight;
}
}
}
//Stack.c
#include"Stack.h"
void StackInit(Stack* s)
{
s->top = 0;
}
void StackPush(Stack* s, DataType data)
{
if (s->top == MAX)
{
printf("栈已满!无法入栈元素\n");
return;
}
(s->top)++;
s->stack[s->top] = data;
}
void StackPop(Stack *s)
{
if (s->top == 0)
{
printf("栈为空,无法出栈元素\n");
return;
}
s->stack[s->top] = 0;
(s->top) --;
}
DataType GetStackTop(Stack*s)
{
if (s->top < 0)
{
printf("栈为空,无法获取栈顶\n");
return 0;
}
return s->stack[(s->top)];
}
int GetStackSize(Stack*s)
{
return s->top;
}
int StackEmpty(Stack *s)
{
if (s->top == 0)
{
return 0;
}
else
{
return 1;
}
}
//Queue.c
#include "Queue.h"
QLinkList* BuyNode(QDataType Data)
{
QLinkList *cur = (QLinkList*)malloc(sizeof(QLinkList));
if (cur == NULL)
{
printf("QueueInit :: malloc failed\n");
exit(0);
}
cur->data = Data;
cur->next = NULL;
return cur;
}
void QueueInit(Queue* Qlist)
{
Qlist->front = NULL;
Qlist->rear = NULL;
Qlist->sz = 0;
}
void QueuePush(Queue*Qlist, QDataType data)
{
assert(Qlist);
QLinkList *cur = BuyNode(data);
QLinkList *pcur = Qlist->front;
if (pcur != NULL)
{
while (pcur->next != NULL)
{
pcur = pcur->next;
}
pcur->next = cur;
Qlist->rear = pcur->next;
}
else
{
Qlist->front = cur;
Qlist->rear = cur;
}
Qlist->sz++;
}
void QueuePop(Queue* Qlist)
{
assert(Qlist);
if (Qlist->sz == 0)
{
printf("队列为空,无法出队!\n");
system("pause");
return;
}
QLinkList *cur = Qlist->front;
Qlist->front = Qlist->front->next;
free(cur);
Qlist->sz--;
}
QDataType QueueFront(Queue* Qlist)
{
assert(Qlist);
if (Qlist->sz == 0)
{
printf("队列为空,无法获取队首元素 \n");
return 0;
}
return Qlist->front->data;
}
QDataType QueueBack(Queue* Qlist)
{
assert(Qlist);
if (Qlist->sz == 0)
{
printf("队列为空,无法获取队尾元素 \n");
}
return Qlist->rear->data;
}
int QueueSize(Queue* Qlist)
{
assert(Qlist);
return Qlist->sz;
}
int QueueEmpty(Queue* Qlist)
{
assert(Qlist);
if (Qlist->sz == 0)
{
return 0;
}
else
{
return 1;
}
}
在主函数中测试代码
#include "binarytree.h"
void TestCreateBinTree()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
InOrder(pRoot);
printf("\n");
PostOrder(pRoot);
printf("\n");
}
void TestCopyBinTree()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
BTNode* cur = CopyBinTree(pRoot);
PreOrder(cur);
printf("\n");
}
void TestDestroyBinTree()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
DestroyBinTree(&pRoot);
PreOrder(pRoot);
printf("\n");
}
void TestGetBTNodeCount()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
printf("Count = %d", GetBTNodeCount(pRoot));
printf("\n");
}
void TestGetKLevelNodeCount()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
printf("Count = %d", GetKLevelNodeCount(pRoot,3));
printf("\n");
}
void TestHeight()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
printf("Count = %d", Height(pRoot));
printf("\n");
}
void TestGetLeafNodeCount()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
printf("Count = %d", GetLeafNodeCount(pRoot));
printf("\n");
}
void TestIsBTNodeInBinTree()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
BTNode * cur = GetBTNode(pRoot, 'F');
if (cur != NULL)
{
printf(" cur->data = %c \n", cur->data);
}
printf("Is = %d", IsBTNodeInBinTree(pRoot,GetBTNode(pRoot,'F')));
printf("\n");
}
void TestGetBTNodeParents()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
BTNode * cur = GetBTNodeParents(pRoot, GetBTNode(pRoot, 'D'));
if (cur != NULL)
{
printf(" cur->data = %c \n", cur->data);
}
printf("\n");
}
void TestMirrorBinTree()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
MirrorBinTree(pRoot);
PreOrder(pRoot);
}
void TestLevelOrder()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
LevelOrder(pRoot);
printf("\n");
}
void TestIsCompelete()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','E','#','#','C','F','#','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
printf("IscompleteBinTree = %d ", IsCompleteBinTree(pRoot));
printf("\n");
}
void TestMirrorBinTreeNor()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
MirrorBinTreeNor(pRoot);
PreOrder(pRoot);
}
void TestPreOrderNor()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PreOrder(pRoot);
printf("\n");
PreOrderNor(pRoot);
printf("\n");
PreOrderNor2(pRoot);
printf("\n");
}
void TestInOrderNor()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
InOrder(pRoot);
printf("\n");
InOrderNor(pRoot);
}
void TestPostOrderNor()
{
BTNode * pRoot;
char str[] = { 'A','B','D','#','#','#','C','E','#','#','F','#','#' };
int index = 0;
CreateBinTree(&pRoot, str, sizeof(str), &index, '#');
PostOrder(pRoot);
printf("\n");
PostOrderNor(pRoot);
}
int main()
{
//TestCreateBinTree();
//TestCopyBinTree();
//TestDestroyBinTree();
//TestGetBTNodeCount();
//TestGetKLevelNodeCount();
//TestHeight();
//TestGetLeafNodeCount();
//TestIsBTNodeInBinTree();
//TestGetBTNodeParents();
//TestMirrorBinTree();
//TestLevelOrder();
//TestIsCompelete();
//TestMirrorBinTreeNor();
//TestPreOrderNor();
//TestInOrderNor();
//TestPostOrderNor();
system("pause");
return 0;
}