Browser address bar input prompt function

Source: Internet
Author: User

Requires that when the user enters an address in the address bar of the browser, the browser can match the user's current input according to the history entered by the user, prompting the user for the characters that might be entered. Probably means to achieve the same effect as the current browser, such as the user previously entered AAA,ABCD,ABCC (simplified, regardless of the prefix "www." and suffix ". com"), the system can prompt AAA,AAAA,ABCD,ABCC when the user enters a in the browser address bar again. When entering AB, the system can prompt abcd,abcc.

Idea: The feeling can be solved by the structure of the tree, the process of solving the problem is as follows:

1. Data structure Selection

After the user enters AAA,AAAA,ABCD,ABCC, the internal storage structure is:


The data structure of the tree is:

Class Tree {tree Tparent;char val;boolean flag; List<tree> children; Tree (tree P, char C, Boolean b) {tparent = P;val = C;flag = B;children = new arraylist<tree> ();}}

Where flag is used to indicate whether a path can be obtained so far.

2. The process of achievement.

The user enters AAA,AAAA,ABCD,ABCC, after which the tree changes as shown in the process:


Figure V is the final result of storing the historical data aaa&aaaa&abcd&abcc, where the yellow node represents flag=true, indicating that a historical data is present in the input history data to the yellow node.

3. The search process.

Take user input Aa&ac as an example. After entering AA, the system finds the point of the symbol condition in the tree (that is, the red node in the graph). With this node as the root traverse, with the yellow point as the end, you can get A&AA. With the prefix, you get the final result: AAA, AAAA. Unable to find AC in the tree after entering AC, so no hint


4.CODE:

Import java.util.arraydeque;import java.util.arraylist;import Java.util.list;import java.util.queue;class Tree {tree Tparent;char Val;boolean Flag; List<tree> children; Tree (tree P, char C, Boolean b) {tparent = P;val = C;flag = B;children = new arraylist<tree> ();}} public class Solution {/** * Inserts a node into root, returning the inserted node. Note Flag settings during insertion * * @param root * @param c * @param b * @return */public static tree insert (Tree root, char C, Boolean B) {Tree node = new Tree (root, c, b), if (!iscontain (Root.children, node)) {Root.children.add (node);} else {for (tree temp:r Oot.children) if (temp.val = = Node.val) {if (b = = true) Temp.flag = B;return temp;}} return node;}  /** * Determine if node is already present in children * * @param children * @param node * @return */public static Boolean Iscontain (list<tree> Children, tree node) {for (tree Temp:children) {if (Temp.val = = Node.val) return true;} return false;} /** * Print the path with node as root * * @param node */public static void Printpath (Tree node) {queue<tree> Queue = new ARraydeque<tree> (); Tree temp = new tree (null, '. ', false); Queue.add (node); while (Queue.size ()! = 0) {temp = Queue.poll (); if (Temp.flag = = Tru e) {Tree Ori = new tree (null, ', ', false); ori = temp; String strtemp = ""; while (temp.tparent! = null) {strtemp + = Temp.val;temp = temp.tparent;} for (int i = Strtemp.length ()-1; I >= 0; i.) System.out.print (Strtemp.charat (i)); SYSTEM.OUT.PRINTLN (); temp = ori;} for (Tree T:temp.children) queue.add (t);}} /** * Find & Traverse * * @param root * @param str */public static void Sendhint (tree root, String str) {tree node = Searchnode (Root, str), if (node = = null) {System.out.println ("No Hint"); return;} Printpath (node);} /** * Search in the history tree based on the input string, find the node that meets the criteria (the node is traversed to get the final hint) * * @param root * @param str * @return */public static Tree Sear Chnode (Tree root, String str) {if (str = = NULL | | str.length () = = 0) return root;for (int i = 0; i < str.length (); ++i) {for (Tree Node:root.children) {if (Node.val = = Str.charat (i)) {return Searchnode (node, str.substring (i + 1, str.length ()));}}} return null;} public static void Main (string[] args) {//TODO auto-generated Method Stubtree origin = new Tree (null, '. ', false); Tree root = new tree (null, '. ', false); origin = root; string[] str = {"AAA", "AAAA", "ABCD", "ABCC", "Adbc", "adddd"};//Achievements for (String Temp:str) {for (int i = 0; i < tem P.length (); ++i) {root = insert (root, Temp.charat (i), (i = = Temp.length ()-1));} Root = origin;} Search Sendhint (Origin, "AB");}}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Browser address bar input prompt function

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.