二叉排序树(Binary Sort Tree)中,任意结点x,其左子树所有结点的值不大于key[x] ,其右子树的所有结点值不小于key[x]。BST中的操作SEARCH、MINIMUM、MAXIMUM、SUCCESSOR、 PREDECCESSOR、INSERT、DELETE都可以在O(h)内完成,h为树的高度。BST可以用作字典,也可以用作优先队列。
BST的C语言实现和操作实例如下:
#include "stdio.h"
#include "stdlib.h"
/* struct of binary tree node */
struct Node {
int key;
struct Node *parent;
struct Node *left;
struct Node *right;
};
/* print binary sort tree by inorder*/
void PrintTree(struct Node *Root)
{
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
pointer=Root;
if(pointer!=NULL) {
PrintTree(pointer->left);
printf("%d ",pointer->key);
PrintTree(pointer->right);
}
}
/* destroy binary sort tree */
void DeleteTree(struct Node *Root)
{
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
pointer=Root;
if(pointer!=NULL) {
DeleteTree(pointer->left);
free(pointer);
DeleteTree(pointer->right);
}
}
/* Minimum */
struct Node *Minimum(struct Node *Root)
{
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
pointer=Root;
while(pointer->left!=NULL)
pointer=pointer->left;
return pointer;
}
/* Maximum */
struct Node *Maximum(struct Node *Root)
{
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
pointer=Root;
while(pointer->right!=NULL)
pointer=pointer->right;
return pointer;
}
/* tree search */
struct Node *Search(struct Node *Root,int value)
{
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
pointer=Root;
while(pointer!=NULL&&pointer->key!=value) {
if(value<pointer->key)
pointer=pointer->left;
else
pointer=pointer->right;
}
return pointer;
}
/* Successor */
struct Node *Successor(struct Node *pointer)
{
if(pointer->right!=NULL)
return Minimum(pointer->right);
struct Node *y=(struct Node*)malloc(sizeof(struct Node));
y=pointer->parent;
while(y!=NULL&&y->right==pointer) {
pointer=y;
y=pointer->parent;
}
return y;
}
/* Predecessor */
struct Node *Predecessor(struct Node *pointer)
{
if(pointer->left!=NULL)
return Maximum(pointer->left);
struct Node *y=(struct Node*)malloc(sizeof(struct Node));
y=pointer->parent;
while(y!=NULL&&y->left==pointer) {
pointer=y;
y=pointer->parent;
}
return y;
}
/* Insert */
struct Node *Insert(struct Node *Root,struct Node *newNode)
{
struct Node *back=(struct Node*)malloc(sizeof(struct Node));
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
back=NULL;
pointer=Root;
/* find the location to insert */
while(pointer!=NULL) {
back=pointer;
if(newNode->key<pointer->key)
pointer=pointer->left;
else
pointer=pointer->right;
}
newNode->parent=back;
if(back==NULL)
Root=newNode;
else {
if(newNode->key<back->key)
back->left=newNode;
else
back->right=newNode;
}
return Root;
}
/* Delete */
struct Node *Delete(struct Node *Root,struct Node *pointer)
{
struct Node *y=(struct Node*)malloc(sizeof(struct Node));
y->parent=NULL;
y->left=NULL;
y->right=NULL;
/* find the real node to delete */
if(pointer->left==NULL||pointer->right==NULL)
y=pointer;
else
y=Successor(pointer);
struct Node *x=(struct Node*)malloc(sizeof(struct Node));
x->parent=NULL;
x->left=NULL;
x->right=NULL;
if(y->left!=NULL)
x=y->left;
else
x=y->right;
if(x!=NULL)
x->parent=y->parent;
if(y->parent==NULL)
Root=x;
else {
if(y==y->parent->left)
y->parent->left=x;
else
y->parent->right=x;
}
int key = pointer->key;
if(y!=pointer)
pointer->key=y->key;
if(y!=NULL) {
free(y);
}
return Root;
}
int main(int argc, char *argv[])
{
int a[10] = {45, 24, 55, 12, 37, 53, 60, 28, 40, 70};
struct Node *Root = NULL;
int i;
for (i=0; i<10; ++i) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->key = a[i];
newNode->parent = NULL;
newNode->left = NULL;
newNode->right = NULL;
Root = Insert(Root,newNode);
}
PrintTree(Root);
/* transfer BST to the RIGHT BST (every node only have right child) */
struct Node *p, *trans_bst = NULL;
p = Minimum(Root);
while(p) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->key = p->key;
newNode->parent = NULL;
newNode->left = NULL;
newNode->right = NULL;
trans_bst = Insert(trans_bst, newNode);
p = Successor(p);
}
p = trans_bst;
while(p) {
printf("%4d", p->key);
p = p->right;
}
DeleteTree(Root);
DeleteTree(trans_bst);
return 0;
}
(刘爱贵 / Aiguille LIU)