[Data structure] Chapter 1 search! (Binary Search Tree bst avl Tree B-(+) tree dictionary tree HASH table ),

Source: Internet
Author: User

[Data structure] Chapter 1 search! (Binary Search Tree bst avl Tree B-(+) tree dictionary tree HASH table ),

Difficult to take notes... I was planning to use the result of one day before and after the result was dragged for five days.

§ 9. 1 static search table9.1.1 search for a sequence table

Complexity O (n)

9.1.2 query of ordered tables

If the table is monotonous, you can useBinary Search. Complexity O (logn)

9.1.3 search for static tree tables

See
Http://blog.csdn.net/area_52/article/details/43795837

9.1.4 search an index sequence table

Create index table search

§ 9. 2 dynamically search tables

Dynamic table search features,The table structure is dynamically generated during the search process. For a given key, if the table contains a record whose key keyword is equal to the key, the query is successful and the record whose key keyword is equal to the key is inserted.

9.2.1 binary sorting tree and balanced binary tree

① Binary sorting tree and its search process
Binary sorting tree (BST)Or an empty tree, or a binary tree of the following nature:
(1) if its left subtree is not empty, the value of all nodes on the left subtree is smaller than the value of its root node.
(2) if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node.
(3) Its left and right subtree is also a binary sorting tree.
The search code is as follows:

