二叉排序樹(Binary Sort Tree)又稱二叉查找樹,亦稱二叉搜索樹。 它或者是一棵空樹;或者是具有下列性質的二叉樹: (1)若左子樹不空,則左子樹上所有結點的值均小於它的根結點的值; (2)若右子樹不空,則右子樹上所有結點的值均大於它的根結點的值; (3)左、右子樹也分別爲二叉排序樹;
我們假設用二叉鏈表進行存儲;二叉鏈表結點。有三個域:一個數據域,兩個分別指向左右子結點的指針域。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 9
typedef int ElemType;
typedef struct BSTNode
{
ElemType data ;
struct BSTNode *Lchild, *Rchild;
}BSTNode;
//查找關鍵字key,迭代版本
BSTNode* SearchBST(BSTNode* &T, ElemType key)
{
BSTNode* p=T;
while( p )
{
if( key == p->data) return p; //查找成功,返回p
if( key < p->data)
p=p->Lchild; //在左子樹中繼續查找
else
p=p->Rchild; //在右子樹中繼續查找
}
return NULL; //T爲空
}
//查找關鍵字key,遞歸版本
BSTNode* Recur_SearchBST(BSTNode* &T, ElemType key)
{
if (NULL==T || key==T->data)
return T;
if ( key< T->data)
return Recur_SearchBST(T->Lchild,key);
else
return Recur_SearchBST(T->Rchild,key);
}
//插入關鍵字key
void InsertBST(BSTNode* &T,ElemType key)
{
if(T==NULL)
{
T=(BSTNode*)malloc(sizeof(BSTNode));
T->Lchild=T->Rchild=NULL;
T->data=key;
return;
}
if(key < T->data )
InsertBST(T->Lchild,key);
else
InsertBST (T->Rchild, key );
}
//n個數據在數組d中,tree爲二叉排序樹根
void CreateBiTree(BSTNode* &T,ElemType d[ ] ,int n)
{
T=NULL;
for(int i=0;i<n;i++)
InsertBST(T,d[i]);
}
BSTNode* DeleteNode(BSTNode* &T)
{
BSTNode* p=T;
if (p->Lchild)
{
BSTNode* L = p->Lchild; //L指向其左子樹;
BSTNode* prer = p->Lchild; //prer指向其左子樹;
while(L->Rchild != NULL)//搜索左子樹的最右邊的葉子結點L
{
prer = L;
L = L->Rchild;
}
p->data = L->data;
if(prer != L)//若L不是p的左孩子,把L的左孩子作爲L的父親的右孩子
prer->Rchild = L->Lchild;
else
p->Lchild = L->Lchild; //否則結點p的左子樹指向L的左子樹
free(L);
return p;
}
else
{
BSTNode *q = p->Rchild; //q指向其右子樹;
free(p);
return q;
}
}
int DeleteBST(BSTNode* &T,ElemType key)
{
//若二叉排序樹T中存在關鍵字等於key的數據元素時,則刪除該數據元素,並返回TRUE;否則返回FALSE
if(!T) return -1; //不存在關鍵字等於key的數據元素
else
{
if(key == T->data) // 找到關鍵字等於key的數據元素
{
T = DeleteNode(T);
return 1;
}
else if(key < T->data)
return DeleteBST(T->Lchild, key);
else
return DeleteBST(T->Rchild, key);
}
}
void InorderTraverse(BSTNode* &T) //中序遍歷
{
if (T!=NULL)
{
InorderTraverse(T->Lchild) ;
printf("%d ",T->data);
InorderTraverse(T->Rchild) ;
}
}
int main()
{
int arr[N] = {75, 95, 15, 78, 84, 51, 24, 120,24};
BSTNode* head=NULL;
CreateBiTree(head,arr,N);
printf("%d\n",head->data);
InorderTraverse(head); printf("\n");
InsertBST(head,63);
InsertBST(head,54);
InorderTraverse(head); printf("\n");
printf("%d\n",SearchBST(head,78));
printf("%d\n",SearchBST(head,55));
DeleteBST(head,63);
InorderTraverse(head);
printf("\n");
DeleteBST(head,95);
InorderTraverse(head);
printf("\n");
DeleteBST(head,75);
InorderTraverse(head);
printf("\n");
free(head);
return 0;
}
用三叉鏈表存儲:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
typedef int ElemType;
typedef struct BSTNode
{
ElemType data ;
struct BSTNode *Lchild, *Rchild, *Parent;
}BSTNode;
void InorderTraverse(BSTNode* &T) //中序遍歷
{
if (T!=NULL)
{
InorderTraverse(T->Lchild) ;
printf("%d ",T->data);
InorderTraverse(T->Rchild) ;
}
}
//查找關鍵字key,迭代版本
BSTNode* SearchBST(BSTNode* &T, ElemType key)
{
BSTNode* p=T;
while( p )
{
if( key == p->data) return p; //查找成功,返回p
if( key < p->data)
p=p->Lchild; //在左子樹中繼續查找
else
p=p->Rchild; //在右子樹中繼續查找
}
return NULL; //T爲空
}
//查找關鍵字key,遞歸版本
BSTNode* Recur_SearchBST(BSTNode* &T, ElemType key)
{
if (NULL==T || key==T->data)
return T;
if ( key< T->data)
return Recur_SearchBST(T->Lchild,key);
else
return Recur_SearchBST(T->Rchild,key);
}
//插入關鍵字key
void InsertBST(BSTNode* &T,ElemType key)
{
BSTNode* p= T;
BSTNode* father=NULL;
while( p!=NULL )
{
if (p->data==key) return; //防止插入重複鍵值
father=p;
p=(key < p->data)? p->Lchild :p->Rchild;
}
p=(BSTNode*)malloc(sizeof(BSTNode));
p->Lchild=p->Rchild=NULL;
p->Parent=father;
p->data=key;
if (T==NULL)
T=p;
else
{
if (key < father->data)
father->Lchild=p;
else
father->Rchild=p;
}
}
//n個數據在數組d中,tree爲二叉排序樹根
void CreateBiTree(BSTNode* &T,ElemType d[] ,int n)
{
T=NULL;
for(int i=0;i<n;i++)
{
InsertBST(T,d[i]);
}
}
BSTNode* DeleteNode(BSTNode* &T)
{
BSTNode* p=T;
if (p->Lchild)
{
BSTNode* L = p->Lchild; //L指向其左子樹;
BSTNode* prer = p->Lchild; //prer指向其左子樹;
while(L->Rchild != NULL)//搜索左子樹的最右邊的葉子結點L
{
prer = L;
L = L->Rchild;
}
p->data = L->data;
if(prer != L)//若L不是p的左孩子,把L的左孩子作爲L的父親的右孩子
prer->Rchild = L->Lchild;
else
p->Lchild = L->Lchild; //否則結點p的左子樹指向L的左子樹
free(L);
return p;
}
else
{
BSTNode *q = p->Rchild; //q指向其右子樹;
free(p);
return q;
}
}
int DeleteBST(BSTNode* &T,ElemType key)
{
//若二叉排序樹T中存在關鍵字等於key的數據元素時,則刪除該數據元素,並返回TRUE;否則返回FALSE
if(!T) return -1; //不存在關鍵字等於key的數據元素
else
{
if(key == T->data) // 找到關鍵字等於key的數據元素
{
T = DeleteNode(T);
return 1;
}
else if(key < T->data)
return DeleteBST(T->Lchild, key);
else
return DeleteBST(T->Rchild, key);
}
}
void PrintRouth(BSTNode* &T, ElemType key)
{
BSTNode* q=SearchBST(T,key);
while(q)
{
printf("%d ",q->data);
q=q->Parent;
}
printf("\n");
}
int main()
{
int arr[N] = {75, 95, 15, 78, 84, 51, 24, 120,25,24};
BSTNode* head=NULL;
CreateBiTree(head,arr,N);
//printf("%d\n",head->data);
InorderTraverse(head); printf("\n");
PrintRouth(head,24);
PrintRouth(head,84);
InsertBST(head,63);
InsertBST(head,54);
InorderTraverse(head); printf("\n");
printf("%d\n",SearchBST(head,78));
printf("%d\n",SearchBST(head,55));
DeleteBST(head,63);
InorderTraverse(head);
printf("\n");
DeleteBST(head,95);
InorderTraverse(head);
printf("\n");
DeleteBST(head,75);
InorderTraverse(head);
printf("\n");
free(head);
return 0;
}