通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
需要注意的事:层序遍历需要一个队列,所以引用一个队列构建,层序遍历的实现是(根存入队列,引出两个子树存入队列,然后依次出队),在引用两个头文件时候要注意在BTree.h上加上#pragma once
防止类重定义
头文件 BTree.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
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) ;
void testTree ( ) ;
BTree.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "BTree.h"
#include "Queue.h"
BTNode* BinaryTreeCreate ( BTDataType* a, int n, int * pi)
{
if ( a[ * pi] != '#' )
{
BTNode* root = ( BTNode* ) malloc ( sizeof ( BTNode) ) ;
root-> _data = a[ * pi] ;
( * pi) ++ ;
root-> _left = BinaryTreeCreate ( a, n, pi) ;
( * pi) ++ ;
root-> _right = BinaryTreeCreate ( a, n, pi) ;
return root;
}
else
return NULL ;
}
void BinaryTreeDestory ( BTNode* root)
{
if ( root == NULL )
return ;
else
{
BinaryTreeDestory ( root-> _left) ;
BinaryTreeDestory ( root-> _right) ;
free ( root) ;
}
root = NULL ;
}
int BinaryTreeSize ( BTNode* root)
{
if ( root == NULL )
return 0 ;
else
return BinaryTreeSize ( root-> _left) + BinaryTreeSize ( root-> _right) + 1 ;
}
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)
{
if ( root == NULL )
return NULL ;
if ( root-> _data == x)
return root;
BTNode* ret = BinaryTreeFind ( root-> _left, x) ;
if ( ret)
return ret;
ret = BinaryTreeFind ( root-> _right, x) ;
if ( ret)
return ret;
return NULL ;
}
void BinaryTreePrevOrder ( BTNode* root)
{
if ( root == NULL )
return ;
printf ( "%c " , root-> _data) ;
BinaryTreePrevOrder ( root-> _left) ;
BinaryTreePrevOrder ( root-> _right) ;
}
void BinaryTreeInOrder ( BTNode* root)
{
if ( root == NULL )
return ;
BinaryTreeInOrder ( root-> _left) ;
printf ( "%c " , root-> _data) ;
BinaryTreeInOrder ( root-> _right) ;
}
void BinaryTreePostOrder ( BTNode* root)
{
if ( root == NULL )
return ;
BinaryTreeInOrder ( root-> _left) ;
BinaryTreeInOrder ( root-> _right) ;
printf ( "%c " , root-> _data) ;
}
void BinaryTreeLevelOrder ( BTNode* root)
{
Queue q;
QueueInit ( & q) ;
if ( root != NULL )
QueuePush ( & q, root) ;
while ( QueueEmpty ( & q) != 0 )
{
BTNode* front = QueueFront ( & q) ;
QueuePop ( & q) ;
printf ( "%c " , front-> _data) ;
if ( front-> _left != NULL )
QueuePush ( & q, front-> _left) ;
if ( front-> _right != NULL )
QueuePush ( & q, front-> _right) ;
}
QueueDestory ( & q) ;
}
int BinaryTreeComplete ( BTNode* root)
{
Queue q;
QueueInit ( & q) ;
if ( root != NULL )
QueuePush ( & q, root) ;
while ( QueueEmpty ( & q) != 0 )
{
BTNode* front = QueueFront ( & q) ;
QueuePop ( & q) ;
if ( front == NULL )
{
break ;
}
QueuePush ( & q, front-> _left) ;
QueuePush ( & q, front-> _right) ;
}
while ( QueueEmpty ( & q) != 0 )
{
BTNode* front = QueueFront ( & q) ;
QueuePop ( & q) ;
if ( front != NULL )
{
return 0 ;
}
}
return 1 ;
QueueDestory ( & q) ;
}
Queue.h
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include "Btree.h"
typedef BTNode* ElemType;
typedef struct sqQueue
{
ElemType _data;
struct sqQueue* _next;
} sqQueue;
typedef struct Queue
{
sqQueue* _front;
sqQueue* _tail;
} Queue;
void QueueInit ( Queue * q) ;
void QueueDestory ( Queue * q) ;
int QueueEmpty ( Queue * q) ;
void QueuePush ( Queue * q, ElemType x) ;
void QueuePop ( Queue * q) ;
ElemType QueueFront ( Queue * q) ;
Queue.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
void QueueInit ( Queue * q)
{
assert ( q) ;
q-> _front = q-> _tail = NULL ;
}
void QueueDestory ( Queue * q)
{
assert ( q) ;
sqQueue* cur = q-> _front;
while ( cur)
{
sqQueue* next = cur-> _next;
free ( cur) ;
cur = next;
}
q-> _front = q-> _tail = NULL ;
}
int QueueEmpty ( Queue * q)
{
return q-> _front == NULL ? 0 : 1 ;
}
void QueuePush ( Queue * q, ElemType x)
{
assert ( q) ;
sqQueue* node = ( sqQueue * ) malloc ( sizeof ( sqQueue) ) ;
node-> _data = x;
node-> _next = NULL ;
if ( q-> _tail == NULL )
{
q-> _tail = q-> _front = node;
}
else
{
q-> _tail-> _next = node;
q-> _tail = node;
}
}
void QueuePop ( Queue * q)
{
assert ( q) ;
sqQueue* front = q-> _front-> _next;
free ( q-> _front) ;
q-> _front = front;
if ( front == NULL )
q-> _tail = NULL ;
}
ElemType QueueFront ( Queue * q)
{
assert ( q) ;
ElemType tmp = q-> _front-> _data;
return tmp;
}
源文件 main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "BTree.h"
#include "Queue.h"
void testTree ( )
{
char arr[ ] = "ABD##E#H##CF##G##" ;
int sz = sizeof ( arr) / sizeof ( arr[ 0 ] ) ;
int i = 0 ;
BTNode* b = BinaryTreeCreate ( arr, sz, & i) ;
printf ( "遍历:\n" ) ;
BinaryTreePrevOrder ( b) ;
printf ( "\n" ) ;
BinaryTreeInOrder ( b) ;
printf ( "\n" ) ;
BinaryTreePostOrder ( b) ;
printf ( "\n" ) ;
printf ( "结点个数: " ) ;
int num = BinaryTreeSize ( b) ;
printf ( "%d\n" , num) ;
printf ( "叶子结点个数: " ) ;
int n = BinaryTreeLeafSize ( b) ;
printf ( "%d\n" , n) ;
printf ( "第K层结点个数: " ) ;
int m = BinaryTreeLevelKSize ( b, 4 ) ;
printf ( "%d\n" , m) ;
BTNode* ret = BinaryTreeFind ( b, 'C' ) ;
printf ( "%c\n" , ret-> _data) ;
BinaryTreeLevelOrder ( b) ;
printf ( "\n" ) ;
printf ( "是否为完全二叉树(是为1,否为0): " ) ;
printf ( "%d\n" , BinaryTreeComplete ( b) ) ;
printf ( "\n" ) ;
BinaryTreeDestory ( b) ;
}
int main ( )
{
testTree ( ) ;
system ( "pause" ) ;
return 0 ;
}