The tenth to 12th chapter algorithm analysis--high-order data structure

Source: Internet
Author: User

1. The second application of greedy algorithm is Huffman encoding for file compression. The main problem of file compression is to assign all the characters in the file to uniquely identify the encoding (n bits), if we know the frequency of all characters in advance, the highest frequency on the top, low frequency on the left of the lowest, this is the optimal encoding.

2. The encoding tree, all characters are placed on the leaf node, each path to the left to represent 0, to the right of each path represents 1, this data structure is sometimes called trie tree, the depth of the node is both the required number of bits. This tree is full of trees: all nodes are either leaves or have two sons. An optimal encoding will always have this nature. And such a code is both a prefix code.

3. Red and black Trees

(1) The red-black tree is a variant of the AVL tree (self-balancing binary search tree), and its various operations cost O (LOGN) time in the worst case scenario;

(2) Red black Tree Properties: 1. The root is black 2. The child nodes of the red node must be black 3. Each path of a node to a null reference must contain the same number of black nodes.

(3) Bottom-up insert: 1. The parent of the newly inserted item is black, the insert completes

2. If the parent node of the insertion node is red:

2.1 The sibling node of the parent node is black and can be rotated either by single or double.

2.2 The sibling of the parent node is also red and needs to be resolved by taking action.

(4) Top-down red-black tree: You can advance the red and black tree top-down process, so that the parent's brother is not red.

4. Suffix arrays and suffix trees

(1) One of the most fundamental problems in data processing is to find the location of a pattern p from the text T, and answer the following questions: A. Is there a substring that matches P's t? How many times, and where b.p appear in T. C. General problem, T is fixed, there are frequent requests for different p. To achieve these goals, we typically preprocess T into a special data structure called a suffix array or suffix tree.

(2) Suffix array: The suffix array of the text T is actually an array of all the suffixes of t that are arranged in order. Mode p If in the text, then p must be a prefix of a suffix, then you can find by binary, O (LOGT)

Time to find. When the maximum common prefix of the adjacent suffix is computed, the number of occurrences of P will be O (p+ logt).

(3) Java implementation of suffix arrays

Compute the longest share string prefix
public static int COMPUTALCP (String s1,string s2) {
int i=0;
while (I<s1.length () &&i<s2.length () &&s1.charat (i) ==s2.charat (i)) {
i++;
}
return i;
}

public static void Createsuffixarray (String str,Int[] SA,Int[] LCP) {
if (SA.Length!=str.length () | | Lcp.Length!=str.length ())
throw new IllegalArgumentException ();

int N = Str.length ();

string[] Suffiesx =New String[n];
for (int i = 0; i < N; i++) {
Suffiesx[i] = str.substring (i);
}
Arrays. sort (suffiesx);

For (int i = 0; i < N; i++) {
Sa[i] = N-suffiesx[i].length ();
}

lcp[0] = 0;
For (int i = 1; i < N; i++) {
Lcp[i] = COMPUTALCP (suffiesx[i],suffiesx[i-1]);
}

}

The tenth to 12th chapter algorithm analysis--high-order data structure

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.