Zatan Defeat oneself is often not the enemy, but oneself. Perseverance is not easy, and the line and cherish.
key tree also known as word search tree, trie tree tree-shaped structure characters string (but not limited to strings), so often used by the search engine system for text word frequency statistics. Its advantages are: the use of the common prefix of the string to reduce the query time, minimize the unnecessary string comparison, query efficiency than the hash tree high
The root node of the key tree does not contain characters, and the non-leaf nodes outside the root node contain only one character, and the leaf nodes store specific information. ; From the root node to a node, the characters that pass through the path are concatenated to the corresponding string for that node, and all child nodes of each node contain different characters. And the sibling nodes are sorted by character size.
For example:
Its key tree form is as follows:
Key Tree application: (from Baidu)
fast retrieval of stringsGive a cooked word list of n words, and an article written in lowercase English, please write all the new words that are not in the cooked vocabulary in the first order of occurrence. In this problem, we can use the array enumeration, with the hash, with the dictionary tree, the cooked word first to build a tree, and then read the article to compare, this method of efficiency is relatively high.string "SortGiven n a different English name consisting of only one word, let you sort them in dictionary order from small to large output with a dictionary tree, using an array of ways to create a dictionary tree, and all the sons of each node of the tree are clearly sorted by their letter size. This tree is preceded by a sequential traversal.the longest common prefixTo create a dictionary tree for all strings, the length of the longest public prefix for the two strings is the number of public ancestors of the node they are in, and then the problem is transformed into the common ancestor problem.
There are two ways to represent the key tree: 1. The double-linked list notation is the child of the tree linked table notation 2. Multi-linked list notation (trie tree)
The following is a key tree of the double-linked list notation Insert, delete, find and other function code:
Full source code network address :http://yun.baidu.com/share/link?shareid=582619924&uk=1208029266
DSTree.cpp: Defines the entry point of the console application. Double-link tree notation for key trees # include "stdafx.h" #include <cstdlib> #include <cstring> #include "queue.h" #include "stack.h" # Define Max_key_len16#define Leaf_end_char ' $ '//leaf node terminator struct Keytype{char key[max_key_len];int LEN; void Initkey (KeyType * Kt,char * k) {int len = strlen (k); strncpy (kt->key,k,len+1); kt->len = Len;} Enum E_KIND{E_KIND_LEAF,E_KIND_BRANCH,};TYPEDEF struct Dlnode{char data;dlnode * nextsibling;//next sibling node E_Kind Kind;; Union{char * record;//leaf node Information dlnode * firstchild;//first child node (branch node)};} *dltree;dlnode * Makenode (e_kind Kind) {dlnode * node = (Dlnode *) malloc (sizeof (Dlnode)); node->kind = kind;node-> nextSibling = null;if (Node->kind = = e_kind_branch) {node->firstchild = NULL;} Else{node->record = (char *) malloc (sizeof (char) *max_key_len); node->data = Leaf_end_char;} return node;} void Inittree (Dltree * tree) {*tree = NULL;} void Destorytree (Dltree * tree) {Dltree p = *tree;if (P! = NULL) {if (P->kind = = e_kind_leaf) {free (p->record);} ElsE{destorytree (&p->firstchild);} Destorytree (&p->nextsibling); free (p); *tree = NULL;}} Dltree Search (dltree T,char * k) {if (t! = NULL) {KeyType kt;initkey (&kt,k); int times = 0;t = T->firstchild;while (t && Times < Kt.len) {while (T && t->data < kt.key[times]) T = t->nextsibling;if (t && t ->data = = Kt.key[times]) {t = t->firstchild;times++;} Else{return NULL;}} if (Times = = Kt.len && T->kind = = e_kind_leaf) {return t;}} return NULL;} Search results. For INSERT keyword Service//is the first child node: Tree: Returns the parent node, not the sibling node. struct Result{bool isfind;//is not found bool isfirst;//is not the first child node Dltree tree;//insertion Position int index;//The position of the node to be inserted.}; Result Searchforinsert (Dltree tree,keytype key) {int times = 0; Result Result;result.isfirst = False;result.isfind = False;while (Tree && Times < Key.len) {Dltree Fathernode = Tree;result.index = Times;tree = Tree->firstchild;while (tree) {if (Tree->data > Key.key[times]) {if ( Fathernode->firstchild = = tree) {Result.isfirst = True;result.tree = Fathernode;} return result;} else if (Tree->data = = Key.key[times]) {result.tree = Tree;times ++;break;} else{//$ than a ~ Z small result.tree = Tree;tree = Tree->nextsibling;}}} if (times = = Key.len) {//all are equal if (tree && tree->firstchild && tree->firstchild->kind = = e_kind_l EAF) {result.isfind = True;result.tree = Tree->firstchild;} Else{result.isfirst = True;result.index = Times;}} return result;} void Insert (Dltree t,keytype Kt,int index) {for (int i = index; i < Kt.len; i++) {dlnode * node = Makenode (E_kind_branch) ; node->data = Kt.key[i];t->firstchild = node;t = node;} Finally insert leaf node dlnode * leaf = Makenode (e_kind_leaf); strncpy (leaf->record,kt.key,kt.len+1); t->firstchild = leaf;} void Inserttree (Dltree * Tree,char * key) {KeyType Kt;initkey (&kt,key), if (*tree = = null) {///empty tree, need to set a root node *tree = Mak Enode (e_kind_branch); insert (*tree,kt,0); return;} result = Searchforinsert (*tree,kt), if (Result.isfind = = False) {//not found, and then inserted. if (RESULT.ISFIRST) {//insert in head dltree Fathernode = Result.tree;dltree Oldfirst = Fathernode->firstchild;insert (Fathernode,kt, Result.index); fathernode->firstchild->nextsibling = Oldfirst;} Else{dltree leftbrother = result.tree;dlnode * node = Makenode (e_kind_branch); node->data = Kt.key[result.index]; node->nextsibling = leftbrother->nextsibling;leftbrother->nextsibling = Node;insert (node,kt,result.index+ 1);}}} void DeleteTree (Dltree * T,char * k) {if (*t! = NULL) {KeyType kt;initkey (&kt,k);D ltree p = (*t)->firstchild;int time s = 0;linkstack Stack;stackinit (&stack), Stackpush (&stack,*t), while (P && times < Kt.len) {while (P & amp;& P->data < kt.key[times]) p = p->nextsibling;if (p && p->data = Kt.key[times]) {Stackpush (&A mp;stack,p);p = p->firstchild;times++;} else{return;//did not find}}if (p && p->kind = = e_kind_leaf) {Dltree f = null;while (!stackempty (Stack)) {Stackpop ( &STACK,&F); if (f->firstchild = = P && p->nextsibLing = = NULL) {//father has only one child free (p);p = f;if (*t = = f) {//All keywords are deleted, empty tree. Free (*t); *t = Null;return;}} Else{break;}} if (f->firstchild = = p) {f->firstchild = p->nextsibling;} Else{dltree pre = F->firstchild;while (pre! = NULL && pre->nextsibling! = p) Pre = pre->nextsibling;pre-& gt;nextsibling = p->nextsibling;} Free (p);} Stackdestory (&stack);}} Sequence traversal void Leveltraverse (Dltree tree) {if (tree! = null) {if (tree->firstchild! = null) {printf ("---------------- Sequence traversal----------------\ n "); Linkqueue Queue;queueinit (&queue); Enqueue (&queue,tree->firstchild); int count = 1;int level = 1;int Nextcount = 0;while (!queueempty (queue)) {printf ("%d layer data:", Level), for (int i = 0; i < count; i++) {Dltree t;dequeue (& QUEUE,&T); while (t) {printf ("%c", t->data); if (T->kind = = E_kind_branch && t->firstchild! = NULL) {E Nqueue (&queue,t->firstchild); nextcount++;} t = t->nextsibling;}} printf ("\ n"); count = Nextcount;nextcount = 0;level++;} Queuedestory(&queue);p rintf ("\ n");}} static char Testarray[][max_key_len] = {//18 "Cao", "Cai", "Chang", "Chao", "Cha", "Chen",//6 "Wen", "Wang", "WU",//3 "Zhao ",//1" Li "," LAN "," Long "," Liu ",//4" Yun "," Yang ",//2" Zha "," L ",};int _tmain (int argc, _tchar* argv[]) {Dltree root; Inittree (&root); for (int i = 0; i <; i++) {Inserttree (&root,testarray[i]);} Leveltraverse (root); for (int i = 0; i <; i++) {char * s = testarray[i];D ltree t = search (root,s); if (t) {printf ("Find% S, recorded as:%s\n ", S,t->record);} else{printf ("Find%s, not found \ n", s);}} for (int i = 0; i < i++) {char * s = testarray[i];d eletetree (&root,s);p rintf ("-------------after deleting%s--------------\ n ", s); Leveltraverse (root);} Destorytree (&root); return 0;}
full source code network address:http://yun.baidu.com/share/link?shareid=582619924&uk=1208029266
Look at the data structure write code (59) The double-chain representation of the key tree