数据结构——二叉树的操作
#include<stdio.h>
#include<stdlib.h>
#include"BinaryTree.h"
#include"Stack.h"
#include"queue.h"
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;
}
}
int BinaryTreeSize(BTNode* root){
if(!root)
return 0;
else
return 1+BinaryTreeSize(root->_left)+BinaryTreeSize(root->_right);
}
void BinaryTreeSize2(BTNode* root, int* num){
if(!root)
return;
else{
(*num)++;
BinaryTreeSize2(root->_left,num);
BinaryTreeSize2(root->_right,num);
}
}
int BinaryTreeLeafSize(BTNode* root){
if(!root) return 0;
if(!root->_left&&!root->_right)
return 1;
return BinaryTreeLeafSize(root->_left)+BinaryTreeLeafSize(root->_right);
}
int BinaryTreeLevelKSize(BTNode* root, int k){
if(!root||k<1)
return 0;
if(k==1)
return 1;
else
return BinaryTreeLevelKSize(root->_left,k-1)+BinaryTreeLevelKSize(root->_right,k-1);
}
BTNode* BinaryTreeFind(BTNode* root, BTDataType x){
if(!root)
return NULL;
else{
if(root->_data==x)
return root;
BTNode* node=BinaryTreeFind(root->_left,x);
if(!node)
node=BinaryTreeFind(root->_right,x);
return node;
}
}
void BinaryTreePrevOrder(BTNode* root){
if(!root)
return;
else{
printf("%c ",root->_data);
BinaryTreePrevOrder(root->_left);
BinaryTreePrevOrder(root->_right);
}
}
void BinaryTreePrevOrderNoR(BTNode* root){
Stack st;
stackInit(&st, 10);
BTNode *cur=root,*top=NULL;
while(cur||!stackEmpty(&st)){
while(cur){
printf("%c ",cur->_data);
stackPush(&st,cur);
cur=cur->_left;
}
top=stackTop(&st);
stackPop(&st);
cur=top->_right;
}
}
void BinaryTreeInOrder(BTNode* root){
if(!root)
return;
else{
BinaryTreeInOrder(root->_left);
printf("%c ",root->_data);
BinaryTreeInOrder(root->_right);
}
}
void BinaryTreeInOrderNoR(BTNode* root){
Stack st;
stackInit(&st, 10);
BTNode *cur=root,*top=NULL;
while(cur||!stackEmpty(&st)){
while(cur){
stackPush(&st,cur);
cur=cur->_left;
}
top=stackTop(&st);
printf("%c ",top->_data);
stackPop(&st);
cur=top->_right;
}
}
void BinaryTreePostOrder(BTNode* root){
if(!root)
return;
else{
BinaryTreePostOrder(root->_left);
BinaryTreePostOrder(root->_right);
printf("%c ",root->_data);
}
}
void BinaryTreePostOrderNoR(BTNode* root){
Stack st;
stackInit(&st, 10);
BTNode *cur=root,*top=NULL,*prev=NULL;
while(cur||!stackEmpty(&st)){
while(cur){
stackPush(&st,cur);
cur=cur->_left;
}
top=stackTop(&st);
if(!top->_right||top->_right==prev){
printf("%c ",top->_data);
stackPop(&st);
prev=top;
}else
cur=top->_right;
}
}
void BinaryTreeLevelOrder(BTNode* root){
Queue q;
queueInit(&q);
BTNode *front=NULL;
if(root)
queuePush(&q,root);
while(!queueEmpty(&q)){
front=queueFront(&q);
queuePop(&q);
printf("%c ",front->_data);
if(front->_left)
queuePush(&q,front->_left);
if(front->_right)
queuePush(&q,front->_right);
}
}
int BinaryTreeComplete(BTNode* root){
Queue q;
queueInit(&q);
BTNode* front=NULL;
if(root)
queuePush(&q,root);
while(!queueEmpty(&q)){
front=queueFront(&q);
queuePop(&q);
if(front){
queuePush(&q,front->_left);
queuePush(&q,front->_right);
}else
break;
}
while(!queueEmpty(&q)){
queuePop(&q);
BTNode* node=queueFront(&q);
if(node)
return 0;
}
return 1;
}