一、栈
头文件(Stack.h)
# pragma once
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
typedef int StackData;
typedef struct Stack
{
StackData* _a;
int _top;
int _capacity;
} Sta;
void StackInit ( Sta* pst) ;
void StackDestroy ( Sta* pst) ;
void StackPush ( Sta* pst, StackData x) ;
void StackPop ( Sta* pst) ;
int StackSize ( Sta* pst) ;
int StackEmpty ( Sta* pst) ;
StackData StackTop ( Sta* pst) ;
函数接口部分(Stack.c)
# include "Stack.h"
void StackInit ( Sta* pst)
{
assert ( pst) ;
pst-> _a = NULL ;
pst-> _top = 0 ;
pst-> _capacity = 0 ;
}
void StackDestroy ( Sta* pst)
{
assert ( pst) ;
free ( pst-> _a) ;
pst-> _a = NULL ;
pst-> _top = 0 ;
pst-> _capacity = 0 ;
}
void StackPush ( Sta* pst, StackData x)
{
assert ( pst) ;
if ( pst-> _top == pst-> _capacity)
{
int capacity = pst-> _capacity == 0 ? 4 : pst-> _capacity * 2 ;
StackData* tmp = ( StackData* ) realloc ( pst-> _a, sizeof ( StackData) * capacity) ;
if ( tmp != NULL )
{
pst-> _a = tmp;
}
else
{
printf ( "realloc error" ) ;
exit ( - 1 ) ;
}
pst-> _capacity = capacity;
}
pst-> _a[ pst-> _top] = x;
pst-> _top += 1 ;
}
void StackPop ( Sta* pst)
{
assert ( pst) ;
assert ( pst-> _top > 0 ) ;
pst-> _top-- ;
}
int StackSize ( Sta* pst)
{
assert ( pst) ;
return pst-> _top;
}
int StackEmpty ( Sta* pst)
{
assert ( pst) ;
return pst-> _top > 0 ? 0 : 1 ;
}
StackData StackTop ( Sta* pst)
{
assert ( pst) ;
assert ( pst-> _top > 0 ) ;
return pst-> _a[ pst-> _top - 1 ] ;
}
测试用例(test.c)
# include "Stack.h"
void test1 ( )
{
Sta stack;
StackInit ( & stack) ;
StackPush ( & stack, 1 ) ;
StackPush ( & stack, 2 ) ;
StackPush ( & stack, 3 ) ;
StackPush ( & stack, 4 ) ;
StackPush ( & stack, 5 ) ;
StackPop ( & stack) ;
StackPop ( & stack) ;
while ( StackEmpty ( & stack) != 1 )
{
printf ( "%d " , StackTop ( & stack) ) ;
StackPop ( & stack) ;
}
StackDestroy ( & stack) ;
}
int main ( )
{
test1 ( ) ;
return 0 ;
}
二、队列
头文件(Queue.h)
# pragma once
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# include <stdbool.h>
typedef int QueDataType;
typedef struct QueueNode
{
struct QueueNode * Next;
QueDataType data;
} QueNode;
typedef struct Queue
{
QueNode* _head;
QueNode* _tail;
} Que;
void QueueInit ( Que* pq) ;
void QueueDestroy ( Que* pq) ;
void QueuePush ( Que* pq, QueDataType x) ;
void QueuePop ( Que* pq) ;
QueDataType QueueFront ( Que* pq) ;
QueDataType QueueBack ( Que* pq) ;
int QueueSize ( Que* pq) ;
bool QueueEmpty ( Que* qp) ;
函数接口部分(Queue.c)
# include "Queue.h"
void QueueInit ( Que* pq)
{
assert ( pq) ;
pq-> _tail = pq-> _head = NULL ;
}
void QueueDestroy ( Que* pq)
{
assert ( pq) ;
QueNode* cur = pq-> _head;
while ( cur != NULL )
{
QueNode* next = cur-> Next;
free ( cur) ;
cur = next;
}
pq-> _head = pq-> _tail = NULL ;
}
void QueuePush ( Que* pq, QueDataType x)
{
assert ( pq) ;
QueNode* newNode = ( QueNode* ) malloc ( sizeof ( QueNode) ) ;
if ( newNode != NULL )
{
newNode-> data = x;
newNode-> Next = NULL ;
}
else
{
printf ( "malloc error" ) ;
exit ( - 1 ) ;
}
if ( QueueEmpty ( pq) )
{
pq-> _tail = pq-> _head = newNode;
}
else
{
pq-> _tail-> Next = newNode;
pq-> _tail = newNode;
}
}
void QueuePop ( Que* pq)
{
assert ( pq) ;
assert ( ! ( QueueEmpty ( pq) ) ) ;
QueNode* headnext = pq-> _head-> Next;
free ( pq-> _head) ;
pq-> _head = headnext;
if ( pq-> _head == NULL )
{
pq-> _tail = NULL ;
}
}
bool QueueEmpty ( Que* pq)
{
assert ( pq) ;
return pq-> _head == NULL ;
}
QueDataType QueueBack ( Que* pq)
{
assert ( pq) ;
assert ( ! ( QueueEmpty ( pq) ) ) ;
return pq-> _tail-> data;
}
QueDataType QueueFront ( Que* pq)
{
assert ( pq) ;
assert ( ! ( QueueEmpty ( pq) ) ) ;
return pq-> _head-> data;
}
int QueueSize ( Que* pq)
{
assert ( pq) ;
int count = 0 ;
QueNode* cur = pq-> _head;
while ( cur != NULL )
{
cur = cur-> Next;
count++ ;
}
return count;
}
测试用例(test.c)
# include "Queue.h"
void test1 ( )
{
Que queue;
QueueInit ( & queue) ;
QueuePush ( & queue, 1 ) ;
QueuePush ( & queue, 2 ) ;
QueuePush ( & queue, 3 ) ;
QueuePop ( & queue) ;
while ( ! QueueEmpty ( & queue) )
{
printf ( "%d " , QueueFront ( & queue) ) ;
QueuePop ( & queue) ;
}
printf ( "\n" ) ;
QueueDestroy ( & queue) ;
}
int main ( )
{
test1 ( ) ;
return 0 ;
}
三、无头单向非循环链表
头文件(SL.h)
# pragma once
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
typedef int typeSL;
typedef struct SListNode
{
typeSL data;
struct SListNode * next;
} SLNode;
void SListpushfront ( SLNode * * pphead, typeSL x) ;
void SListpopfront ( SLNode* * pphead) ;
void SListpushback ( SLNode* * pphead, typeSL x) ;
void SListpopback ( SLNode* * pphead) ;
void SListInsertAfter ( SLNode* pos, typeSL x) ;
void SListEraseAfter ( SLNode* phead) ;
void SListInsert ( SLNode* * pphead, SLNode* pos, typeSL x) ;
void SListErase ( SLNode* * pphead, SLNode* pos) ;
SLNode* SListFind ( SLNode* phead , typeSL x) ;
void SListprint ( SLNode* phead) ;
SLNode* BuySListNode ( typeSL x) ;
void SListDestory ( SLNode* * pphead) ;
函数接口部分(SL.c)
# include "SL.h"
SLNode* BuySListNode ( typeSL x)
{
SLNode* newNode = ( SLNode* ) malloc ( sizeof ( SLNode) ) ;
if ( newNode == NULL )
{
perror ( "Buy malloc" ) ;
exit ( - 1 ) ;
}
newNode-> data = x;
newNode-> next = NULL ;
return newNode;
}
void SListpushback ( SLNode* * pphead, typeSL x)
{
assert ( pphead) ;
SLNode* newNode = BuySListNode ( x) ;
if ( * pphead == NULL )
{
* pphead = newNode;
}
else
{
SLNode* tail = * pphead;
while ( tail-> next)
{
tail = tail-> next;
}
tail-> next = newNode;
}
}
void SListpopback ( SLNode* * pphead)
{
assert ( pphead) ;
if ( * pphead == NULL )
{
return ;
}
else if ( ( * pphead) -> next == NULL )
{
free ( * pphead) ;
* pphead = NULL ;
}
else
{
SLNode* prev = NULL ;
SLNode* tail = * pphead;
while ( tail-> next)
{
prev = tail;
tail = tail-> next;
}
free ( tail) ;
prev-> next = NULL ;
}
}
void SListprint ( SLNode* phead)
{
SLNode* cur = phead;
while ( cur)
{
printf ( "%d->" , cur-> data ) ;
cur = cur-> next;
}
printf ( "NULL\n" ) ;
}
void SListpushfront ( SLNode* * pphead, typeSL x)
{
assert ( pphead) ;
SLNode* newNode = BuySListNode ( x) ;
newNode-> next = * pphead;
* pphead = newNode;
}
void SListpopfront ( SLNode* * pphead)
{
assert ( pphead) ;
if ( * pphead == NULL )
{
return ;
}
else
{
SLNode* Next = ( * pphead) -> next;
free ( * pphead) ;
* pphead = Next;
}
}
SLNode* SListFind ( SLNode* phead, typeSL x)
{
SLNode* pos = phead;
while ( pos)
{
if ( pos-> data == x)
{
return pos;
}
pos = pos-> next;
}
return NULL ;
}
void SListInsertAfter ( SLNode* pos, typeSL x)
{
assert ( pos) ;
SLNode* newNode = BuySListNode ( x) ;
newNode-> next = pos-> next;
pos-> next = newNode;
}
void SListEraseAfter ( SLNode* pos)
{
assert ( pos) ;
if ( pos-> next)
{
SLNode* posnext = pos-> next;
SLNode* posnextnext = posnext-> next;
pos-> next = posnextnext;
free ( posnext) ;
}
}
void SListDestory ( SLNode* * pphead)
{
assert ( pphead) ;
SLNode* cur = * pphead;
while ( cur)
{
SLNode* next = cur-> next;
free ( cur) ;
cur = next;
}
* pphead = NULL ;
}
void SListInsert ( SLNode* * pphead, SLNode* pos, typeSL x)
{
assert ( pphead) ;
assert ( pos) ;
SLNode* newNode = BuySListNode ( x) ;
if ( * pphead == pos)
{
newNode-> next = pos;
* pphead = newNode;
}
else
{
SLNode* pospre = * pphead;
while ( pospre-> next != pos)
{
pospre = pospre-> next;
}
pospre-> next = newNode;
newNode-> next = pos;
}
}
void SListErase ( SLNode* * pphead, SLNode* pos)
{
assert ( pphead) ;
assert ( pos) ;
if ( pos == * pphead)
{
SListpopfront ( pphead) ;
}
else
{
SLNode* pospre = * pphead;
while ( pospre-> next != pos)
{
pospre = pospre-> next;
}
pospre-> next = pos-> next;
free ( pos) ;
}
}
测试用例(test.c)
# include "SL.h"
void test1 ( )
{
SLNode* phead = NULL ;
SListpushfront ( & phead, 1 ) ;
SListpushfront ( & phead, 2 ) ;
SListpushfront ( & phead, 3 ) ;
SListpushfront ( & phead, 4 ) ;
SListprint ( phead) ;
SListpopfront ( & phead) ;
SListpopfront ( & phead) ;
SListpopfront ( & phead) ;
SListprint ( phead) ;
SListpushback ( & phead, 2 ) ;
SListpushback ( & phead, 3 ) ;
SListpushback ( & phead, 4 ) ;
SListpushback ( & phead, 5 ) ;
SListprint ( phead) ;
SListpopback ( & phead) ;
SListpopback ( & phead) ;
SListpopback ( & phead) ;
SListprint ( phead) ;
SListInsertAfter ( SListFind ( phead, 1 ) , 3 ) ;
SListprint ( phead) ;
SListEraseAfter ( SListFind ( phead, 3 ) ) ;
SListprint ( phead) ;
SListDestory ( & phead) ;
SListprint ( phead) ;
}
void test2 ( )
{
SLNode* phead = NULL ;
SListpushfront ( & phead, 1 ) ;
SListpushfront ( & phead, 2 ) ;
SListpushfront ( & phead, 3 ) ;
SListpushfront ( & phead, 4 ) ;
SListprint ( phead) ;
SListInsert ( phead, SListFind ( phead, 3 ) , 1 ) ;
SListprint ( phead) ;
}
int main ( )
{
test1 ( ) ;
return 0 ;
}
四、有头双向循环链表
头文件(ListNode.h)
# pragma once
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
typedef int ListNode;
typedef struct ListNode
{
ListNode data;
ListNode* next;
ListNode* prev;
} LNode;
LNode* ListNodeInit ( ) ;
void ListNodepushback ( LNode* phead, ListNode x) ;
void ListNodepopback ( LNode* phead) ;
void ListNodepushfront ( LNode* phead, ListNode x) ;
void ListNodepopfront ( LNode* phead) ;
void ListNodeDestroy ( LNode* phead) ;
LNode* ListNodeFind ( LNode* phead, ListNode x) ;
void ListNodepop ( LNode* pos) ;
void ListNodepush ( LNode* pos, ListNode x) ;
LNode* BuyListNode ( ListNode x) ;
void ListNodePrint ( LNode* phead) ;
函数接口部分(ListNode.c)
# include "ListNode.h"
LNode* BuyListNode ( ListNode x)
{
LNode* newNode = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( newNode != NULL )
{
newNode-> data = x;
}
else
{
printf ( "newNode创建失败" ) ;
exit ( - 1 ) ;
}
return newNode;
}
LNode* ListNodeInit ( )
{
LNode* phead = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( phead != NULL )
{
phead-> next = phead;
phead-> prev = phead;
return phead;
}
else
{
printf ( "init error" ) ;
exit ( - 1 ) ;
}
}
void ListNodePrint ( LNode* phead)
{
assert ( phead) ;
LNode* cur = phead-> next;
while ( cur != phead)
{
printf ( "%d " , cur-> data) ;
cur = cur-> next;
}
}
void ListNodepushback ( LNode* phead, ListNode x)
{
assert ( phead) ;
ListNodepush ( phead, x) ;
}
void ListNodepopback ( LNode* phead)
{
assert ( phead) ;
LNode* tail = phead-> prev;
assert ( tail != phead) ;
ListNodepop ( tail) ;
}
void ListNodepushfront ( LNode* phead, ListNode x)
{
ListNodepush ( phead-> next, x) ;
}
void ListNodepopfront ( LNode* phead)
{
assert ( phead) ;
assert ( phead-> next != phead) ;
ListNodepop ( phead-> next) ;
}
void ListNodeDestroy ( LNode* phead)
{
assert ( phead) ;
LNode* cur = phead-> next;
while ( cur != phead)
{
LNode* curnext = cur-> next;
free ( cur) ;
cur = curnext;
}
free ( phead) ;
phead = NULL ;
}
LNode* ListNodeFind ( LNode* phead, ListNode x)
{
LNode* cur = phead-> next;
while ( cur != phead)
{
if ( cur-> data == x)
{
return cur;
}
cur = cur-> next;
}
return NULL ;
}
void ListNodepop ( LNode* pos)
{
assert ( pos) ;
LNode* posnext = pos-> next;
LNode* posprev = pos-> prev;
posnext-> prev = posprev;
posprev-> next = posnext;
free ( pos) ;
pos = NULL ;
}
void ListNodepush ( LNode* pos, ListNode x)
{
assert ( pos) ;
LNode* newNode = BuyListNode ( x) ;
LNode* posprev = pos-> prev;
newNode-> prev = posprev;
newNode-> next = pos;
posprev-> next = newNode;
pos-> prev = newNode;
}
测试用例(test.c)
# include "ListNode.h"
void test1 ( )
{
LNode* phead = ListNodeInit ( ) ;
ListNodepushback ( phead, 1 ) ;
ListNodepushback ( phead, 2 ) ;
ListNodepushback ( phead, 3 ) ;
ListNodepopback ( phead) ;
ListNodepopback ( phead) ;
ListNodepopback ( phead) ;
ListNodepushfront ( phead, 1 ) ;
ListNodepushfront ( phead, 2 ) ;
ListNodepushfront ( phead, 3 ) ;
ListNodepopfront ( phead) ;
ListNodepush ( ListNodeFind ( phead, 2 ) , 4 ) ;
ListNodepush ( ListNodeFind ( phead, 2 ) , 3 ) ;
ListNodepush ( ListNodeFind ( phead, 2 ) , 3 ) ;
ListNodepop ( ListNodeFind ( phead, 4 ) ) ;
ListNodepop ( ListNodeFind ( phead, 1 ) ) ;
ListNodepop ( ListNodeFind ( phead, 3 ) ) ;
ListNodePrint ( phead) ;
ListNodeDestroy ( phead) ;
phead = NULL ;
}
void test2 ( )
{
LNode* phead = ListNodeInit ( ) ;
ListNodepushback ( phead, 1 ) ;
ListNodepushback ( phead, 2 ) ;
ListNodepushback ( phead, 3 ) ;
ListNodepopback ( phead) ;
ListNodepopback ( phead) ;
ListNodepopback ( phead) ;
ListNodepushfront ( phead, 1 ) ;
ListNodepushfront ( phead, 2 ) ;
ListNodepushfront ( phead, 3 ) ;
ListNodepopfront ( phead) ;
ListNodepopfront ( phead) ;
ListNodepopfront ( phead) ;
ListNodepopfront ( phead) ;
ListNodePrint ( phead) ;
}
int main ( )
{
test2 ( ) ;
return 0 ;
}
五、堆
头文件(Heap.h)
# pragma once
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <assert.h>
# include <stdlib.h>
# include <string.h>
typedef int Hpdatatype;
typedef struct Heap
{
Hpdatatype* _a;
int _size;
int _capacity;
} Heap;
void HeapInit ( Heap* php, Hpdatatype* x, int n) ;
void AdjustDown ( Hpdatatype* a, int n, int root) ;
void swap ( Hpdatatype* a, Hpdatatype* b) ;
void HeapDestroy ( Heap* php) ;
void HeapPush ( Heap* php, Hpdatatype x) ;
void HeapPop ( Heap* php) ;
Hpdatatype Heaptop ( Heap* php) ;
函数接口的实现(Heap.c)
# include "Heap.h"
void swap ( Hpdatatype * a, Hpdatatype * b)
{
Hpdatatype* tmp = * a;
* a = * b;
* b = tmp;
}
void AdjustDown ( Hpdatatype * a, int n , int root)
{
int parant = root;
int child = parant * 2 + 1 ;
while ( child < n)
{
if ( child + 1 < n && a[ child + 1 ] < a[ child] )
{
child = child + 1 ;
}
if ( a[ parant] > a[ child] )
{
swap ( & a[ parant] , & a[ child] ) ;
parant = child;
child = child * 2 + 1 ;
}
else
{
break ;
}
}
}
void HeapInit ( Heap* php, Hpdatatype* x, int n)
{
php-> _a = ( Hpdatatype* ) malloc ( sizeof ( Heap) * n) ;
if ( php-> _a != NULL )
{
memcpy ( php-> _a, x, sizeof ( Hpdatatype) * n) ;
}
else
{
printf ( "malloc error" ) ;
exit ( - 1 ) ;
}
php-> _size = n;
php-> _capacity = n;
for ( int i = ( n - 1 - 1 ) / 2 ; i >= 0 ; i-- )
{
AdjustDown ( php-> _a, php-> _size, i) ;
}
}
void HeapDestroy ( Heap* php)
{
assert ( php) ;
free ( php-> _a) ;
php-> _a = NULL ;
php-> _size = 0 ;
php-> _capacity = 0 ;
}
void AdjustUp ( Hpdatatype* a, int n, int child)
{
int parent = ( child - 1 ) / 2 ;
while ( child > 0 )
{
if ( a[ child] >= a[ parent] )
{
break ;
}
else
{
swap ( & a[ child] , & a[ parent] ) ;
child = parent;
parent = ( parent - 1 ) / 2 ;
}
}
}
void HeapPush ( Heap* php, Hpdatatype x)
{
assert ( php) ;
if ( php-> _size == php-> _capacity)
{
php-> _capacity *= 2 ;
Hpdatatype* tmp = ( Hpdatatype* ) realloc ( php-> _a, sizeof ( Hpdatatype) * php-> _capacity) ;
if ( tmp != NULL )
{
php-> _a = tmp;
}
else
{
printf ( "realloc error" ) ;
exit ( - 1 ) ;
}
}
php-> _a[ php-> _size] = x;
++ php-> _size;
AdjustUp ( php-> _a, php-> _size, php-> _size - 1 ) ;
}
void HeapPop ( Heap* php)
{
assert ( php) ;
assert ( php-> _size > 0 ) ;
swap ( & php-> _a[ 0 ] , & php-> _a[ php-> _size - 1 ] ) ;
-- php-> _size;
AdjustDown ( php-> _a, php-> _size, 0 ) ;
}
Hpdatatype Heaptop ( Heap* php)
{
assert ( php) ;
assert ( php-> _size > 0 ) ;
return php-> _a[ 0 ] ;
}
测试用例(test.c)
# include "Heap.h"
void HeapSort ( int * a, int n)
{
for ( int i = ( n - 1 - 1 ) / 2 ; i >= 0 ; -- i)
{
AdjustDown ( a, n, i) ;
}
int end = n - 1 ;
while ( end > 0 )
{
swap ( & a[ 0 ] , & a[ end] ) ;
AdjustDown ( a, end, 0 ) ;
-- end;
}
}
void test2 ( )
{
int arr[ ] = { 27 , 15 , 19 , 18 , 28 , 34 , 65 , 49 , 25 , 37 } ;
HeapSort ( arr, sizeof ( arr) / sizeof ( Hpdatatype) ) ;
}
void test1 ( )
{
int arr[ ] = { 27 , 15 , 19 , 18 , 28 , 34 , 65 , 49 , 25 , 37 } ;
Heap hp;
HeapInit ( & hp, arr, sizeof ( arr) / sizeof ( Hpdatatype) ) ;
HeapPush ( & hp, 3 ) ;
HeapPush ( & hp, 66 ) ;
}
int main ( )
{
test1 ( ) ;
return 0 ;
}
六、完全二叉树
头文件(BinaryTree.h)
# pragma once
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# include <string.h>
# include <stdbool.h>
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode * _left;
struct BinaryTreeNode * _right;
} BTNode;
BTNode* CreatBTNode ( BTDataType x) ;
int BTSize ( BTNode* root) ;
int BTLeafSize ( BTNode* root) ;
int BTDeepSize ( BTNode* root) ;
void PrintPrevOrder ( BTNode* root) ;
void PrintInOrder ( BTNode* root) ;
void PrintBackOrder ( BTNode* root) ;
int BTLevelKSize ( BTNode* root, int k) ;
BTNode* BTFindX ( BTNode* root, BTDataType x) ;
void BTDestroy ( BTNode* root) ;
void PrintLevelOrder ( BTNode* root) ;
bool BTComplete ( BTNode* root) ;
typedef BTNode* QueDataType;
typedef struct QueueNode
{
struct QueueNode * Next;
QueDataType data;
} QueNode;
typedef struct Queue
{
QueNode* _head;
QueNode* _tail;
} Que;
void QueueInit ( Que* pq) ;
void QueueDestroy ( Que* pq) ;
void QueuePush ( Que* pq, QueDataType x) ;
void QueuePop ( Que* pq) ;
QueDataType QueueFront ( Que* pq) ;
QueDataType QueueBack ( Que* pq) ;
int QueueSize ( Que* pq) ;
bool QueueEmpty ( Que* qp) ;
函数接口的实现(BinaryTree.c)
# include "BinaryTree.h"
void PrintPrevOrder ( BTNode* root)
{
if ( root == NULL )
{
printf ( "NULL " ) ;
return ;
}
printf ( "%c " , root-> _data) ;
PrintPrevOrder ( root-> _left) ;
PrintPrevOrder ( root-> _right) ;
}
void PrintInOrder ( BTNode* root)
{
if ( root == NULL )
{
printf ( "NULL " ) ;
return ;
}
PrintInOrder ( root-> _left) ;
printf ( "%c " , root-> _data) ;
PrintInOrder ( root-> _right) ;
}
void PrintBackOrder ( BTNode* root)
{
if ( root == NULL )
{
printf ( "NULL " ) ;
return ;
}
PrintBackOrder ( root-> _left) ;
PrintBackOrder ( root-> _right) ;
printf ( "%c " , root-> _data) ;
}
BTNode* CreatBTNode ( BTDataType x)
{
BTNode* node = ( BTNode* ) malloc ( sizeof ( BTNode) ) ;
if ( node != NULL )
{
node-> _left = node-> _right = NULL ;
node-> _data = x;
return node;
}
else
{
printf ( "Creat malloc error" ) ;
exit ( - 1 ) ;
}
}
int BTSize ( BTNode* root)
{
if ( root == NULL )
{
return 0 ;
}
else
{
return 1 + BTSize ( root-> _left) + BTSize ( root-> _right) ;
}
}
int BTLeafSize ( BTNode* root)
{
if ( root == NULL )
{
return 0 ;
}
if ( root-> _left == NULL && root-> _right == NULL )
{
return 1 ;
}
else
{
return BTLeafSize ( root-> _left) + BTLeafSize ( root-> _right) ;
}
}
int BTDeepSize ( BTNode* root)
{
if ( root == NULL )
{
return 0 ;
}
int leftdepth = BTDeepSize ( root-> _left) ;
int rightdepth = BTDeepSize ( root-> _right) ;
return leftdepth > rightdepth ? leftdepth + 1 : rightdepth + 1 ;
}
int BTLevelKSize ( BTNode* root, int k)
{
if ( root == NULL )
{
return 0 ;
}
if ( k == 1 )
{
return 1 ;
}
return BTLevelKSize ( root-> _left, k - 1 ) + BTLevelKSize ( root-> _right, k - 1 ) ;
}
BTNode* BTFindX ( BTNode* root, BTDataType x)
{
if ( root == NULL )
{
return NULL ;
}
if ( root-> _data == x)
{
return root;
}
BTNode* node = BTFindX ( root-> _left, x) ;
if ( node)
{
return node;
}
node = BTFindX ( root-> _left, x) ;
if ( node)
{
return node;
}
return NULL ;
}
void BTDestroy ( BTNode* root)
{
if ( root == NULL )
{
return ;
}
BTDestroy ( root-> _left) ;
BTDestroy ( root-> _right) ;
free ( root) ;
}
void PrintLevelOrder ( BTNode* root)
{
Que que;
QueueInit ( & que) ;
QueuePush ( & que, root) ;
while ( ! ( QueueEmpty ( & que) ) )
{
BTNode* front = QueueFront ( & que) ;
QueuePop ( & que) ;
printf ( "%c " , front-> _data) ;
if ( front-> _left)
{
QueuePush ( & que, front-> _left) ;
}
if ( front-> _right)
{
QueuePush ( & que, front-> _right) ;
}
}
QueueDestroy ( & que) ;
}
bool BTComplete ( BTNode* root)
{
if ( root == NULL )
{
return true;
}
Que que;
QueueInit ( & que) ;
while ( ! QueueEmpty ( & que) )
{
BTNode* front = QueueFront ( & que) ;
QueuePop ( & que) ;
if ( front == NULL )
{
break ;
}
QueuePush ( & que, front-> _left) ;
QueuePush ( & que, front-> _right) ;
}
while ( ! QueueEmpty ( & que) )
{
BTNode* front = QueueFront ( & que) ;
if ( front != NULL )
{
QueueDestroy ( & que) ;
return false;
}
QueuePop ( & que) ;
}
return true;
QueueDestroy ( & que) ;
}
队列的引用接口实现部分(Queue.c)
# include "BinaryTree.h"
void QueueInit ( Que* pq)
{
assert ( pq) ;
pq-> _tail = pq-> _head = NULL ;
}
void QueueDestroy ( Que* pq)
{
assert ( pq) ;
QueNode* cur = pq-> _head;
while ( cur != NULL )
{
QueNode* next = cur-> Next;
free ( cur) ;
cur = next;
}
pq-> _head = pq-> _tail = NULL ;
}
void QueuePush ( Que* pq, QueDataType x)
{
assert ( pq) ;
QueNode* newNode = ( QueNode* ) malloc ( sizeof ( QueNode) ) ;
if ( newNode != NULL )
{
newNode-> data = x;
newNode-> Next = NULL ;
}
else
{
printf ( "malloc error" ) ;
exit ( - 1 ) ;
}
if ( QueueEmpty ( pq) )
{
pq-> _tail = pq-> _head = newNode;
}
else
{
pq-> _tail-> Next = newNode;
pq-> _tail = newNode;
}
}
void QueuePop ( Que* pq)
{
assert ( pq) ;
assert ( ! ( QueueEmpty ( pq) ) ) ;
QueNode* headnext = pq-> _head-> Next;
free ( pq-> _head) ;
pq-> _head = headnext;
if ( pq-> _head == NULL )
{
pq-> _tail = NULL ;
}
}
bool QueueEmpty ( Que* pq)
{
assert ( pq) ;
return pq-> _head == NULL ;
}
QueDataType QueueBack ( Que* pq)
{
assert ( pq) ;
assert ( ! ( QueueEmpty ( pq) ) ) ;
return pq-> _tail-> data;
}
QueDataType QueueFront ( Que* pq)
{
assert ( pq) ;
assert ( ! ( QueueEmpty ( pq) ) ) ;
return pq-> _head-> data;
}
int QueueSize ( Que* pq)
{
assert ( pq) ;
int count = 0 ;
QueNode* cur = pq-> _head;
while ( cur != NULL )
{
cur = cur-> Next;
count++ ;
}
return count;
}
测试用例(test.c)
# include "BinaryTree.h"
int main ( )
{
BTNode* A = CreatBTNode ( 'A' ) ;
BTNode* B = CreatBTNode ( 'B' ) ;
BTNode* C = CreatBTNode ( 'C' ) ;
BTNode* D = CreatBTNode ( 'D' ) ;
BTNode* E = CreatBTNode ( 'E' ) ;
A-> _left = B;
A-> _right = C;
B-> _left = D;
B-> _right = E;
PrintPrevOrder ( A) ;
printf ( "\n" ) ;
PrintInOrder ( A) ;
printf ( "\n" ) ;
PrintBackOrder ( A) ;
printf ( "\n" ) ;
PrintLevelOrder ( A) ;
printf ( "\n" ) ;
printf ( "ATree %d\n" , BTSize ( A) ) ;
printf ( "BTree %d\n" , BTSize ( B) ) ;
printf ( "ATree leaf %d\n" , BTLeafSize ( A) ) ;
printf ( "BTree leaf %d\n" , BTLeafSize ( B) ) ;
printf ( "ATree Deep %d\n" , BTDeepSize ( A) ) ;
printf ( "ATree Deep %d\n" , BTDeepSize ( A) ) ;
printf ( "BTree Deep %d\n" , BTDeepSize ( B) ) ;
if ( BTComplete ( A) )
{
printf ( "complete" ) ;
}
else
{
printf ( "uncomplete" ) ;
}
return 0 ;
}