typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode * _left;
struct BinaryTreeNode * _right;
} BTNode;
BTNode* BinaryTreeCreate ( BTDataType* a, int n, int * pi) ;
void BinaryTreeDestory ( BTNode* * root) ;
int BinaryTreeSize ( BTNode* root) ;
int BinaryTreeLeafSize ( BTNode* root) ;
int BinaryTreeLevelKSize ( BTNode* root, int k) ;
BTNode* BinaryTreeFind ( BTNode* root, BTDataType x) ;
void BinaryTreePrevOrder ( BTNode* root) ;
void BinaryTreeInOrder ( BTNode* root) ;
void BinaryTreePostOrder ( BTNode* root) ;
void BinaryTreeLevelOrder ( BTNode* root) ;
int BinaryTreeComplete ( BTNode* root) ;
# include <stdio.h>
# include <malloc.h>
# include <assert.h>
# include "BinaryTree.h"
# include "Queue.h"
BTNode* BinaryTreeCreate ( BTDataType* a, int n, int * pi)
{
if ( a[ * pi] == '#' || * pi > n)
{
( * pi) ++ ;
return NULL ;
}
BTNode* cur = ( BTNode* ) malloc ( sizeof ( BTNode) ) ;
cur-> _data = a[ * pi] ;
( * pi) ++ ;
cur-> _left = BinaryTreeCreate ( a, n, pi) ;
cur-> _right = BinaryTreeCreate ( a, n, pi) ;
return cur;
}
void BinaryTreeDestory ( BTNode* * root)
{
assert ( NULL != root) ;
if ( NULL == * root)
{
return ;
}
BinaryTreeDestory ( & ( ( * root) -> _left) ) ;
BinaryTreeDestory ( & ( ( * root) -> _right) ) ;
free ( * root) ;
* root = NULL ;
}
int BinaryTreeSize ( BTNode* root)
{
if ( NULL == root)
{
return 0 ;
}
return 1 + BinaryTreeSize ( root-> _left) + BinaryTreeSize ( root-> _right) ;
}
int BinaryTreeLeafSize ( BTNode* root)
{
if ( NULL == root)
{
return 0 ;
}
if ( NULL == root-> _left && NULL == root-> _right)
{
return 1 ;
}
return BinaryTreeLeafSize ( root-> _left) + BinaryTreeLeafSize ( root-> _right) ;
}
int BinaryTreeLevelKSize ( BTNode* root, int k)
{
if ( k == 0 || NULL == root)
{
return 0 ;
}
if ( k == 1 )
{
return 1 ;
}
return BinaryTreeLevelKSize ( root-> _left, k - 1 ) + BinaryTreeLevelKSize ( root-> _right, k - 1 ) ;
}
BTNode* BinaryTreeFind ( BTNode* root, BTDataType x)
{
if ( NULL == root)
{
return NULL ;
}
if ( root-> _data == x)
{
return root;
}
BTNode* ret = NULL ;
if ( ret = BinaryTreeFind ( root-> _left, x) )
{
return ret;
}
return BinaryTreeFind ( root-> _right, x) ;
}
void BinaryTreePrevOrder ( BTNode* root)
{
if ( NULL == root)
{
return ;
}
printf ( "%c" , root-> _data) ;
BinaryTreePrevOrder ( root-> _left) ;
BinaryTreePrevOrder ( root-> _right) ;
}
void BinaryTreeInOrder ( BTNode* root)
{
if ( NULL == root)
{
return ;
}
BinaryTreeInOrder ( root-> _left) ;
printf ( "%c" , root-> _data) ;
BinaryTreeInOrder ( root-> _right) ;
}
void BinaryTreePostOrder ( BTNode* root)
{
if ( NULL == root)
{
return ;
}
BinaryTreePostOrder ( root-> _left) ;
BinaryTreePostOrder ( root-> _right) ;
printf ( "%c" , root-> _data) ;
}
void BinaryTreeLevelOrder ( BTNode* root)
{
Queue q;
if ( NULL == root)
{
return ;
}
QueueInit ( & q) ;
QueuePush ( & q, root) ;
while ( ! QueueEmpty ( & q) )
{
if ( root-> _left != NULL )
{
QueuePush ( & q, root-> _left) ;
}
if ( root-> _right != NULL )
{
QueuePush ( & q, root-> _right) ;
}
printf ( "%c" , root-> _data) ;
QueuePop ( & q) ;
if ( ! QueueEmpty ( & q) )
{
root = QueueFront ( & q) ;
}
}
QueueDestroy ( & q) ;
}
int BinaryTreeComplete ( BTNode* root)
{
if ( NULL == root)
{
return 1 ;
}
int count = 1 ;
int size = BinaryTreeSize ( root) ;
Queue q;
QueueInit ( & q) ;
QueuePush ( & q, root) ;
while ( ! QueueEmpty ( & q) )
{
if ( root-> _left != NULL )
{
QueueDestroy ( & q) ;
if ( size == 2 * count - 1 )
return 1 ;
else
return 0 ;
}
if ( root-> _right != NULL )
{
QueueDestroy ( & q) ;
if ( size == 2 * count)
return 1 ;
else
return 0 ;
}
count++ ;
QueuePush ( & q, root-> _left) ;
QueuePush ( & q, root-> _right) ;
QueuePop ( & q) ;
root = QueueFront ( & q) ;
}
}