在这里插入代码片
```BTNode* BinaryTreeCreate(BTDataType* a, int* pi)
{
if (a[*pi] != '#')
{
BTNode* root = (BTNode*)malloc(sizeof(BTNode));
root->_data = a[*pi];
++(*pi);
root->_left = BinaryTreeCreate(a, pi);
++(*pi);
root->_right = BinaryTreeCreate(a, pi);
return root;
}
else
return NULL;
}
void BinaryTreeDestory(BTNode** root)
{
BTNode* cur = *root;
if (cur)
{
BinaryTreeDestory(&cur->_left);
BinaryTreeDestory(&cur->_right);
free(cur);
*root = NULL;
}
}
void BianryTreeSize2(BTNode* root, int* num)
{
if (root)
{
++(*num);
BianryTreeSize2(root->_left, num);
BianryTreeSize2(root->_right, num);
}
}
int BinaryTreeLeafSize(BTNode* root)
{
if (root == NULL)
return 0;
if (root->_left == NULL && root->_right == NULL)
return 1;
return BinaryTreeLeafSize(root->_left)
+ BinaryTreeLeafSize(root->_right);
}
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);
}
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
BTNode* ret;
if (root == NULL)
return NULL;
if (root->_data == x)
return root;
ret = BinaryTreeFind(root->_left, x);
if (ret)
return ret;
return BinaryTreeFind(root->_right, x);
}
void BinaryTreePrevOrder(BTNode* root)
{
if (root)
{
printf("%c ", root->_data);
BinaryTreePrevOrder(root->_left);
BinaryTreePrevOrder(root->_right);
}
}
void BinaryTreePrevOrderNoR(BTNode* root)
{
Stack st;
stackInit(&st, 10);
BTNode* cur = root;
BTNode* top;
while (cur || stackEmpty(&st) != 1)
{
while (cur)
{
printf("%c ", cur->_data);
stackPush(&st, cur);
cur = cur->_left;
}
top = stackTop(&st);
stackPop(&st);
cur = top->_right;
}
printf("\n");
}
void BinaryTreeInOrder(BTNode* root)
{
if (root)
{
BinaryTreeInOrder(root->_left);
printf("%c ", root->_data);
BinaryTreeInOrder(root->_right);
}
}
void BinaryTreeInOrderNoR(BTNode* root)
{
Stack st;
stackInit(&st, 10);
BTNode* cur = root;
BTNode* top;
while (cur || stackEmpty(&st) != 1)
{
while (cur)
{
stackPush(&st, cur);
cur = cur->_left;
}
top = stackTop(&st);
stackPop(&st);
printf("%c ", top->_data);
cur = top->_right;
}
printf("\n");
}
void BinaryTreePostOrder(BTNode* root)
{
if (root)
{
BinaryTreePostOrder(root->_left);
BinaryTreePostOrder(root->_right);
printf("%c ", root->_data);
}
}
void BinaryTreePostOrderNoR(BTNode* root)
{
Stack st;
stackInit(&st, 10);
BTNode* cur = root;
BTNode* top;
BTNode* prev = NULL;
while (cur || stackEmpty(&st) != 1)
{
while (cur)
{
stackPush(&st, cur);
cur = cur->_left;
}
top = stackTop(&st);
if (top->_right == NULL || top->_right == prev)
{
printf("%c ", top->_data);
stackPop(&st);
prev = top;
}
else
cur = top->_right;
}
printf("\n");
}
void BinaryTreeLevelOrder(BTNode* root)
{
Queue q;
queueInit(&q);
if (root)
queuePush(&q, root);
while (queueEmpty(&q) != 1)
{
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");
}
int BinaryTreeComplete(BTNode* root)
{
Queue q;
queueInit(&q);
if (root)
queuePush(&q, root);
while (queueEmpty(&q) != 1)
{
BTNode* front = queueFront(&q);
queuePop(&q);
if (front)
{
queuePush(&q, front->_left);
queuePush(&q, front->_right);
}
else
break;
}
while (queueEmpty(&q) != 1)
{
BTNode* front = queueFront(&q);
queuePop(&q);
if (front)
{
return 0;
}
}
return 1;
}