Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Dictionary implementation of searching binary Tree

Shulou Source: shulou.com Published: 2022-06-03 05:27:41 09月11日 Update

Use the search binary tree to determine whether a word is spelled correctly:

Assuming that all words are inserted into the search binary tree according to the nature of the search tree, we determine whether a word is spelled correctly is to find out if the word exists in the tree (to find out if key exists).

/ * * Date: March 26th, 2018 14:42:54*Author: Meng*WebSite:msyci.com*QQ:3515955122**/# include # include # include # includetypedef char* KeyType;typedef char* ValueType Typedef struct BSTreeNode {struct BSTreeNode* _ left; struct BSTreeNode* _ right; KeyType _ key; ValueType _ value;} BSTreeNode;BSTreeNode * BuyTreeNode (KeyType x MagneValueType value) / / create node {BSTreeNode* node = (BSTreeNode*) malloc (sizeof (BSTreeNode)); assert (node); node- > _ key = x; node- > _ left = NULL; node- > _ right = NULL Node- > _ value = value; return node;} / / insert, search function int BSTreeNodeInsertR (BSTreeNode * * tree,KeyType key, ValueType value) / / insert of search tree {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_right,key,value); else return-1;} BSTreeNode* BSTreeNodeFindR (BSTreeNode* tree, KeyType key) / / find {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, "China", "China"); BSTreeNodeInsertR (& tree, "score", "result"); BSTreeNodeInsertR (& tree, "char", "character") BSTreeNodeInsertR (& tree, "int", "× × ×"); BSTreeNodeInsertR (& tree, "float", "floating point"); 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, "China")-> _ value); printf ("% s\ n", BSTreeNodeFindR (tree, "score")-> _ value); printf ("% p\ n", BSTreeNodeFindR (tree, "double")) } int main (void) {TestApplication (); char c = getchar (); return 0;}

Running result:

Implement a simple Chinese and English dictionary

/ * * Date: March 26th, 2018 15:35:07*Author: Meng*WebSite:msyci.com*QQ:3515955122**/# include # include # include # includetypedef char* KeyType;typedef char* ValueType Typedef struct BSTreeNode {struct BSTreeNode* _ left; struct BSTreeNode* _ right; KeyType _ key; ValueType _ value;} BSTreeNode;BSTreeNode * BuyTreeNode (KeyType x MagneValueType value) / / create node {BSTreeNode* node = (BSTreeNode*) malloc (sizeof (BSTreeNode)); assert (node); node- > _ key = x; node- > _ left = NULL; node- > _ right = NULL Node- > _ value = value; return node;} / / insert, search function int BSTreeNodeInsertR (BSTreeNode * * tree,KeyType key, ValueType value) / / insert of search tree {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_right,key,value); else return-1;} BSTreeNode* BSTreeNodeFindR (BSTreeNode* tree, KeyType key) / / find {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, "China", "China"); BSTreeNodeInsertR (& tree, "score", "result"); BSTreeNodeInsertR (& tree, "char", "character") BSTreeNodeInsertR (& tree, "int", "× × ×"); BSTreeNodeInsertR (& tree, "float", "floating point"); 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, "China")-> _ value); printf ("% s\ n", BSTreeNodeFindR (tree, "score")-> _ value); printf ("% p\ n", BSTreeNodeFindR (tree, "double")) } int main (void) {TestApplication (); char c = getchar (); return 0;}

Running result:

Tags: Search words functions characters scores floating points results nodes China run Dictionary Chinese and English that is properties Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux MariaDB NVidia Shulou Technology