Trie Tree (dictionary tree) Implementation of Word frequency statistics or prefix matching types

Source: Internet
Author: User
Tags bool

First, the concept

If we have and,as,at,cn,com these keywords, then the trie tree (the dictionary tree) is this:

From the above figure, we can find some interesting features more or less.

First: The root node does not contain characters, and each child node outside of the root node contains a single character.

Second: From the root node to a node, the characters that pass through the path are concatenated, which is the corresponding string for that node.

Third: The common prefix for each word is saved as a character node.

Ii. Scope of Use

Now that we learn trie tree, we must know what this thing is for.

First: Word frequency statistics.

May be someone to say, the word frequency statistics simple ah, a hash or a heap can be finished, but the problem comes, if the memory is limited. can still be so

Play it. So here we can use the trie tree to compress the space, because the public prefix is saved with a node.

Second: prefix matching

Take the above diagram, if I want to get all the string starting with "a", from the figure can be very obvious to see is: And,as,at, if not trie tree,

How do you do that? It is clear that the simple practice of Time complexity O (N2), then the trie tree is not the same, it can do h,h for you to retrieve the length of the word,

It can be said that this is the effect of the second kill.

Three, trie key realization

1. Structural body

Trie tree structure
struct Trie
{
	Trie *next[26];
	bool Isword;
} Root;


2. Insert operation

Insert operation (also build Trie tree)
void Insert (char *tar)
{
	trie* head = &Root;
	int id;
	while (*tar)
	{
		id = *tar-' a ';
		if (head->next[id] = = NULL)
			Head->next[id] = new Trie ();
		Head = head->next[id];
		tar++;
	}
	Head->isword = true;
}

3. Find operations

Find
bool Search (char *tar)
{
	trie* head = &Root;
	int id;
	while (*tar)
	{
		id = *tar-' a ';
		if (head->next[id] = = NULL)
			return false;
		Head = head->next[id];
		tar++;
	}
	if (Head->isword)
		return true;
	else
		return false;
}


Iv. Application of solving problems

poj2503 question, probably meaning is given two languages, a, b of the corresponding relationship between the words, and then tell you a word in a, let you output a word in a, intuitive understanding such as is to tell you the English tree, let you output Chinese "trees", according to the above ideas, appropriate changes in the structure of the body, c Implementation of the code is as follows:

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct nodetype{struct nodetype*
	CHILD[26];
	Char word[11];
int Isword;
}node;
	void Insertword (node* node,char* wo,char* wt) {int id;
		while (*WT) {id=*wt-' a ';
			if (node->child[id]==null) {node->child[id]= (node*) malloc (sizeof (node));
			(Node->child[id])->isword=0;
			for (int i=0;i<26;i++) {node->child[id]->child[i]=null;
		}} node=node->child[id];
	wt++;
	} node->isword=1;
strcpy (NODE-&GT;WORD,WO);
	} char* Searchword (node* node,char* wd) {char noword[5]= "eh";
	int id;
		while (*WD) {id=*wd-' a ';
		if (node->child[id]==null) {return noword;
		} node=node->child[id];
	wd++;
	} if (Node->isword) {return node->word;
	} else{return Noword;
	}} int main () {int i,j;
	Char wo[11],wt[11],wd[25];
	node* node= (node*) malloc (sizeof (node));
	node->isword=0;
	for (i=0;i<26;i++) {node->child[i]=null; } while (gets (WD) && Wd[0]! = 0) {for (i=0;wd[i]!= '; i++) {wo[i] = Wd[i];  
        } Wo[i] = 0;
		for (++i,j=0; wd[i]; i++,j++) {wt[j] = Wd[i];  
        } Wt[j] = 0;
    Insertword (NODE,WO,WT);
		} while (scanf ("%s", wd)!=eof) {char* Word=searchword (NODE,WD);
	printf ("%s\n", word);
} return 0; }

In order to improve the level of Java, and write again in Java, but the input of the data in Java I changed a bit, first input n pairs of words, and then input m query words, because I really do not know what is read in Java is empty line and constantly read the format is how, anyway, it is important to think, The Java code is as follows:

Package com.lxq.poj2503;

Import Java.util.Scanner;
		public class Main {public static void main (string[] args) {int n,m;
		String lan=null,forgn = null,word=null;
		Scanner sc=new Scanner (system.in);
		N=sc.nextint ();
		Node node=new node ();
			for (int i=0;i<n;i++) {lan=sc.next ();
			Forgn=sc.next ();
		Insertword (NODE,LAN,FORGN);
		} m=sc.nextint ();
			for (int i=0;i<m;i++) {word=sc.next ();
		System.out.println (Searchword (Node,word)); }} private static string Searchword (node node, string Word) {for (int i=0;i<word.length (); i++) {int Id=word.cha
			RAt (i)-' a ';
			if (node.child[id]==null) {return "eh";
		} Node=node.child[id];
		} if (Node.isword) {return node.word;
		} else{return "eh"; }} private static void Insertword (node node, string lan, String forgn) {for (Int. i=0;i<forgn.length (); i++) {in
			T Id=forgn.charat (i)-' a ';
			if (node.child[id]==null) {node.child[id]=new node ();
		} Node=node.child[id]; } Node.isword=truE
	Node.word=lan;
	}} class node{node[] child=new node[26];
	String Word;
	Boolean Isword;
		Public Node () {for (int i=0;i<26;i++) {child[i]=null;
	} Isword=false;
 }
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.