PHP dictionary tree (Trie tree) definition and implementation example, Dictionary trie

Source: Internet
Author: User

PHP dictionary tree (Trie tree) definition and implementation example, Dictionary trie

This document describes how to define and implement the PHP dictionary tree (Trie tree. We will share this with you for your reference. The details are as follows:

Trie tree concept (Baidu's explanation): The dictionary tree, also known as the word search tree, is a tree structure and a variant of the hash tree. A typical application is used for statistics, sorting, and saving a large number of strings (but not limited to strings). Therefore, it is often used by the search engine system for text word frequency statistics. The advantage is that the common prefix of a string is used to reduce the query time and minimize unnecessary string comparisons. The query efficiency is higher than the hash height.

My understanding is used for string search. Each node contains only one character. For example, if you enter the word "world", the structure of the tree is:

Enter the word "worab", and the structure of the tree is:

Therefore, each node must have an is_end field to identify whether it is an ending word. For example, if you enter wor and search for all words starting with wor, assume that one word is wor and you start searching from "w, when "r" is retrieved, You need to judge that the is_end of the "r" node is true, then add the wor to the result list, and then continue to search for it below.

PHP implementation code:

<? Phpclass Node {public $ value; // Node value public $ is_end = false; // whether it is an end Node; whether it is an end Node public $ childNode = array (); // subnode/* Add a child node -- Note: it is not a reference function, because the PHP Object value assignment itself is a reference value */public function & addChildNode ($ value, $ is_end = false) {$ node = $ this-> searchChildNode ($ value); if (empty ($ node) {// No node exists, add as a child node $ Node = new node (); $ node-> value = $ value; $ this-> childNode [] = $ node ;} $ node-> is_end = $ is_end; return $ n Ode;}/* query subnode */public function searchChildNode ($ value) {foreach ($ this-> childNode as $ k => $ v) {if ($ v-> value = $ value) {// if a node exists, return the return $ this-> childNode [$ k] ;}} return false ;}} /* Add a string */function addString (& $ head, $ str) {$ node = null; for ($ I = 0; $ I <strlen ($ str ); $ I ++) {if ($ str [$ I]! = '') {$ Is_end = $ I! = (Strlen ($ str)-1 )? False: true; if ($ I = 0) {$ node = $ head-> addChildNode ($ str [$ I], $ is_end );} else {$ node = $ node-> addChildNode ($ str [$ I], $ is_end );}}}} /* Get all strings -- recursion */function getChildString ($ node, $ str_array = array (), $ str = '') {if ($ node-> is_end = true) {$ str_array [] = $ str;} if (empty ($ node-> childNode) {return $ str_array ;} else {foreach ($ node-> childNode as $ k => $ v) {$ str_array = getChildString ($ v, $ str _ Array, $ str. $ v-> value);} return $ str_array;}/* search */function searchString ($ node, $ str) {for ($ I = 0; $ I <strlen ($ str); $ I ++) {if ($ str [$ I]! = '') {$ Node = $ node-> searchChildNode ($ str [$ I]); // print_r ($ node); if (empty ($ node )) {// NULL return false;} return getChildString ($ node);}/* Call Test start */$ head = new Node; // tree head // Add the word addString ($ head, 'hewol '); addString ($ head, 'hemy'); addString ($ head, 'hem '); addString ($ head, 'you'); addString ($ head, 'yo'); // get all words $ str_array = getChildString ($ head ); // search $ search_array = searchString ($ head, 'hem'); // print all search results in a loop ($ search_array as $ key => $ value) {echo 'hem '. $ value. '<br>'; // search prefix for output}

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.