# 二叉树基础操作

## 二叉树的实现（C）

#include<windows.h>
#include<stdio.h>
#include"stack.h"
#include"queue.h"

typedef int  BTDataType;
typedef struct BTreeNode
{
struct BTreeNode* left;
struct BTreeNode* right;
BTDataType _data;
}BTNode;

BTNode* BuyBTNode(BTDataType x)
{
BTNode* _new;
_new = (BTNode*)malloc(sizeof(BTNode));
_new->left = NULL;
_new->right = NULL;
_new->_data = x;
return _new;
}

BTNode* CreateBTree(BTDataType* a, size_t* pIndex, BTDataType invalid)//给一个pindex用于迭代
{
//	int a[] = { 1, 2, 3, '#', '#',4,'#', '#', 5, 6,'#' ,'#' ,'#' };
BTNode* tree;
BTDataType* b;//b为了将a存起来下一次好用
b = a;
a = a + *pIndex;
if (*a == invalid)
{
(*pIndex)++;
return NULL;
}
(*pIndex)++;
a = b;
tree->left = CreateBTree(a, pIndex, invalid);
tree->right = CreateBTree(a, pIndex, invalid);
return tree;
}

void BTreePrevOrder(BTNode* root)//前序
{
if (root == NULL)
{
return;
}
printf("%d ",root->_data);
BTreePrevOrder(root->left);
BTreePrevOrder(root->right);
}
void BTreeInOrder(BTNode* root)//中序
{
if (root == NULL)
{
return;
}
BTreeInOrder(root->left);
printf("%d ", root->_data);
BTreeInOrder(root->right);
}
void BTreePostOrder(BTNode* root)//后续
{
if (root == NULL)
{
return;
}
BTreePostOrder(root->left);
BTreePostOrder(root->right);
printf("%d ", root->_data);
}

size_t BTreeSize(BTNode* root)
{
if (root == NULL)
{
return 0;
}
return  BTreeSize(root->left) + BTreeSize(root->right)+1;
}

size_t BTreeLeafSize(BTNode* root)
{
if (root == NULL)
{
return 0;
}
if ((root->left) == NULL && (root->right) == NULL)
{
return 1 ;
}
return  BTreeLeafSize(root->left) + BTreeLeafSize(root->right);
}

size_t BTreeKLevelSize(BTNode* root, size_t k)
{
if (root == NULL)
{
return 0;
}
if (k == 1)
{
return 1;
}
k--;
return BTreeKLevelSize(root->left,k) + BTreeKLevelSize(root->left,k);
}

BTNode* BTreeFind(BTNode* root, BTDataType x)
{
if (root == NULL)
{
return NULL;
}
if (root->_data == x)
{
return root;
}
BTreeFind(root->left, x);
BTreeFind(root->right,x);
return NULL;
}

int IsCompleteBTree(BTNode* root)
{
Queue* q = (Queue*)malloc(sizeof(Queue));
QueueInit(q);
QueuePush(q, root);
{
QueuePop(q);
}
while (QueueEmpty(q) != 0)
{
if (q->_head->_next->_data == NULL)
{
QueuePop(q);
}
else
{
return 0;
}
}
return 1;
}

int IsCompleteBTree1(BTNode* root)
{
int flag = 0;
Queue* q = (Queue*)malloc(sizeof(Queue));
QueueInit(q);
QueuePush(q, root);
while (QueueEmpty(q)!=0)
{
QueuePop(q);
if (q->_head->_next->_data == NULL)
{
flag = 1;
}
if (flag = 1 && q->_head->_next->_data != NULL)
{
return 0;
}
}
return 1;
}

void BTreePrevOrderNonR(BTNode* root)
{
Stack s;
StackInit(&s);
BTNode* ptr = root;
while (ptr || StackEmpty(&s) != 0)
{
while (ptr)
{
printf("%d ", ptr->_data);
StackPush(&s, ptr);
ptr = ptr->left;
}
ptr = s._array[s._top-1]->right;
StackPop(&s);
}
}

void BTreeInOrderNonR(BTNode* root)
{
Stack s;
StackInit(&s);
BTNode* ptr = root;
while (ptr || StackEmpty(&s) != 0)
{
while (ptr)
{
StackPush(&s,ptr);
ptr = ptr->left;
}
printf("%d ",s._array[s._top-1]->_data);
ptr = s._array[s._top - 1]->right;
StackPop(&s);
}
}

void BTreeInOrderNonR(BTNode* root)
{
Stack s;
StackInit(&s);
BTNode* ptr = root;
while (ptr || StackEmpty(&s) != 0)
{
while (ptr)
{
StackPush(&s,ptr);
ptr = ptr->left;
}
printf("%d ",s._array[s._top-1]->_data);
ptr = s._array[s._top - 1]->right;
StackPop(&s);
}
}