二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),亦称二叉搜索树。
-
定义
二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:
- 若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值
- 若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值
- 左、右子树也分别为二叉排序树
-
查找
若根结点的关键字值等于查找的关键字,成功,返回ture。
若小于根结点的关键字值,递归查左子树。
若大于根结点的关键字值,递归查右子树。
若子树为空,查找不成功,返回false。
- 递归
// 如果找到了,返回 0 表示成功
// 如果没找到,返回 -1 表示失败
int SBTreeSearch(SBTreeNode* root, Key key)
{
if (root==NULL)
{
// 空树
return -1;
}
if (key == root->key)
{
return 0;
}
else if (key < root->key)
{
return SBTreeSearch(root->left, key);
}
else
{
return SBTreeSearch(root->right, key);
}
}
- 非递归
int SBTreeSearchLoop(SBTreeNode* root, Key key)
{
SBTreeNode* cur = root;
while (cur != NULL)
{
if (key == cur->key)
{
return 0;
}
else if (key < cur->key) {
cur = cur->left;
}
else {
cur = cur->right;
}
}
return -1;
}
-
插入
在二叉搜索树中插入新元素时,必须先检测该元素是否在此树中已经存在。如果已经存在,则则不进行插入;否则将新元素加入到搜索停止的地方。
- 递归
int SBTreeInsert(SBTreeNode** pproot, Key key)
{
if (*pproot == NULL) {
SBTreeNode *node = (SBTreeNode *)malloc(sizeof(SBTreeNode));
node->key = key;
node->left = NULL; node->right = NULL;
*pproot = node;
return 0;
}
if (key == (*pproot)->key)
{
return -1;
}
if (key < (*pproot)->key)
{
return SBTreeInsert(&(*pproot)->left, key);
}
else
{
return SBTreeInsert(&(*pproot)->right, key);
}
}
- 非递归
int SBTreeInsertLoop(SBTreeNode** pproot, Key key)
{
assert(pproot);
SBTreeNode* cur = *pproot;
SBTreeNode* parent = NULL;
while (cur != NULL)
{
if (key == cur->key)
{
return -1;
}
parent = cur;
if (key < cur->key) {
cur = cur->left;
}
else {
cur = cur->right;
}
}
SBTreeNode* node = (SBTreeNode*)malloc(sizeof(SBTreeNode));
node->key = key;
node->left = node->right = NULL;
if (parent == NULL)
{
// 对空树做插入
*pproot = node;
}
else if (key < parent->key)
{
parent->left = node;
}
else
{
parent->right = node;
}
return 0;
}
-
删除
在二叉排序树删去一个结点,分三种情况讨论:
1.若*p结点为叶子结点,即PL(左子树)和PR(右子树)均为空树。由于删去叶子结点不破坏整棵树的结构,则可以直接删除此子结点。
2.若*p结点只有左子树PL或右子树PR,此时只要令PL或PR直接成为其双亲结点*f的左子树(当*p是左子树)或右子树(当*p是右子树)即可,作此修改也不破坏二叉排序树的特性。
3.若*p结点的左子树和右子树均不空。在删去*p之后,为保持其它元素之间的相对位置不变,可按中序遍历保持有序进行调整,可以有两种做法:
其一是令*p的左子树为*f的左/右(依*p是*f的左子树还是右子树而定)子树,*s为*p左子树的最右下的结点,而*p的 右子树为*s的右树; 其二是令*p的直接前驱(或直接后继)替代*p,然后再从二叉排序树中删去它的直接前驱(或直接后继)-即让*f的左子树(如果有的话)成为*p左子树的最左下结点(如果有的话),再让*f成为*p的左右结点的父结点。
// 如果找到,成功删除,返回 0
// 否则返回 -1
int SBTreeRemoveLoop(SBTreeNode** pproot, Key key)
{
SBTreeNode* cur = *pproot;
SBTreeNode*parent = NULL;
while (cur != NULL)
{
if (key == cur->key)
{
if (cur->left == NULL)
{
if (parent == NULL)
{
*pproot = cur->right;
}
else if (key < parent->key)
{
parent->left = cur->right;
}
}
if (parent == NULL)
{
*pproot = cur->right;
}
else if (key < parent->key)
{
parent->left = cur->right;
}
else
{
parent->right = cur->right;
}
free(cur);
return 0;
}
else if (cur->right == NULL)
{
if (parent == NULL)
{
// 要删除的是根结点
*pproot = cur->left;
}
else if (key < parent->key)
{
parent->left = cur->right;
}
else
{
parent->right = cur->left;
}
free(cur);
return 0;
}
else
{
// 替换法删除
// 左右孩子都不为空
// 找右子树中最小的一个
SBTreeNode* del = cur->right;
SBTreeNode* delParent = cur;
while (del->left != NULL)
{
delParent = del;
del = del->left;
}
cur->key = del->key;
if (delParent == cur)
{
delParent->right = del->right;
}
else
{
delParent->left = del->right;
}
free(del);
return 0;
}
parent = cur;
if (key < cur->key) {
cur = cur->left;
}
else {
cur = cur->right;
}
}
return -1;
}
-
二叉树的应用
- 判断一个单词是否拼写正确
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct WNode
{
char word[100];
struct WNode* left;
struct WNode* right;
}WNode;
// 只有查找和插入
int Search(WNode* root, char* word)
{
if (root == NULL)
{
return -1;
}
int r = strcmp(word, root->word);
if (r == 0)
{
return 0;
}
else if (r < 0)
{
return Search(root->left, word);
}
else
{
return Search(root->right, word);
}
}
int Insert(WNode **pproot, char *word)
{
if (*pproot == NULL)
{
// 插入
WNode *node = (WNode *)malloc(sizeof(WNode));
node->left = node->right = NULL;
strcpy(node->word, word);
*pproot = node;
return 0;
}
int r = strcmp(word, (*pproot)->word);
if (r == 0)
{
return -1;
}
else if (r < 0)
{
return Insert(&(*pproot)->left, word);
}
else
{
return Insert(&(*pproot)->right, word);
}
}
void test3()
{
char * words[] = {
"Apple", "Orange", "Banana", "Watermelon", "Pinapple",
"Average", "Dictionary", "Pen", "Able"
};
WNode *root = NULL;
for (int i = 0; i < sizeof(words) / sizeof(char *); ++i)
{
Insert(&root, words[i]);
}
while (1)
{
char word[100];
scanf("%s", word);
if (Search(root, word) == 0)
{
printf("拼写正确\n");
}
else {
printf("拼写错误\n");
}
}
}
- 统计每天有多少节课
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct SNode
{
char date[100]; // key
int count; // 课程数量 value
struct SNode *left;
struct SNode *right;
}SNode;
// 只有查找和插入
int * SchedulSearch(SNode *root, char *date)
{
if (root == NULL) {
return NULL;
}
int r = strcmp(date, root->date);
if (r == 0) {
return &root->count;
}
else if (r < 0) {
return SchedulSearch(root->left, date);
}
else {
return SchedulSearch(root->right, date);
}
}
int SchedelInsert(SNode **pproot, char *date, int count)
{
if (*pproot == NULL)
{
// 插入
SNode *node = (SNode *)malloc(sizeof(SNode));
node->left = node->right = NULL;
strcpy(node->date, date);
node->count = count;
*pproot = node;
return 0;
}
int r = strcmp(date, (*pproot)->date);
if (r == 0)
{
return -1;
}
else if (r < 0)
{
return SchedelInsert(&(*pproot)->left, date, count);
}
else
{
return SchedelInsert(&(*pproot)->right, date, count);
}
}
void test4()
{
SNode *root = NULL;
char *courses[][2] = {
{ "2018-10-16", "DS" },
{ "2018-10-16", "Linux" },
{ "2018-10-17", "Computer" },
{ "2018-10-17", "PE" },
{ "2018-10-16", "PE" },
{ "2018-10-18", "Linux" }
};
for (int i = 0; i < sizeof(courses) / sizeof(courses[0]); i++)
{
char * date = courses[i][0];
int *pCount = SchedulSearch(root, date);
if (pCount != NULL)
{
*pCount += 1;
}
else
{
SchedelInsert(&root, date, 1);
}
}
printf("成功\n");
}