search_tree.h
//实现二叉搜索树的递归和非递归版本
#pragma once
#include<stddef.h>
typedef char SearchTreeType;
typedef struct SearchTreeNode {
SearchTreeType key; // 关键码
struct SearchTreeNode* lchild;
struct SearchTreeNode* rchild;
} SearchTreeNode;
void SearchTreeInit(SearchTreeNode** Root);
void SearchTreeInsert(SearchTreeNode** Root, SearchTreeType Key);
SearchTreeNode* SearchTreeFind(SearchTreeNode* root,SearchTreeType to_find);
void SearchTreeRemove(SearchTreeNode** Root, SearchTreeType Key);
void SearchTreeInsertByLoop(SearchTreeNode** Root,SearchTreeType Key);
SearchTreeNode* SearchTreeFindByLoop(SearchTreeNode* root,SearchTreeType to_find);
void SearchTreeRemoveByLoop(SearchTreeNode** Root,SearchTreeType Key);
search_tree.c
//实现二叉搜索树的递归和非递归版本
//typedef char SearchTreeType;
//
//typedef struct SearchTreeNode {
//SearchTreeType key; // 关键码
//struct SearchTreeNode* lchild;
//struct SearchTreeNode* rchild;
//} SearchTreeNode;
#include<stdio.h>
#include<stdlib.h>
#include"search_tree.h"
#define TEST_HEADER printf("\n=============%s==============\n",__FUNCTION__)
void SearchTreeInit(SearchTreeNode** Root)
{
if(Root==NULL)
{
return;
}
*Root=NULL;
}
SearchTreeNode* CreatSearchNode(SearchTreeType Key)
{
SearchTreeNode* tmp=(SearchTreeNode*)malloc(sizeof(SearchTreeNode));
tmp->key=Key;
tmp->lchild=NULL;
tmp->rchild=NULL;
return tmp;
}
void SearchTreeInsert(SearchTreeNode** Root, SearchTreeType Key)
{
if(Root==NULL)
{
return;
}
if(*Root==NULL)
{
*Root=CreatSearchNode(Key);
return;
}
SearchTreeNode* root=*Root;
if(root->key > Key)
{
SearchTreeInsert(&root->lchild,Key);
}
else if(root->key < Key)
{
SearchTreeInsert(&root->rchild,Key);
}
else
{
return;
}
}
SearchTreeNode* SearchTreeFind(SearchTreeNode* root, SearchTreeType to_find)
{
if(root==NULL)
{
return NULL;
}
if(root->key > to_find)
{
return SearchTreeFind(root->lchild,to_find);
}
else if(root->key < to_find)
{
return SearchTreeFind(root->rchild,to_find);
}
else
{
return root;
}
}
void SearchTreeRemove(SearchTreeNode** Root, SearchTreeType Key)
{
if(Root==NULL)
{
return;
}
if(*Root==NULL)
{
return;
}
if((*Root)->key > Key)
{
SearchTreeRemove(&(*Root)->lchild,Key);
}
else if((*Root)->key < Key)
{
SearchTreeRemove(&(*Root)->rchild,Key);
}else
{
SearchTreeNode* root=*Root;
SearchTreeNode* to_delete=NULL;
if(root->lchild==NULL && root->rchild==NULL)
{
free(*Root);
*Root=NULL;
}
else if(root->lchild!=NULL && root->rchild==NULL)
{
*Root=root->lchild;
free(root);
}
else if(root->lchild==NULL && root->rchild!=NULL)
{
*Root=root->rchild;
free(root);
}
else
{
//choose lchild rightmost value
root=root->lchild;
while(root->rchild!=NULL)
{
root=root->rchild;
}
(*Root)->key=root->key;
SearchTreeRemove(&(*Root)->lchild,root->key);
}
}
}
void PreOder(SearchTreeNode* root)
{
if(root==NULL)
{
return;
}
printf("%c",root->key);
PreOder(root->lchild);
PreOder(root->rchild);
}
void InOder(SearchTreeNode* root)
{
if(root==NULL)
{
return;
}
InOder(root->lchild);
printf("%c",root->key);
InOder(root->rchild);
}
void Test()
{
TEST_HEADER;
SearchTreeNode* searchtree;
SearchTreeInit(&searchtree);
SearchTreeInsert(&searchtree,'b');
SearchTreeInsert(&searchtree,'e');
SearchTreeInsert(&searchtree,'c');
SearchTreeInsert(&searchtree,'a');
SearchTreeInsert(&searchtree,'f');
SearchTreeInsert(&searchtree,'d');
printf("==After insert==\n");
printf("preoder\n");
PreOder(searchtree);
printf("\n");
printf("inoder\n");
InOder(searchtree);
printf("\n");
SearchTreeNode*find= SearchTreeFind(searchtree,'e');
printf("The find result is %c \n",find->key);
find= SearchTreeFind(searchtree,'g');
printf("The find is %p \n",find);
printf("%c \n",searchtree->key);
SearchTreeRemove(&searchtree,'b');
SearchTreeRemove(&searchtree,'e');
SearchTreeRemove(&searchtree,'d');
printf("==After remove==\n");
printf("preoder\n");
PreOder(searchtree);
printf("\n");
printf("inoder\n");
InOder(searchtree);
printf("\n");
}
void SearchTreeInsertByLoop(SearchTreeNode** Root,SearchTreeType Key)
{
if(Root==NULL)
{
return;
}
if(*Root==NULL)
{
*Root=CreatSearchNode(Key);
return;
}
SearchTreeNode* root= *Root;
SearchTreeNode* pre=NULL;
while(1)
{
if(root==NULL)
{
if(pre->key > Key)
{
pre->lchild=CreatSearchNode(Key);
}
else
{
pre->rchild=CreatSearchNode(Key);
}
return;
}
if(root->key > Key)
{
pre=root;
root=root->lchild;
}
else if(root->key < Key)
{
pre=root;
root=root->rchild;
}
else
{
return;
}
}
}
SearchTreeNode* SearchTreeFindByLoop(SearchTreeNode* root,SearchTreeType to_find)
{
if(root==NULL)
{
return NULL;
}
while(1)
{
if(root==NULL)
{
return NULL;
}
if(root->key > to_find)
{
root=root->lchild;
}
else if(root->key < to_find)
{
root=root->rchild;
}
else
{
return root;
}
}
}
void DestoryNode(SearchTreeNode* to_destory)
{
free(to_destory);
return;
}
void SearchTreeRemoveByLoop(SearchTreeNode** Root,SearchTreeType Key)
{
if(Root==NULL)
{
return;
}
if(*Root==NULL)
{
return;
}
SearchTreeNode* pre=NULL;
SearchTreeNode* root=*Root;
while(1)
{
if(root==NULL)
{
return;
}
if(root->key > Key)
{
pre=root;
root=root->lchild;
}
else if(root->key < Key)
{
pre=root;
root=root->rchild;
}
else
{
SearchTreeNode* to_delete=root;
if(root->lchild==NULL && root->rchild==NULL)
{
if(pre==NULL)
{
*Root=NULL;
DestoryNode(to_delete);
return;
}
if(pre->key > Key)
{
pre->lchild=NULL;
}
else
{
pre->rchild=NULL;
}
}
else if(root->lchild!=NULL && root->rchild==NULL)
{
if(pre==NULL)
{
*Root=root->lchild;
DestoryNode(to_delete);
return;
}
if(pre->key > Key)
{
pre->lchild=root->lchild;
}
else
{
pre->rchild=root->lchild;
}
}
else if(root->lchild==NULL && root->rchild!=NULL)
{
if(pre==NULL)
{
*Root=root->rchild;
DestoryNode(to_delete);
return;
}
if(pre->key > Key)
{
pre->lchild=root->rchild;
}
else
{
pre->rchild=root->rchild;
}
}
else
{
// left tree rightmost to change.
pre=root;
root=root->lchild;
while(root->rchild!=NULL)
{
pre=root;
root=root->rchild;
}
to_delete->key=root->key;
to_delete=root;
if(root->lchild==NULL)
{
pre->rchild=NULL;
}
else
{
pre->rchild=root->lchild;
}
}
DestoryNode(to_delete);
return;
}
}
}
void PrintChar(const char* str,SearchTreeNode* root)
{
printf("%s \n",str);
PreOder(root);
printf("\n");
InOder(root);
printf("\n");
}
void TestLoop()
{
TEST_HEADER;
SearchTreeNode * root;
SearchTreeInit(&root);
SearchTreeInsert(&root,'e');
SearchTreeInsert(&root,'a');
SearchTreeInsert(&root,'f');
SearchTreeInsert(&root,'b');
SearchTreeInsert(&root,'d');
SearchTreeInsert(&root,'c');
PreOder(root);
printf("\n");
InOder(root);
printf("\n");
SearchTreeNode* find=SearchTreeFindByLoop(root,'b');
printf("The find result is b actual: %c \n",find->key);
SearchTreeRemoveByLoop(&root,'b');
PrintChar("remove one value",root);
SearchTreeRemoveByLoop(&root,'f');
PrintChar("remove two value",root);
SearchTreeRemoveByLoop(&root,'d');
PrintChar("remove three value",root);
SearchTreeRemoveByLoop(&root,'c');
PrintChar("remove four value",root);
SearchTreeRemoveByLoop(&root,'e');
PrintChar("remove five value",root);
}
int main()
{
Test();
TestLoop();
return 0;
}
运行结果
=============Test==============
==After insert==
preoder
baecdf
inoder
abcdef
The find result is e
The find is (nil)
b
==After remove==
preoder
acf
inoder
acf
=============TestLoop==============
eabdcf
abcdef
The find result is b actual: b
remove one value
eadcf
acdef
remove two value
eadc
acde
remove three value
eac
ace
remove four value
ea
ae
remove five value
a
a