# 二叉树的基本操作及应用

BTree.h

#ifndef __BTree_H__
#define __BTree_H__

#include<stdlib.h>
#include<stdio.h>
#include<assert.h>

typedef char DataType;

typedef struct BTreeNode
{
DataType data;
struct BTreeNode *left;
struct BTreeNode *right;
}BTreeNode;

void CreatBTree(BTreeNode **cur, DataType *str, DataType invalid, int *index);
void Preorder(BTreeNode *cur);
void Inorder(BTreeNode *cur);
void Postorder(BTreeNode *cur);
void Destroy(BTreeNode **cur);
void CpBTree(BTreeNode *cur, BTreeNode **ret);
int BTreeHight(BTreeNode *cur);
int BTreeNum(BTreeNode *cur);
int BTree(BTreeNode *cur);
int BTreeKnum(BTreeNode *cur, int K);
BTreeNode *LeftNode(BTreeNode *cur, DataType data);
BTreeNode *RightNode(BTreeNode *cur, DataType  data);
BTreeNode* ParentNode(BTreeNode *cur, DataType  data);
#endif


BTree.c

#include"BTree.h"

//申请一个节点
{
BTreeNode *NewNode = (BTreeNode *)malloc(sizeof(BTreeNode));
if(NULL == NewNode)
{
assert(0);
}
NewNode->data = data;
NewNode->left = NULL;
NewNode->right = NULL;
return NewNode;
}
//构建树
void CreatBTree(BTreeNode **cur, DataType *str, DataType invalid, int *index)
{
BTreeNode *ret = NULL;
assert(cur&&str);
if(str[*index] && invalid != str[*index])
{
(*index)++;
CreatBTree(&(*cur)->left, str, invalid , index);
if(str[*index] != '\0')
{
(*index)++;
CreatBTree(&(*cur)->right, str, invalid , index);
}
}
}
//拷贝树
void CpBTree(BTreeNode *cur, BTreeNode **ret)
{
assert(ret);
if(cur)
{
CpBTree(cur->left, &(*ret)->left);
CpBTree(cur->right, &(*ret)->right);
}
}
//前序遍历
void Preorder(BTreeNode *cur)
{
if(cur)
{
printf("%c",cur->data);
Preorder(cur->left);
Preorder(cur->right);
}
}
//中序
void Inorder(BTreeNode *cur)
{
if(cur)
{
Inorder(cur->left);
printf("%c",cur->data);
Inorder(cur->right);
}
}
//后序
void Postorder(BTreeNode *cur)
{
if(cur)
{
Postorder(cur->left);
Postorder(cur->right);
printf("%c",cur->data);
}
}
//销毁
void Destroy(BTreeNode **cur)
{
BTreeNode *ret = *cur;
assert(cur);
if(*cur)
{
Destroy(&ret->left);
Destroy(&ret->right);
free(ret);
ret = NULL;
}
}
//求树的节点个数
int BTreeNum(BTreeNode *cur)
{
if(cur == NULL)
return 0;
return BTreeNum(cur->left) + BTreeNum(cur->right) + 1;
}
//求树的高度
int BTreeHight(BTreeNode *cur)
{
int lefthight = 0;
int righthight = 0;
if(NULL == cur)
return 0;
lefthight = BTreeHight(cur->left);
righthight = BTreeHight(cur->right);

return lefthight > righthight ? lefthight+1 : righthight+1;
}
//叶子节点
int BTree(BTreeNode *cur)
{
if(NULL == cur)
return 0;
if(NULL == cur->left && NULL == cur->right)
return 1;
return BTree(cur->left)+BTree(cur->right);
}
//求第k层的节点
int BTreeKnum(BTreeNode *cur, int K)
{
if(NULL == cur)
return 0;
if(K == 1)
return 1;
return BTreeKnum(cur->left,K-1) + BTreeKnum(cur->right,K-1);
}
//求一个节点的左孩子
BTreeNode *LeftNode(BTreeNode *cur, DataType  data)
{
BTreeNode *ret = NULL;
if(NULL == cur)
return NULL;
if(cur->data == data && cur->left != NULL)
{
return cur->left;
}
ret = LeftNode(cur->left, data);
//如果左子数找到的话，就不用进右子树找了
//如果左子数找不到的话，进入右子树找
if(ret == NULL)
ret =  LeftNode(cur->right, data);
return ret;
}
//求一个节点的右孩子
BTreeNode *RightNode(BTreeNode *cur, DataType  data)
{
BTreeNode *ret = NULL;
if(NULL == cur)
return NULL;
if(cur->data == data && cur->right != NULL)
{
return cur->right;
}
ret = RightNode(cur->left, data);
//如果左子数找到的话，就不用进右子树找了
//如果左子数找不到的话，进入右子树找
if(ret == NULL)
ret =  RightNode(cur->right, data);
return ret;
}
//求一个节点的双亲节点
BTreeNode* ParentNode(BTreeNode *cur, DataType  data)
{
BTreeNode *ret = NULL;
if(cur->data == data)
return NULL;
if(cur->left->data == data|| cur->right->data == data)
return cur;
ret = ParentNode(cur->left, data);
//如果左子数找到的话，就不用进右子树找了
//如果左子数找不到的话，进入右子树找
if(ret == NULL)
ret =  ParentNode(cur->right, data);
return ret;
}