/** Find */Position Find (ElementType X, BinTree BST) {if (! BST) return NULL; // empty tree if (X> BST-> Data) return Find (X, BST-> Right); if (X <BST-> Data) return Find (X, BST-> Left); return BST;/** found */}
/** Find */Position Find (ElementType X, BinTree BST) {while (BST) {if (X> BST-> Data) {BST = BST-> Right;} else if (X <BST-> Data) {BST = BST-> Left;} else return BST; // found} return NULL; // search failed}
/** Recursively find the minimum value of the Binary Search Tree element */Position FindMin (BinTree BST) {if (! BST) return NULL; else if (! BST-> Left) return BST; // else return FindMin (BST-> Left) is the minimum value if no Left child exists; // continue searching along the Left branch}
/** Find the maximum element of the Binary Search Tree by iteration */Position FindMax (BinTree BST) {if (BST) {while (BST-> Right) BST = BST-> Right ;} return BST ;}

② Insert and delete a binary sorting tree
(1) Insert a node in BST
The newly inserted node must be a newly added leaf node, and is the Left or Right child of the last node accessed on the path when the search fails. So you can change the search algorithm.

A disordered sequence can be converted into an ordered sequence by constructing a binary sorting tree. The process of constructing a tree is the process of sorting a disordered sequence.

(3) Delete a node in BST
There are three solutions:
1. If the leaf node is deleted, delete it directly.
2. The deleted node only has the left or right subtree, so you can directly Delete the subtree and put it on its parent.
3. The left and right subtree of the deleted node are not empty. There are two methods: one is to connect the left subtree, And the right subtree is placed on the far right of the Left subtree. Second, replace it with a direct precursor (successor), and then delete its direct precursor (successor) (see P230 for details)
The deletion code is as follows:

Status DeleteBST (BiTree & T, KeyType key) {// If the binary tree T contains a data element whose key is equal to the key, // This Data Element Node is deleted, returns TRUE; otherwise, returns FALSE if (! T) return FALSE; // if (EQ (key, T-> data. key) return Delete (T); // locate the data element else if (LT (key, T-> data. key) return DeleteBST (T-> lchild, key); else return DeleteBST (T-> rchild, key);} Status Delete (BiTree & p) {// Delete the node p from the binary sorting tree, and repeat its left or right subtree if (! P-> rchild) {// if the right subtree is empty, you only need to re-connect its left subtree q = p; p = p-> lchild; free (q );} else if (! P-> lchild) {// just re-connect the right subtree q = p; p = p-> rchild; free (q );} else {// left and right subtree are not empty q = p; s = p-> lchild; while (s-> rchild) {// turn left, then, right-to-end q = s; s = s-> rchild;} p-> data = s-> data; // s points to the precursor of the deleted node if (q! = P) q-> rchild = s-> lchild; // re-connect * The right subtree else q-> lchild = s-> lchild; // re-connect * The left subtree of q delete s;} return TRUE ;}

③ Search and analysis of binary sorting tree
The ASL calculation finds that if the binary tree tends to be "balanced", its ASL can be effectively reduced and the efficiency is improved. In some cases, it is still necessary to perform "balancing" processing in the process of forming a binary sorting tree, which is called a binary balancing tree.

④ Balanced binary tree (AVL Tree)
A balanced binary tree is also called an AVL Tree. It is either an empty tree or a binary tree of the following nature: Its left and right subtree are both balanced binary trees, the absolute value of the depth difference between the left subtree and the right subtree cannot exceed 1.
IfBalance factor BFDefined as the depth of the Left subtree of the node minus the depth of its right subtree, the balance factor of all nodes on the balanced binary tree can only be-1, 0, 1. As long as the absolute value of the balance factor of one node on a binary tree is greater than 1, the binary tree is unbalanced.
The number of vertices in the AVL tree on the n layer conformsFibonacci.
A layer-1 AVL Tree requires at least one node, a layer-2 AVL Tree requires at least two nodes, a layer-3 requires at least four nodes, and a layer-4 requires at least seven nodes...
And so onMinimum node of n layer = least node of (n-1) layer + least node of (n-2) layer + 1

In the AVL Tree, inserting a new node may lead to a loss of balance. In this case, we need to find the pointer to the smallest root node that loses the balance, and then adjust the link between the nodes in the subtree to make it a new balance subtree. When the minimum subtree without balance is adjusted to the balance subtree, the entire binary tree becomes a balanced binary tree.
The minimum subtree that loses the balance refers to the subtree that uses the node closest to the inserted node and has an abnormal balance factor (absolute value> 1) as the root.
Assume that A is used to represent the root node of the minimum subtree without balance, the following four operations are available for adjusting the subtree:

(1) RR Rotation
The new node is inserted in the right tree of the right subtree of A (whether left or right), then the right subtree of A is put up as the root of the tree, the left subtree of the original right subtree of A is inserted on the right of.

(2) LL Rotation
The new node is inserted in the left Tree of the Left subtree of A (whether inserted in the left or right), then the left subtree of A is raised as the root of the tree, the right subtree of the original left subtree of a gets the left subtree of.

(3) RL Rotation
The new node is inserted in the left Tree of the right subtree of A (whether inserted in the left or right). At this time, the focus is to balance the three nodes of a c d. D is big in the middle of the three nodes, so D is used as the root, A is used as the left subtree, and C is used as the right subtree. Locate the remaining nodes in order of size.

(4) LR Rotation
The new node is inserted in the right tree of the Left subtree of A (whether inserted in the left or right). At this time, the focus is to balance the three nodes of a B D. D is big in the middle of the three nodes, so D is used as the root, B is used as the left subtree, and A is used as the right subtree. Locate the remaining nodes in order of size.

⑤ Analysis of the Balance Tree search
The search time complexity isO (logn)

9.2.2 B-tree and B + tree

Tree B to be supplemented ....
① B-tree and its search

② B-tree search and analysis

③ B-Tree insertion and Deletion

④ B + tree

9.2.3 key tree

The key tree is also called the number search tree (DST). It isDegree> = 2In the tree, each node in the tree does not contain one or several keywords, but only contains symbols that comprise keywords.
For example,If the keyword is a numeric value, the node contains only one digit. If the keyword is a word, the node contains only one letter.
This kind of book makes it easy to search for certain types of keywords.
It is a key tree. A string consisting of node characters from the root to the leaf node PATH represents a keyword.

We agree that the key tree isOrdered Tree
The key tree has two storage structures, resulting in two key trees

① Dual-tree in the expression of the child brother
Each branch node includes three domains.
A character in the symbol domain that stores keywords.
The first domain stores the pointer to the first child root.
The next domain stores the pointer to the right brother.
At the same time, the first domain of the leaf node stores the pointer to the keyword record.

Dual-tree implementation code

# Define MAXKEYLEN 16 // Maximum length of the keyword typedef struct {char ch [MAXKEYLEN]; // keyword int num; // keyword length} KeysType; // keyword type: typedef enum {LEAF, BRANCH} NodeKind; // node type: {LEAF, BRANCH} typedef struct DLTNode {char symbol; struct DLTNode * next; // The pointer to the sibling node NodeKind kind; union {Record * infoptr; // The Record Pointer of the leaf node struct DLTNode * first; // The Child chain pointer of the branching node };} DLTNode, * DLTree; // double-stranded Tree Type

Double-tree search code

Record * SearchDLTree (DLTree T, KeysType K) {// searches for records with a keyword of K in non-empty double-link tree T. If so, // returns a pointer to the Record, otherwise, the NULL pointer p = T-> first; I = 0; // initialize while (p & I <K. num) {while (p & p-> symbol! = K. ch [I]) p = p-> next; // find the I-bit if (p & I <K. num-1) p = p-> first; // prepare to find the next bit + + I;} // find the end if (! P) return NULL; // else return p-> infoptr; // Search successful} // Search DLTree

The largest d of each node in the key tree is related to the "base" of the keyword. If the keyword is a word, d = 27; if the keyword is a value, d = 11. The depth of the key tree depends on the number of characters or digits in the keyword.

② Trie Tree represented by multiple linked lists (Dictionary tree)
If multiple linked lists of trees are used to represent the key tree, each node of the tree should contain d pointer fields. At this time, the key tree is also called the Trie (same as Try) tree or the Prefix Tree. The Trie tree can be viewedFinite State Automation.

For example, a Trie tree

The nature of the Trie tree:
① The root node does not contain any character. Each node except the root node only contains one character.
② From the root node to a node, the character passing through the path is connected to the string corresponding to the node.
③ All subnodes of each node contain different characters.

The search process on the Trie tree is as follows:
Starting from the root node, the pointer corresponding to the given value goes down layer by layer until the leaf node. If the keywords in the leaf node are the same as the given value, the query is successful, if the pointer corresponding to the given value in the branch node is null, the query fails.
The search algorithm code is as follows:

Typedef struct TrieNode {NodeKind kind; union {struct {KeysType K; Record * infoptr;} lf; // leaf node struct {TrieNode * ptr [27]; int num;} bh; // branch node };} TrieNode, * TrieTree; Record * SearchTrie (TrieTree T, KeysType K) {// In the dictionary tree T, find the record for (p = T, I = 0; // find p & p-> kind = BRANCH & I <K. num; // * p is the branch node p = p-> bh. ptr [ord (K. ch [I])], ++ I ;); // ord calculates the number of characters in the alphabet. if (p & p-> kind = LEAF & p-> lf. K = K) return p-> lf. infoptr; // else return NULL; // search failed} // SearchTrie
§ 9. 3 hash table9.3.1 what is a hash table

Map a set of keywords to a finite continuous address set (range) based on the hash function H (key) and the method for dealing with conflicts, the keyword "image" in the address set is used as the storage location of the record in the table.Hash table. This image process is calledHash Table CreationOrHash, The resulting storage location nameHash addressOrHash address.

9.3.2 construction of Hash Functions

① Direct addressing
A linear function value of a key or key is a hash address. That is
H (key) = key or H (key) = A * key + B
Direct addressing is rarely used

② Digital Analysis
A hash address is composed of several digits of the key. (For example, the ID card can use the last four digits to form a hash address)

③ China and France
The number of digits in the center of the square of the key is the hash address (random)

④ Folding Method
Divide the key into several parts with the same number of digits (the digits in the last part can be different), and then overlay these parts and use them as the hash address.
For example, H (123456) = 12 + 34 + 56 = 102 can affect the result and increase randomness.

※⑤Except the remaining remainder
The number of p (usually the prime number) whose keyword is not greater than the m length of the hash table is obtained as the hash address. That is
* H (key) = key % p (p <= m )*

⑥ Random Number Method
Usually used for key length difference

9.3.3 conflict Handling Methods

① Open address Method
That isLOC = (H (key) + di) % m (I = 1, 2 ,..., M-1)
Optional
(1)Linear detection and re-partitioning (most commonly used)
(Di = 1, 2 ,...., M-1)
(2) split by square Detection
(Di = 1 ^ 2,-1 ^ 2 ^ 2,-2 ^ 2 ,..., K ^ 2,-k ^ 2) (k <= m/2)

② Rehash
Create another hash function based on the addressing of the hash function.
Hi = RHi (key) I = 1, 2 ,..., K
RHi is a different hash function, that is, when a synonym generates an address conflict, calculate the address of another hash function until the conflict does not occur. This method is not easy to generate "aggregation", but increases the computing time.

Link address method (Zipper method)(Second commonly used)
To be retained, all locations are in the form of a linked list. The initial state is a null pointer. All records whose hash function is I are inserted at the end of the I-th linked list.

△④ Create a public overflow Zone

9.3.4 query and Analysis of hash tables

The process of searching a hash table is basically the same as that of creating a hash table.
The fill factor α of the hash table is defined
α = number of records in the table/length of the hash table
The α function for the average query length of the hash table, instead of the n function. Therefore, no matter how large n is, we can always select a suitable fill factor to limit the average search length to a certain range.

[Chapter 4 has been completed]

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.