"Find two number xor maximum" 01 Dictionary tree Solution

Source: Internet
Author: User

first, the topic

Find the maximum value of an XOR value for any 2 of the N non-negative numbers. n Order of magnitude is 10^5. Analysis: Direct brute force solution will be timed out. But a string can be seen as a 32-bit 01 string, so you can build a dictionary tree, build a good tree for any nonnegative integer x, just first reverse, then follow the tree greedy lookup on the line. If you find 0 or 1 continue, you cannot find it and ignore the continue lookup.

Ii. Introduction to the dictionary tree

The dictionary tree is also called the word lookup tree, the Trie tree, is a tree-shaped structure, is a kind of Hashi variant. Typical applications are used to statistics, sort and save a large number of strings (but not limited to strings), so are often used by search engine systems for text frequency statistics. Its advantage is: Use the common prefix of string to save storage space, minimize unnecessary string comparison, query efficiency is higher than hash table.


A dictionary tree is similar to a dictionary when you want to find out whether a word is in the dictionary tree, first look at the first letter of the word is not in the first layer of the dictionary, if not, that the dictionary tree does not have the word, if the letter in the Children's node to find whether there is a word of the second letter, did not say that there is no word, Some words use the same method to continue searching. The dictionary tree can be used not only to store letters, but also to store numbers and other data

Three, code implementation dictionary tree and the algorithm of finding XOR or maximum value

The node definition of the tree:

	package trees;
	Import Java.util.HashMap;
	Import Java.util.Map;

	/**
	 * Created by Lemontree on 2016/10/31.
	 * * Public
	class Trienode {
    Map childdren;
    Boolean wordend;
    Public Trienode () {
        Childdren = new HashMap ();
        Wordend=false
    }
	}
	Package tree;

	The implementation of dictionary tree related methods and the realization of the difference or maximum value, in fact, is to modify the search strategy.

	/**
	 * Created by Lemontree on 2016/10/31.
	 * * Public
	class Trie {public
    trienode root;

    Public Trie () {
        root = new Trienode ();
        Root.wordend=false;
    }
    Inserts a number into the dictionary tree public
    void Insert (String word) {
        trienode node = root;
        for (int i=0;i>1;
            Sb.insert (0,c);
        }    System.out.println (sb.tostring ());
        return sb.tostring ();
    }
    public string Search (string word) {
        trienode node = root;
        StringBuilder sb = new StringBuilder ();
        for (int i=0;i

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.