Review of search algorithms for data structure review

Source: Internet
Author: User

first, the basic concept of the search

A lookup (searching) is a given value that determines in a lookup table that its key is equal to a given worth of data elements (or records).

A lookup table is a collection of data elements (or records) of the same type.

Look-up tables are divided into two main categories as follows:

Static lookup table: A lookup table that is used only as a lookup operation.

Sequential table lookups, ordered table lookups, linear index lookups

Dynamic lookup Table: Inserts a data element that does not exist in the lookup table during the lookup process, or removes an element that already exists from the lookup table.

Binary sort tree, balanced binary tree (AVL tree), Multi-path lookup tree (b-Tree)

second, sequential table query

  This is the simplest one, starting with the last record in the table, comparing the recorded keyword to the given value, and if the key of a record is equal to the given value, the lookup succeeds and the record is found; Conversely, if the keyword and the given value are not equal until the first record is compared, Indicates that no records were found in the table, and the lookup failed.

The average lookup length is (n+1)/2, the best length is 1, and the worst lookup length is n. Time complexity O (n)

Optimization measures:

Set Sentinel

Set a[0] As the keyword value, called Sentinel, when the return value of 0 indicates not found. Find from the tail.

Eliminates the need to determine whether or not the search location is out of bounds after every comparison in the lookup process.

Place a record of the find probability to the front of the lookup table.

third, the orderly table lookup

Binary find (binary search)

    Average look-up length: Log (n+1)-1, pushed by the decision tree of the binary search tree. The worst performance and average performance of binary lookups is very close. Time complexity O (LGN).

Interpolation Lookup

    This method is improved on the basis of binary search, the core is the value of mid, the formula is:

MID = low + (Key-arr[low]) * (high-low)/(Arr[high]-arr[low])

As for why this improvement, because the key is small, then I tend to look at the low address in the table to find, and key large then we tend to look at the table's high address end.

The complexity of Time is also O (LGN).

Fibonacci Find

    Instead of using two points, the values of the Fibonacci sequence are used to narrow our range. Time complexity O (LGN).

four, linear index lookup

Dense index

Chunking Index

Inverted index

five or two fork sorting treevi. Balancing the search tree (AVL tree)

  Since binary sorting trees are generally not balanced, the lookup performance of binary sorting trees depends on the shape of the two-fork sort tree, so a balanced two-fork tree is constructed. The height of the left and right sub-tree is less than 1. So this tree is always balanced. Improved search efficiency.

The value of the left subtree of the nodes on the binary tree minus the depth of the right subtree is called the balance factor BF. As long as the absolute value of the balance factor of a node on a two-fork tree is greater than 1, the two-tree is unbalanced.

The construction process of a balanced binary tree:

In short, the imbalance is eliminated at the earliest moment.

The lookup time complexity is O (Logn), while the insertion and deletion time complexity is also O (logn).

Red-black tree, another balancing algorithm for binary sorting trees.

seven, multi-path search tree (b-Tree)

  Objective: To reduce the number of visits to external storage devices.

Definition: Multiple search books, each of its nodes can have more than two children tree, and each node can store multiple elements.

Block path lookup tree form:

2-3 Trees

2-3-4 Tree

B-Tree Balanced multi-path lookup tree

B + tree In order to solve the problem of all element traversal

The number of children with the largest nodes is called the order of B-trees, so 2-3 trees are 3-order B-numbers.

Hash table lookup (hash table)

Review of search algorithms for data structure review

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.