主函数main.c
#include "search_tree.h"
int main(int argc, char const *argv[])
{
uint keys[] = {10, 9, 4, 6, 14, 15, 20};
pNode root = create_tree(keys, sizeof(keys)/sizeof(uint));
pre_order_display(root);
mid_order_display(root);
back_order_display(root);
insert_node(root, create_node(12));
pre_order_display(root);
mid_order_display(root);
back_order_display(root);
pNode node = search_node(root, 14);
root = delete_node_in_tree(root, node);
pre_order_display(root);
mid_order_display(root);
back_order_display(root);
return 0;
}
search_tree.h
// 查找树 又名 搜索树 排序树
/*
规则:
1、每个节点包含一个查找值key
2、生成树的过程,查找值小于根的查找值的,应当放在左子树,反之,右子树
3、查找树进行中序遍历,便是排序好的结果
*/
#ifndef _SEARCH_TREE_H_
#define _SEARCH_TREE_H_
typedef unsigned int uint;
typedef struct n
{
int data;
uint key;
struct n *left;
struct n *right;
}Node, *pNode, **ppNode;
// 根据一组查找值key生成一颗查找树
pNode create_tree(uint keys[], uint len);
pNode create_node(uint key);
void link_node(pNode cur,pNode parent, uint isleft,pNode left, pNode right);
pNode insert_node(pNode root, pNode node);
pNode delete_node_in_tree(pNode root, pNode node);
pNode search_node(pNode root, uint key);
void pre_order_display(pNode root);
void mid_order_display(pNode root);
void back_order_display(pNode root);
#endif
search_tree.c
#include "search_tree.h"
#include <stdio.h>
#include <stdlib.h>
pNode search_parent(pNode root, pNode node);
pNode create_tree(uint keys[], uint len)
{
if (len == 0)
{
return NULL;
}
pNode root = create_node(keys[0]);
for (int i = 1; i < len; i++)
{
pNode node = create_node(keys[i]);
insert_node(root, node);
}
return root;
}
pNode create_node(uint key)
{
pNode node = (pNode)malloc(sizeof(Node));
node->key = key;
node->data = key;
node->left = NULL;
node->right = NULL;
return node;
}
void link_node(pNode cur,pNode parent, uint isleft,pNode left, pNode right)
{
if (isleft)
{
parent->left = cur;
}else
{
parent->right = cur;
}
cur->left = left;
cur->right = right;
}
pNode insert_node(pNode root, pNode node)
{
if (root == NULL )
{
return NULL;
}
if (node == NULL)
{
return root;
}
if (root->key < node->key)
{
if (root->right == NULL)
{
link_node(node,root,0,node->left,node->right);
}else
{
insert_node(root->right,node);
}
}else
{
if (root->left == NULL)
{
link_node(node,root,1,node->left,node->right);
}else
{
insert_node(root->left,node);
}
}
return root;
}
void delete_node(pNode node)
{
node->left = NULL;
node->right = NULL;
free(node);
node = NULL;
}
pNode delete_node_in_tree(pNode root, pNode node)
{
if (root == NULL || node == NULL)
{
return NULL;
}
if (root == node)
{
if (node->left == NULL && node->right == NULL)
{
root = NULL;
} else if (node->left != NULL && node->right == NULL)
{
root = node->left;
} else if (node->left == NULL && node->right != NULL)
{
root = node->right;
} else
{
root = node->left;
insert_node(root,node->right);
}
}
pNode parent = search_parent(root, node);
if (parent == NULL)
{
return root;
}
if (parent->left == node)
{
if (node->left == NULL && node->right == NULL)
{
parent->left = NULL;
} else if (node->left != NULL && node->right == NULL)
{
parent->left = node->left;
} else if (node->left == NULL && node->right != NULL)
{
parent->left = node->right;
} else
{
parent->left = node->left;
insert_node(root,node->right);
}
} else
{
if (node->left == NULL && node->right == NULL)
{
parent->right = NULL;
} else if (node->left != NULL && node->right == NULL)
{
parent->right = node->left;
} else if (node->left == NULL && node->right != NULL)
{
parent->right = node->right;
} else
{
parent->right = node->left;
insert_node(root, node->right);
}
}
delete_node(node);
node = NULL;
return root;
}
pNode search_node(pNode root, uint key)
{
if (root == NULL)
{
return NULL;
}
if (root->key == key)
{
return root;
}
if (root->key > key)
{
return search_node(root->left, key);
}
if (root->key < key)
{
return search_node(root->right,key);
}
return NULL;
}
pNode search_parent(pNode root, pNode node)
{
if (root == NULL || node == NULL || root == node)
{
return NULL;
}
if (root->left == node || root->right == node)
{
return root;
}
if (node->key > root->key)
{
return search_parent(root->right, node);
} else
{
return search_parent(root->left, node);
}
}
void pre_order(pNode root, void(*func)(pNode node))
{
if (root == NULL)
{
return ;
}
// DLR
func(root);
pre_order(root->left,func);
pre_order(root->right,func);
}
void mid_order(pNode root, void(*func)(pNode node))
{
if (root == NULL)
{
return ;
}
// LDR
mid_order(root->left, func);
func(root);
mid_order(root->right,func);
}
void back_order(pNode root, void(*func)(pNode node))
{
if (root == NULL)
{
return ;
}
// LRD
back_order(root->left,func);
back_order(root->right,func);
func(root);
}
void display_node(pNode node)
{
printf("%d ", node->data);
}
void pre_order_display(pNode root)
{
pre_order(root,display_node);
printf("\n");
}
void mid_order_display(pNode root)
{
mid_order(root,display_node);
printf("\n");
}
void back_order_display(pNode root)
{
back_order(root,display_node);
printf("\n");
}