基本概念就不说了
直接从BST(二叉查找树)开始
//二叉树节点
typedef struct node {
int data; //节点数据部分可为任意内容
struct node * parent; //父节点
struct node * left; //左孩子
struct node * right; //右孩子
} node;
//二叉树
typedef struct binary_tree{
node ** root; //在删除和平衡二叉树的过程中,根节点会随时变化
unsigned level; //二叉树层数(暂未启用)
unsigned max_num; //最大节点容量(暂未启用)
unsigned cur_num; //现有节点数(暂未启用)
} binary_tree;
相关函数
//创建BST节点
static node * creat_node(int data)
{
node * n = malloc(sizeof(node));
if (n != NULL) {
n->data = data;
n->parent = NULL;
n->left = NULL;
n->right = NULL;
}
return n;
}
//判断n节点是否为p的右孩子
int is_right_child(node * n, node * p)
{
return p->right == n;
}
//判断n节点是否为p的左孩子
int is_left_child(node * n, node * p)
{
return p->left == n;
}
//判断p节点是否为n的父节点
int is_parent(node * n, node * p)
{
return n->parent == p;
}
//寻找最大的节点
node * find_max_node(node * root)
{
if (root != NULL) {
while(root->right != NULL)
root = root->right;
}
return root;
}
//寻找最小的节点
node * find_min_node(node * root)
{
if (root != NULL) {
while(root->left != NULL)
root = root->left;
}
return root;
}
//某子树的深度
static int depth_node(node * root)
{
int ld = 0;
int rd = 0;
if (root == NULL) {
return 0;
} else {
ld = depth_node(root->left);
rd = depth_node(root->right);
return 1 + (ld > rd ? ld : rd);
}
}
//某节点的平衡因子
int bf_node(node * root)
{
int ld = 0;
int rd = 0;
if (root->left == NULL)
ld = 0;
else
ld = depth_node(root->left);
if (root->right == NULL)
rd = 0;
else
rd = depth_node(root->right);
return ld - rd;
}
//创建空二叉树
static binary_tree * creat_binary_tree(void)
{
binary_tree * bitree = malloc(sizeof(binary_tree));
if (bitree != NULL) {
bitree->root = malloc(sizeof(node *));
bitree->level = 0;
bitree->cur_num = 0;
bitree->max_num = 0;
}
return bitree;
}
//判断某子树是否平衡
int is_balanced_tree(node * root)
{
int ld = 0;
int rd = 0;
int difference = 0;
ld = depth_node(root->left);
rd = depth_node(root->right);
difference = ld - rd;
if (difference == 0 || difference == 1 || difference == -1) {
return BALANCED;
}
return IMBALANCED;
}
//递归查找节点
node * search_node(node * root, int data)
{
while (root != NULL && root->data != data) {
if (data < root->data)
root = root->left;
else
root = root->right;
}
return root;
}
//非递归查找节点
node * search_node(node * root, int data)
{
if (root == NULL || root->data == data) {
return root;
} else {
if (data < root->data) {
return search_node(root->left, data);
} else {
return search_node(root->right, data);
}
}
}
//计算子树节点数
int count_nodes(node * root)
{
int num = 0;
if (root == NULL) {
num = 0;
} else {
num = count_nodes(root->left) + count_nodes(root->right) + 1;
}
return num;
}
//前序遍历
void preorder(node * r)
{
show_data(r);
if (r->left != NULL)
preorder(r->left);
if (r->right != NULL)
preorder(r->right);
}
//中序遍历
void inorder(node * r)
{
if (r->left != NULL)
inorder(r->left);
show_data(r);
if (r->right != NULL)
inorder(r->right);
}
//后序遍历
void postorder(node * r)
{
if (r->left != NULL)
postorder(r->left);
if (r->right != NULL)
postorder(r->right);
show_data(r);
}
//在保持二叉查找树性质不变的情况下,直接插入新的节点
node * direct_add_node(node ** r, int value)
{
node * root = *r;
if (root == NULL) {
root = creat_node(value);
*r = root;
return root;
}
node * new_node = NULL;
if (compare(value, root->data) == LESS) {
if (root->left != NULL) {
return direct_add_node(&(root->left), value);
} else {
new_node = add_left_child(root, value);
return new_node;
}
} else {
if (root->right != NULL) {
return direct_add_node(&(root->right), value);
} else {
new_node = add_right_child(root, value);
return new_node;
}
}
}