typedef struct Node
{
Node* lchild;
Node* rchild;
int data;
} Node;
int InsertTreeNode(int data, Node** root)//非递归式创建二叉排序树
{
if (*root == NULL)
{
*root = (Node *)malloc(sizeof(Node));
(*root)->data = data;
(*root)->lchild = NULL;
(*root)->rchild = NULL;
return 0;
}
Node* new_node = (Node *)malloc(sizeof(Node));
new_node->data = data;
new_node->lchild = NULL;
new_node->rchild = NULL;
Node* head = *root;
int flag = 1;
Node* head_father = NULL;
while (head)
{
head_father = head;
if (data < head->data)//大于左孩子
{
head = head->lchild;
flag = 0;
}
else if (data > head->data)
{
flag = 1;
head = head->rchild;
}
else
{
printf("data = %d has exist:", data);
return -1;
}
}
if (flag)
{
head_father->rchild = new_node;
}
else
{
head_father->lchild = new_node;
}
return 1;
}
void MidOrderTree(Node* root)
{
if (root)
{
MidOrderTree(root->lchild);
printf("%d ", root->data);
MidOrderTree(root->rchild);
}
}
int search(Node* root, Node* last_father, Node** last_node, int data)
{
Node* head = root;
if (head == NULL)
{
*last_node = last_father;
printf("not found\n");
return -1;
}
if (data == head->data)
{
printf("found\n");
*last_node = root;
return 1;
}
else if (data > head->data)
{
search(head->rchild, head, last_node, data);
}
else
{
search(head->lchild, head, last_node, data);
}
}
void InsertOneNode(Node* root, int data)
{
Node* last_node = NULL;
if (search(root, NULL, &last_node, data) == -1)//
{
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->lchild = NULL;
new_node->rchild = NULL;
new_node->data = data;
if (last_node->data > data)
{
last_node->lchild = new_node;
}
else
{
last_node->rchild = new_node;
}
printf("inset num %d\n", data);
}
else
{
printf("num %d exist\n", data);
}
}
void DeleteData(Node** find_ptr, int data)
{
//如果只有单边子树
if ((*find_ptr)->lchild == NULL)
{
Node* tmp = (*find_ptr);
(*find_ptr) = (*find_ptr)->rchild;//用指针的指针 右子树
free(tmp);
return;
}
if ((*find_ptr)->rchild == NULL)
{
Node* tmp = (*find_ptr);
(*find_ptr) = (*find_ptr)->lchild;//左子树
free(tmp);
return;
}
//如果存在双子节点
//找到待删除节点的前驱(中序遍历中)节点(左子树的最右边)
Node* s = (*find_ptr)->lchild;
Node* s_father = (*find_ptr);
while (s->rchild)
{
s_father = s;
s = s->rchild;
}
if (s_father != (*find_ptr))//说明s的初始位置存在右边节点
{
(*find_ptr)->data = s->data;//替换
s_father->rchild = s->lchild;
free(s);
}
else
{ //替换
(*find_ptr)->data = s->data;
s_father->lchild = s->lchild;
free(s);
}
}
void DeleteNode(Node** root, int data)//此处为什么用指针的指针
{
//这个节点是叶子节点
//这个节点是左子树或者右子树
//这个节点包含左右节点
if (*root == NULL)
{
printf("data = %d not exist", data);
return;
}
if (data == (*root)->data)
{
printf("del num = %d\n", (*root)->data);
DeleteData(root, data);//用上指针的指针,里面修改指针
}
else if (data > (*root)->data)
{
DeleteNode(&(*root)->rchild, data);
}
else
{
DeleteNode(&(*root)->lchild, data);
}
}
//递归式创建二叉排序树
int data[10] = { 1, 3, 7, 9, 8, 4, 2, 5, 6, 10 };
Node* ptr = NULL;
for (int i = 0; i < 10; i++)
{
InsertTreeNode(data[i], &ptr);
}
MidOrderTree(ptr);
Node *last_node = NULL;
search(ptr, NULL, &last_node, -1);
InsertOneNode(ptr, 12);
MidOrderTree(ptr);
DeleteNode(&ptr, 12);//为什么用指针的指针
MidOrderTree(ptr);