关于二叉搜索树的基本知识点:二叉搜索树的创建,删除,查找
模拟实现一个简单的字典
即将二叉搜索树里数据域改为单词和翻译。查找时与单词比较,若相等,返回它的翻译。若不相等返回单词不存在。
代码:
BSWordTree.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
typedef char* key;
typedef char* value;
typedef struct BSTree
{
struct BSTree *_pleft;
struct BSTree *_pright;
key _k;
value _v;
}BSWordTree;
//初始化
void InitBSWordTree(BSWordTree **BSTree);
//插入
int InsertBSWordTree(BSWordTree **BSTree, key _k, value _v);
//查找
char * FindBSWordTree(BSWordTree *BStree, key _k);
//销毁
void DestroyBSWordTree(BSWordTree **BStree);
BSWordTree.c
#include"BSTword.h"
//初始化
void InitBSWordTree(BSWordTree **BSTree)
{
assert(BSTree);
*BSTree = NULL;
}
BSWordTree* BuyNode(key k, value v)
{
BSWordTree *newnode = (BSWordTree *)malloc(sizeof(BSWordTree));
if (NULL == newnode)
{
//若开辟空间失败,则打印出错误,不进行后面的代码
assert(0);
return NULL;
}
newnode->_k = k;
newnode->_v = v;
newnode->_pleft = NULL;
newnode->_pright = NULL;
return newnode;
}
////非递归插入
//int InsertBSWordTree(BSWordTree **BStree, key k, value v)
//{
// BSWordTree *cur = NULL;
// BSWordTree *parent = NULL;
//
// assert(BStree);
// cur = *BStree;
// //1.若二叉树为空
// if (NULL == *BStree)
// {
// *BStree = BuyNode(k,v);
// }
// //2.若二叉树不为空
// else
// {
//
// //①.寻找插入的位置
// while (cur != NULL)
// {
// parent = cur;
// if (strcmp(cur->_k,k) > 0)
// {
// cur = cur->_pleft;
// }
// else if (strcmp(cur->_k, k) < 0)
// {
// cur = cur->_pright;
// }
// else
// {
// return 0;
// }
// }
// //②.建立新结点
// cur = BuyNode(k, v);
// //③.data若比双亲大,则插右边,若比双亲小,则插左边
// if (strcmp(parent->_k, k) > 0)
// {
// parent->_pleft = cur;
// }
// else
// {
// parent->_pright = cur;
// }
// }
// return 1;
//}
//递归插入
int InsertBSWordTree(BSWordTree **BStree, key k, value v)
{
BSWordTree *cur = NULL;
BSWordTree *parent = NULL;
assert(BStree);
if (NULL == *BStree)
{
*BStree = BuyNode(k, v);
}
if (strcmp((*BStree)->_k, k) > 0)
{
return InsertBSWordTree(&(*BStree)->_pleft, k, v);
}
else if (strcmp((*BStree)->_k, k) < 0)
{
return InsertBSWordTree(&(*BStree)->_pright, k, v);
}
else
{
return 0;
}
}
////非递归查找
//char * FindBSWordTree(BSWordTree *BStree, key _k)
//{
// BSWordTree *cur = BStree;
// //根节点为空,即二叉树为空,返回0
// if (BStree == NULL)
// {
// return NULL;
// }
// while (cur != NULL)
// {
// if (strcmp (cur->_k, _k) == 0)
// {
// return cur->_v;
// }
// else if (strcmp(cur->_k, _k) > 0)
// {
// cur = cur->_pleft;
// }
// else
// {
// cur = cur->_pright;
// }
// }
// //cur==NULL即没有找到元素和data相等,返回null
// return NULL;
//}
//递归查找
char * FindBSWordTree(BSWordTree *BStree, key _k)
{
BSWordTree *cur = BStree;
//根节点为空,即二叉树为空,返回0
if (BStree == NULL)
{
return NULL;
}
if (strcmp(cur->_k, _k) == 0)
{
return cur->_v;
}
else if (strcmp(cur->_k, _k) > 0)
{
return FindBSWordTree(BStree->_pleft, _k);
}
else
{
return FindBSWordTree(BStree->_pright, _k);
}
}
//销毁二叉树
void DestroyBSWordTree(BSWordTree **BStree)
{
assert(BStree);
if (*BStree == NULL)
{
return;
}
DestroyBSWordTree(&(*BStree)->_pleft);
DestroyBSWordTree(&(*BStree)->_pright);
free(*BStree);
*BStree = NULL;
}
测试文件
#include"BSTword.h"
void TestBSWordTree()
{
BSWordTree *BSTree;
char a[10] = { 0 };
//初始化 sss
InitBSWordTree(&BSTree);
InsertBSWordTree(&BSTree,"love","爱");
InsertBSWordTree(&BSTree, "hate", "讨厌");
InsertBSWordTree(&BSTree, "learn", "学习");
InsertBSWordTree(&BSTree, "english", "英语");
InsertBSWordTree(&BSTree, "math", "数学");
while (1)
{
printf("请输入想要查询的单词:");
scanf("%s", &a);
if (FindBSWordTree(BSTree, a))
{
printf("您查询的单词意思为:%s\n", FindBSWordTree(BSTree, a));
}
else
{
printf("单词不存在!\n");
}
}
}
int main()
{
TestBSWordTree();
system("pause");
return 0;
}
运行:
判断一个单词的拼写是否正确
即将二叉搜索树里数据域改为单词。查找时与单词比较。
.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include<string.h>
typedef struct BS
{
struct BS* _left;
struct BS* _right;
char * word;
}BSTree;
void InitBSwordTree(BSTree **bs);
int InsertBSwoedTree(BSTree **bs,char *word);
int FindWord(BSTree *bs, char *word);
.c
#include"BSwordTree.h"
void InitBSwordTree(BSTree **bs)
{
assert(bs);
*bs = NULL;
}
BSTree *BuyNode(char *word)
{
BSTree *node = NULL;
node = (BSTree *)malloc(sizeof(BSTree));
if (node == NULL)
{
assert(0);
return NULL;
}
node->_left = NULL;
node->_right = NULL;
node->word = word;
return node;
}
//非递归插入
//int InsertBSwoedTree(BSTree **bs,char *word)
//{
// BSTree *cur = NULL;
// BSTree *parent = NULL;
//
// assert(bs);
// int ret = 0;
//
// cur = *bs;
// //二叉树为空时插入
// if (*bs == NULL)
// {
// *bs = BuyNode(word);
// return 1;
// }
// else
// {
// while (cur != NULL)
// {
// ret = strcmp(word, cur->word);
// if (ret > 0)
// {
// parent = cur;
// cur = cur->_right;
// }
// else if (ret < 0)
// {
// parent = cur;
// cur = cur->_left;
// }
// else
// {
// return 0;
// }
// }
// //二叉树不为空插入
// cur = BuyNode(word);
// if (strcmp(parent->word,word) > 0)
// {
// parent->_left = cur;
// }
// else
// {
// parent->_right = cur;
// }
// }
// return 1;
//}
//递归插入
int InsertBSwoedTree(BSTree **bs, char *word)
{
BSTree *cur = NULL;
BSTree *parent = NULL;
assert(bs);
int ret = 0;
if (*bs == NULL)
{
*bs = BuyNode(word);
}
ret = strcmp(word, (*bs)->word);
if (ret > 0)
{
return InsertBSwoedTree(&(*bs)->_right, word);
}
else if (ret < 0)
{
return InsertBSwoedTree(&(*bs)->_left, word);
}
else
return 0;
}
//非递归查找
//int FindWord(BSTree *bs, char *word)
//{
// BSTree *cur = bs;
// int ret = 0;
// if (bs == NULL)
// {
// return 0;
// }
// while (cur != NULL)
// {
// ret = strcmp(word, cur->word);
// if (ret == 0)
// {
// return 1;
// }
// else if (ret > 0)
// {
// cur = cur->_right;
// }
// else
// {
// cur = cur->_left;
// }
// }
// return 0;
//}
//递归查找
int FindWord(BSTree *bs, char *word)
{
BSTree *cur = bs;
int ret = 0;
if (bs == NULL)
{
return 0;
}
ret = strcmp(word, cur->word);
if (ret == 0)
{
return 1;
}
else if (ret > 0)
{
return FindWord(bs->_right, word);
}
else
{
return FindWord(bs->_left, word);
}
}
测试文件
#include"BSwordTree.h"
void Test()
{
BSTree* bs;
char word[20] = { 0 };
InitBSwordTree(&bs);
InsertBSwoedTree(&bs, "english");
InsertBSwoedTree(&bs, "math");
InsertBSwoedTree(&bs, "banana");
InsertBSwoedTree(&bs, "apple");
InsertBSwoedTree(&bs, "love");
InsertBSwoedTree(&bs, "word");
while (1)
{
printf("请输入单词:");
scanf("%s", word);
if (FindWord(bs, word))
{
printf("单词拼写正确!\n");
}
else
{
printf("单词拼写错误!\n");
}
}
}
int main()
{
Test();
system("pause");
return 0;
}
运行: