二叉查找树或者是一棵空树,或者是具有下列性质的二叉树:
1、每个结点都有一个作为查找依据的关键码,所有结点的关键码互不相同。
2、左子树(如果存在)上所有结点的关键码都小于根结点的关键码。
3、右子树(如果存在)上所有结点的关键码都大于根结点的关键码。
4、左子树和右子树也是二叉查找树。
BSTreeMain.c
#include <stdio.h>
#include <stdlib.h>
#include "bstree.h"
int main(int argc, char **argv)
...{
int i;
ElemType n, a[10]=...{4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
BSTree tmp, head;
head = NULL;
tmp = NULL;
for(i = 0; i < 10; i++)
TreeInsert(&head, TreeNodeCreate(a[i]));
InOrderTreeWalk(head);
printf(" ");
while(scanf("%d",&n)==1 && n)
...{
tmp = TreeSearch(head, n);
PrintNodeKey(tmp);
if(tmp != NULL)
...{
TreeDelete(&head, tmp);
}
InOrderTreeWalk(head);
printf(" ");
}
return 0;
}bstree.h
#ifndef _BSTREE_H
#define _BSTREE_H
typedef int ElemType;
typedef struct BSTNode...{
ElemType key;
struct BSTNode *left;
struct BSTNode *right;
struct BSTNode *p;
}BSTNode, *BSTree;
BSTree TreeNodeCreate(ElemType k);
void InOrderTreeWalk(BSTree t);
BSTree TreeSearch(BSTree t, ElemType k);
BSTree TreeMinimum(BSTree t);
BSTree TreeMaximum(BSTree t);
BSTree TreeRredecessor(BSTree t);
BSTree TreeSuccessor(BSTree t);
int TreeInsert(BSTree *t, BSTree z);
int TreeDelete(BSTree *t, BSTree z);
void PrintNodeKey(BSTree bst);
#endifbstree.c
#include <stdio.h>
#include <stdlib.h>
#include "bstree.h"
void InOrderTreeWalk(BSTree t)
...{
if(t != NULL)
...{
InOrderTreeWalk(t->left);
printf("%d ", t->key);
InOrderTreeWalk(t->right);
}
}
/**//*
*递归版本
*/
BSTree TreeSearch(BSTree t, ElemType k)
...{
if(t == NULL || k == t->key)
return t;
if(k < t->key)
return TreeSearch(t->left, k);
else
return TreeSearch(t->right, k);
}
/**//*
*非递归版本
*/
BSTree IterativeTreeSearch(BSTree t, ElemType k)
...{
while(t != NULL && k != t->key)
...{
if(k < t->key)
t = t->left;
else
t = t->right;
}
return t;
}
BSTree TreeMinimum(BSTree t)
...{
while(t->left != NULL)
t = t->left;

return t;
}
BSTree TreeMaximum(BSTree t)
...{
while(t->right != NULL)
t = t->right;

return t;
}
BSTree TreeSuccessor(BSTree t)
...{
BSTree y;
if(t->right != NULL)
return TreeMinimum(t->right);
y = t->p;
while(y != NULL && t == y->right)
...{
t = y;
y = y->p;
}
return y;
}
BSTree TreeRredecessor(BSTree t)
...{
BSTree y;
if(t->left != NULL)
return TreeMaximum(t->left);
y = t->p;
while(y != NULL && t == y->left)
...{
t = y;
y = y->p;
}
return y;
}
int TreeInsert(BSTree *t, BSTree z)
...{
BSTree x, y;
y = NULL;
x = *t;
while(x != NULL)
...{
y = x;
if(z->key < x->key)
x = x->left;
else
x = x->right;
}
z->p = y;
if(y == NULL)
*t = z;
else if(z->key < y->key)
y->left = z;
else
y->right = z;
return 1;
}
int TreeDelete(BSTree *t, BSTree z)
...{
BSTree x, y;
if(z->left == NULL || z->right == NULL)
y = z;
else
y = TreeSuccessor(&(**t));
if(y->left != NULL)
x = y->left;
else
x = y->right;
if(x != NULL)
x ->p = y->p;
if(y->p == NULL)
(*t) = x;
else if(y == y->p->left)
y->p->left = x;
else
y->p->right = x;
if(y != z)
z->key = y->key;
free(y);
y = NULL;
return 1;
}
BSTree TreeNodeCreate(ElemType k)
...{
BSTree tmp;
tmp = (BSTree)malloc(sizeof(BSTNode));
tmp->key = k;
tmp->left = NULL;
tmp->p = NULL;
tmp->right = NULL;
return tmp;
}
void PrintNodeKey(BSTree bst)
...{
if(bst == NULL)
printf("It's a NULL node! ");
else
printf("%d ", bst->key);
}
本文介绍了一种二叉查找树的数据结构,并提供了插入、删除、查找等关键操作的具体实现。通过对二叉查找树的定义及其核心算法的解析,帮助读者深入理解二叉查找树的工作原理。
764

被折叠的 条评论
为什么被折叠?



