Learning data structure_3 _ string and tree (1)

Source: Internet
Author: User

Today, I went on to work as a part-time employee. After lunch, I went to the library to check the data structure. Oh, I was abused by the KMP algorithm for one afternoon, when I first took the data structure electives, why didn't I have a good time. The following are the study notes for today.


String)

1. Definition: a finite sequence consisting of 0 or more characters, that is, a string.

2. The comparison of strings is achieved by the character encoding size, including the ASCII code (7-bit and 8-bit binary) and Unicode code (16-bit); the strings are equal.

3. the string is not a linear table, because the string targets character sets and focuses on searching and replacing substrings. The linear table operates on a single element.

4. strings are also stored in a sequential and chained manner.

5. Simple pattern matching algorithm: A large loop is made on the Main string. Each character of the Main string starts with a small loop of the Child String Length t until the matching is successful or all traversal is performed. Time complexity O (n-m + 1) * m)

6. KMP pattern matching algorithm

Principle: Use completed matching results to save repeated matching operations. Time complexity O (N + M)

Difficulties: Solving next [J] and using next [] array for index operations

Next [J]: when the first J-1 matches, the first I of the Main string does not match the fourth J of the pattern, the next [J] character must be compared with the I character of the Main string (I does not move ).


Void get_next (string T, int * Next)

{

Int I, J;

I = 1, j = 0;

Next [1] = 0;

While (I <t [0])

{

If (j = 0 | T [I] = T [J])

{

I ++;

J ++;

Next [I] = J;

}

Else J = next [J];

}

}

7. Improved KMP Algorithm

Improvement on next []: when calculating next [], if the character is equal to the B character pointed to by next, the nextval of a points to the nextval of B. Otherwise, the nextval of A is equal to the next value of.


Tree)

1. Definition: it is a finite set of N nodes. There is only one root node, and the subtree does not overlap.

2. node classification: If the degree (number of sub-trees owned by the node) is 0, the leaf node (terminal node). The degree of the tree is the maximum degree of each node.

3. Forest: m> = a set of 0 trees that do not match each other.

4. Tree Storage Structure: parental notation; Child sibling notation. You can add a specific pointer field according to the needs of the algorithm to implement the functions in the above three kinds of notation.

5. Binary Tree: Therefore, the degree of the node <= 2; left and right Subtrees are ordered.

6. Special Binary Tree

Oblique tree: All nodes have only the left (right) subtree. A linear table is a special case of a tree.

Full Binary Tree: All branch nodes have left and right Subtrees, and all leaves are on the last layer.

Full Binary Tree: Binary Tree without blocks by sequence number.

7. The nature of Binary Trees

Layer I has a maximum of 2 ^ (I-1) nodes;

A binary tree with a depth of K can have at most (2 ^ K)-1 node;

Any Binary Tree meets the requirements: the number of knots with a leaf node tree = degree of 2 + 1;

If the Complete Binary Tree node is N, its depth is (log2 N) rounded up to + 1;

The parent node of the Complete Binary Tree node I is I/2 rounded up; the left child is 2I.

8. Binary Tree Storage Structure

A Complete Binary Tree can be represented by a simple sequence structure. A common binary tree needs to be represented by a binary linked list: lchild + Data + rchild. If yes, you can add a pointer to the parent tree, that is, the triple linked list.

9. binary tree traversal: each node is accessed only once.

Forward traversal: Root, left, and right subtree

Middle-order traversal: Left subtree, root, right subtree

Post-order traversal: First leaves and then nodes (from left subtree to right subtree), and then the root node

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.