binarytree.h
#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef int BTDataType;
typedef struct BinaryTreeNode
{
struct BinaryTreeNode* _left;
struct BinaryTreeNode* _right;
BTDataType _data;
}BTNode;
#include"stack.h"
#include"queue.h"
BTNode *BuyNewNode(BTDataType x)
{
BTNode *newnode = (BTNode *)malloc(sizeof(BTNode));
assert(newnode);
newnode->_data = x;
newnode->_left = NULL;
newnode->_right = NULL;
return newnode;
}
BTNode *BinaryTreeCreateByPrev(int *a, int *index, int invalid)
{
assert((*index) >= 0);
if (a[*index] == invalid)
{
return NULL;
}
BTNode *root = BuyNewNode(a[*index]);
++(*index);
root->_left = BinaryTreeCreateByPrev(a, index, invalid);
++(*index);
root->_right = BinaryTreeCreateByPrev(a, index, invalid);
return root;
}
void BTreePrevOrder(BTNode *root)
{
if (root == NULL)
{
return;
}
printf("%2d", root->_data);
BTreePrevOrder(root->_left);
BTreePrevOrder(root->_right);
}
void BTreeInOrder(BTNode* root)
{
if (root == NULL)
{
return;
}
BTreeInOrder(root->_left);
printf("%2d", root->_data);
BTreeInOrder(root->_right);
}
void BTreePostOrder(BTNode* root)
{
if (root == NULL)
{
return;
}
BTreePostOrder(root->_left);
BTreePostOrder(root->_right);
printf("%2d", root->_data);
}
size_t BTreeSize(BTNode* root)
{
if (root == NULL)
{
return 0;
}
return (1 + BTreeSize(root->_left) + BTreeSize(root->_right));
}
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)
{
assert(k);
if (root == NULL)
{
return 0;
}
if (k == 1)
{
return 1;
}
k--;
return BTreeKLevelSize(root->_left, k) + BTreeKLevelSize(root->_right, k);
}
size_t BTreeDepth(BTNode* root)
{
if (root == NULL)
{
return 0;
}
size_t left = BTreeDepth(root->_left);
size_t right = BTreeDepth(root->_right);
return 1 + (left > right ? left : right);
}
void BTreeLevelOrder(BTNode* root)
{
Queue q;
QueueInit(&q);
if (root == NULL)
{
return;
}
QueuePush(&q, root);
while (QueueEmpty(&q))
{
BTNode *front = QueueFront(&q);
if (front->_left != NULL)
{
QueuePush(&q, front->_left);
}
if (front->_right != NULL)
{
QueuePush(&q, front->_right);
}
printf("%2d", front->_data);
QueuePop(&q);
free(front);
}
}
BTNode* BTreeFind(BTNode* root, BTDataType x)
{
if (root == NULL)
{
return NULL;
}
if (root->_data == x)
{
return root;
}
BTNode *ret = NULL;
if (root->_left)
{
ret = BTreeFind(root->_left, x);
if (ret)
{
return ret;
}
}
if (root->_right)
{
ret = BTreeFind(root->_right, x);
if (ret)
return ret;
}
return NULL;
}
int IsCompleteBTree(BTNode* root)
{
Queue q;
QueueInit(&q);
if (root == NULL)
{
return 0;
}
QueuePush(&q, root);
while (QueueFront(&q) != NULL)
{
BTNode *front = QueueFront(&q);
QueuePush(&q, front->_left);
QueuePush(&q, front->_right);
QueuePop(&q);
free(front);
}
if (QueueEmpty(&q) == 0)
{
return 1;
}
else
{
while (QueueEmpty(&q))
{
if (QueueFront(&q) != NULL)
{
return 0;
}
QueuePop(&q);
}
return 1;
}
}
void BTreePrevOrderNonR(BTNode* root)
{
if (root == NULL)
{
return;
}
Stack s;
StackInit(&s);
BTNode *cur = root;
while (cur || StackEmpty(&s))
{
while (cur)
{
printf("%d ", cur->_data);
StackPush(&s, cur);
cur = cur->_left;
}
BTNode *top = StackTop(&s);
cur = top->_right;
StackPop(&s);
}
}
void BTreeInOrderNonR(BTNode* root)
{
Stack s;
StackInit(&s);
BTNode *cur = root;
while (cur || StackEmpty(&s))
{
while (cur)
{
StackPush(&s, cur);
cur = cur->_left;
}
BTNode *top = StackTop(&s);
printf("%d ", top->_data);
cur = top->_right;
StackPop(&s);
}
}
void BTreePostOrderNonR(BTNode* root)
{
Stack s;
StackInit(&s);
BTNode *cur = root;
BTNode *last = NULL;
while (cur || StackEmpty(&s))
{
while (cur)
{
StackPush(&s, cur);
cur = cur->_left;
}
BTNode *top = StackTop(&s);
if (top->_right == NULL || last == top->_right)
{
printf("%d ", top->_data);
StackPop(&s);
}
else
{
cur = top->_right;
last = top->_right;
}
}
}
queue.h
typedef BTNode* QueueDataType;
typedef struct QueueNode
{
QueueDataType _data;
struct QueueNode* _next;
}QueueNode;
typedef struct Queue
{
QueueNode* _head;
QueueNode* _tail;
}Queue;
void QueueInit(Queue* q)
{
assert(q);
q->_head = NULL;
q->_tail = NULL;
}
QueueNode* BuyQueueNode(QueueDataType x)
{
QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
node->_data = x;
node->_next = NULL;
return node;
}
void QueuePush(Queue* q, QueueDataType x)
{
assert(q);
if (q->_tail == NULL)
{
q->_head = q->_tail = BuyQueueNode(x);
}
else
{
q->_tail->_next = BuyQueueNode(x);
q->_tail = q->_tail->_next;
}
}
void QueuePop(Queue* q)
{
if (q->_head == NULL)
{
return;
}
else
{
QueueNode* next = q->_head->_next;
free(q->_head);
q->_head = next;
if (q->_head == NULL)
{
q->_tail = NULL;
}
}
}
QueueDataType QueueFront(Queue* q)
{
assert(q->_head);
return q->_head->_data;
}
QueueDataType QueueBack(Queue* q)
{
return q->_tail->_data;
}
size_t QueueSize(Queue* q)
{
QueueNode* cur = q->_head;
size_t size = 0;
while (cur)
{
++size;
cur = cur->_next;
}
return size;
}
int QueueEmpty(Queue* q)
{
if (q->_head == NULL)
return 0;
else
return 1;
}
stack.h
#pragma once
#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef BTNode* STDataType;
#define Stack_size 10
typedef struct Stack
{
STDataType* _array;
size_t _top;
size_t _end;
}Stack;
void StackInit(Stack* s)
{
assert(s);
s->_array = (STDataType*)malloc(sizeof(STDataType)*Stack_size);
s->_end = Stack_size;
s->_top = 0;
}
void StackPush(Stack* s, STDataType x)
{
assert(s);
if (s->_end == s->_top)
{
s->_end *= 2;
s->_array = (STDataType*)realloc(s->_array, sizeof(STDataType)*(s->_end));
s->_array[s->_top] = x;
(s->_top)++;
}
else
{
s->_array[s->_top] = x;
(s->_top)++;
}
}
void Stack_print(Stack *s)
{
if (s == NULL)
{
return;
}
while ((s->_top)--)
{
printf("%d\t", s->_array[s->_top]);
}
}
void StackPop(Stack* s)
{
assert(s);
if (s->_top == 0)
{
printf("the stack is empty");
}
else
{
s->_top--;
}
}
STDataType StackTop(Stack* s)
{
assert(s);
assert(s->_top);
int num = s->_top;
return s->_array[(--num)];
}
size_t StackSize(Stack* s)
{
assert(s);
return s->_end;
}
int StackEmpty(Stack* s)
{
if (s->_top == 0)
{
return 0;
}
else
{
return 1;
}
}
test.c
void test()
{
int a[] = { 1, 2, 3, '#', '#',4,'#', '#', 5, 6,'#' ,'#' ,'#' };
int index = 0;
BTNode *root = BinaryTreeCreateByPrev(a, &index, '#');
BTreePrevOrder(root);
printf("\n");
BTreeInOrder(root);
printf("\n");
BTreePostOrder(root);
printf("\n");
printf("该树有%d个节点\n", BTreeSize(root));
printf("该树有%d个叶子节点\n", BTreeLeafSize(root));
printf("该树第3层的节点数为%d\n", BTreeKLevelSize(root, 3));
printf("该树的深度为%d\n", BTreeDepth(root));
printf("%d\n", BTreeFind(root, 3)->_data);
printf("%d\n", IsCompleteBTree(root));
/*BTreePrevOrderNonR(root);
printf("\n");
BTreeInOrderNonR(root);
printf("\n");
BTreePostOrderNonR(root);*/
}
int main()
{
test();
system("pause");
return 0;
}