二叉搜尋樹應用-判斷一個單詞是否拼寫正確,實現簡單字典
搜尋二叉樹基本概念請看上篇部落格
這兩個問題都是典型的K(key)V(value)問題,我們用KV演算法解決。
- 判斷一個單詞是否拼寫正確:假設把所有單詞都按照搜尋樹的性質插入到搜尋二叉樹中,我們判斷一個單詞拼寫是否正確就是在樹中查詢該單詞是否存在(查詢key是否存在)。
結構宣告下
typedef char* KeyType;
typedef struct BSTreeNode
{
struct BSTreeNode *_left;
struct BSTreeNode *_right;
KeyType _key;
}BSTreeNode;
插入,查詢函式程式碼如下:
int BSTreeNodeInsertR(BSTreeNode **tree,KeyType key) //搜尋樹的插入
{
int tmp = 0;
if(*tree == NULL)
{
*tree = BuyTreeNode(key);
return 0;
}
tmp = strcmp((*tree)->_key,key);
if (tmp>0)
return BSTreeNodeInsertR(&(*tree)->_left,key);
else if (tmp<0)
return BSTreeNodeInsertR(&(*tree)->_right,key);
else
return -1;
}
BSTreeNode *BSTreeNodeFindR(BSTreeNode *tree,KeyType const key) //查詢
{
int tmp = 0;
if (!tree)
return NULL;
tmp = strcmp(tree->_key,key);
if (tmp > 0)
return BSTreeNodeFindR(tree->_left,key);
else if (tmp < 0)
return BSTreeNodeFindR(tree->_right,key);
else
return tree;
}
測試程式碼:
void TestApplication()
{
BSTreeNode *tree = NULL;
BSTreeNodeInsertR(&tree,"hello");
BSTreeNodeInsertR(&tree,"world");
BSTreeNodeInsertR(&tree,"int");
BSTreeNodeInsertR(&tree,"char");
BSTreeNodeInsertR(&tree,"float");
BSTreeNodeInsertR(&tree,"double");
printf("%s \n", BSTreeNodeFindR(tree,"char")->_key);
printf("%s \n", BSTreeNodeFindR(tree,"double")->_key);
printf("%s \n", BSTreeNodeFindR(tree,"int")->_key);
printf("%s \n", BSTreeNodeFindR(tree,"float")->_key);
printf("%s \n", BSTreeNodeFindR(tree,"hello")->_key);
printf("%s \n", BSTreeNodeFindR(tree,"world")->_key);
printf("%p \n", BSTreeNodeFindR(tree,"chars"));
printf("%p \n", BSTreeNodeFindR(tree,"str"));
}
測試結果:
2. 請模擬實現一個簡單的字典(簡單的中英文字典)
結構構建
typedef char* KeyType;
typedef int ValueType;
typedef struct BSTreeNode
{
struct BSTreeNode *_left;
struct BSTreeNode *_right;
KeyType _key;
ValueType _count;
}BSTreeNode;
查詢函式與上面相同,插入函式有所改變。
int BSTreeNodeInsertR(BSTreeNode **tree,KeyType key, ValueType value) //搜尋樹的插入
{
int tmp = 0;
if(*tree == NULL)
{
*tree = BuyTreeNode(key,value);
return 0;
}
tmp = strcmp((*tree)->_key,key);
if (tmp>0)
return BSTreeNodeInsertR(&(*tree)->_left,key,value);
else if (tmp<0)
return BSTreeNodeInsertR(&(*tree)->_right,key,value);
else
return -1;
}
測試程式碼:
void test()
{
BSTreeNode *tree = NULL;
BSTreeNodeInsertR(&tree,"hello","你好");
BSTreeNodeInsertR(&tree,"world","世界");
BSTreeNodeInsertR(&tree,"char","字元");
BSTreeNodeInsertR(&tree,"int","整形");
BSTreeNodeInsertR(&tree,"float","浮點型");
printf("%s \n", BSTreeNodeFindR(tree,"char")->_value);
printf("%s \n", BSTreeNodeFindR(tree,"int")->_value);
printf("%s \n", BSTreeNodeFindR(tree,"float")->_value);
printf("%s \n", BSTreeNodeFindR(tree,"hello")->_value);
printf("%s \n", BSTreeNodeFindR(tree,"world")->_value);
printf("%p \n", BSTreeNodeFindR(tree,"double"));
}
測試結果
構建測試案例儘量構建全面,防止特殊情況被忽略。
相關文章
- 判斷序列是否是二叉搜尋樹的後續遍歷
- 字典樹(字首樹)簡單實現
- PHP判斷電子郵件是否正確的簡單方法介紹PHP
- [Python手撕]判斷二叉搜尋樹Python
- 使用正規表示式判斷是否為手機號碼(簡單且實用)
- Elasticsearch 實現簡單搜尋Elasticsearch
- 單詞搜尋
- 判斷二叉樹是否為滿二叉樹二叉樹
- 如果精確判斷一個IP是否被佔用
- 如何判斷一棵樹是否是二叉平衡樹~
- javascript實現二叉搜尋樹JavaScript
- 單詞拼寫糾正-05-2452.力扣 距離字典兩次編輯距離以內的單詞力扣
- java 英文單詞拼寫糾正框架(Word Checker)Java框架
- 遞迴判斷是否二叉平衡樹遞迴
- HDU 1671 字典樹(判斷是否有一個串是另一個串的子串)。
- 【C++】判斷一顆二叉樹是否對稱C++二叉樹
- 二叉搜尋樹的python實現Python
- Trie|如何用字典樹實現搜尋引擎的關鍵詞提示功能
- 是否同一棵二叉搜尋樹(25 分)
- js實現完全排序二叉樹、二叉搜尋樹JS排序二叉樹
- 79. 單詞搜尋
- 單詞搜尋問題
- LC49判斷二叉樹是否相等二叉樹
- JavaScript實現簡單二叉查詢樹JavaScript
- JavaScript 二叉搜尋樹以及實現翻轉二叉樹JavaScript二叉樹
- 如何判斷FMEA是否以正確的方式完成?
- 用 JavaScript 實現簡單拼圖遊戲JavaScript遊戲
- LeetCode-079-單詞搜尋LeetCode
- 手寫AVL平衡二叉搜尋樹
- 二叉樹的插入和搜尋–python實現二叉樹Python
- 如何在 Java 中實現二叉搜尋樹Java
- 二叉搜尋樹(Binary Search Tree)(Java實現)Java
- JS實現簡單的判斷文字框長度JS
- 超簡單整合ML kit 實現聽寫單詞播報
- 英文單詞縮寫----DXNRY – Dictionary 字典
- SimpleAISearch:C# + DuckDuckGo 實現簡單的AI搜尋AIC#Go
- 二叉搜尋樹
- 【leetcode 簡單】 第六十八題 二叉搜尋樹的最近公共祖先LeetCode
- 【資料結構與演算法】二叉搜尋樹的簡單封裝資料結構演算法封裝