Hash tree (scatter Tsuraki) and trie tree (dictionary tree, prefix tree)

Source: Internet
Author: User


1.Hash Tree

Ideally, if you want to get the records you are looking for without any comparison, you must establish a definite correspondence between the memory location and its keywords, so that each keyword corresponds to a unique storage location. Thus, as soon as the lookup is found, as long as the corresponding relationship F to find a given value k like F (k). As a result, no comparison is required to obtain the records directly. In this case, we call this correspondence a hash function, the table created by this idea as a hash table.

In a hash table, the same hash address may be obtained for different keywords, and this behavior is called a conflict. In general, conflicts can only be reduced as much as possible, and not completely avoided. Because a hash function is an image from a collection of keywords to an address collection. Typically, the collection of keywords is larger, and its elements include all possible keywords, while the elements of the address collection are only the address values in the hash table. In general, a hash function is a compressed image function , which inevitably creates a conflict.



The theoretical basis of hash tree


" Prime number resolution theorem "
Simply put, thenumber of consecutive integers with n different prime numbers that can be "distinguished" is equal to their product . "Resolution" means that these successive integers cannot have exactly the same remainder sequence.

For example:
From 2 consecutive prime numbers, 10 consecutive prime numbers can be distinguished by about M (10) =2*3*5*7*11*13*17*19*23*29= 6,464,693,230 numbers, which have exceeded the expression range of the commonly used integers (32bit) in the computer. 100 consecutive prime numbers can be distinguished by about M (100) = 4.711930 times 10 219.

Insert


We select the prime number resolution algorithm to build a hash tree.
Select a continuous prime number starting at 2 to create a 10-layer hash tree . The first layer node is the root node, the root node has 2 nodes, the second layer has 3 nodes under each node, and so on, that is, the number of child nodes of each layer node is continuous prime . To the tenth floor, there are 29 nodes under each node.
Sub-nodes in the same node, from left to right, represent different remainder results.
For example, there are three sub-nodes under the second level node. Then from left to right respectively represent: In addition to more than 3 0, in addition to more than 3 1, in addition to more than 3 2.
The remainder of the number of hits is determined by the processing path .

Let's take a random 10-digit insertion as an example to illustrate the Hashtree insertion process.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/77/24/wKioL1Zj8xXR9kh8AADDr6MweSM474.png "title=" 20150603103842199.png "alt=" Wkiol1zj8xxr9kh8aaddr6mwesm474.png "/>


2.Trie Tree


 
Trie is its time complexity. Its insertion and query time complexity are O (k), where k is the length of the key, regardless of how many elements are stored in the Trie. The hash table is called O (1), but in the calculation of the hash will certainly be O (k), and there are collisions and other problems; The disadvantage of Trie is the high space consumption.
      trie Tree, also known as the word search tree or key tree, is a tree structure, is a kind of variants of hash tree . Typical applications are used to count and sort large numbers of strings (but not limited to strings), so they are often used by search engine systems for text frequency statistics. It has the advantage of minimizing unnecessary string comparisons and querying efficiencies over hash tables.
      trie is the space-changing time. Use common prefix of string To reduce the cost of query time to achieve the purpose of increasing efficiency.


As an example of a dictionary tree constructed with English words, each node in the trie tree contains 26 child nodes because there are 26 English letters (assuming the words are made up of lowercase letters).

Below we have and,as,at,cn,com these keywords, then how to build trie tree?


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/77/24/wKioL1Zj6CCwm99QAABQL33XR8o878.png "title=" 2012112521092438.png "alt=" Wkiol1zj6ccwm99qaabql33xr8o878.png "/>


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/77/25/wKiom1Zj6B3zg_AVAAA5qiJI8WY858.png "title=" 2012112521371883.png "alt=" Wkiom1zj6b3zg_avaaa5qiji8wy858.png "/>


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.




Reference article:

Trie Tree: Applied to statistics and sorting http://blog.csdn.net/hguisu/article/details/8131559

Graphic details hashtree (hash tree) http://blog.csdn.net/yang_yulei/article/details/46337405





This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1720067

Hash tree (scatter Tsuraki) and trie tree (dictionary tree, prefix tree)

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.