Basic data structure notes (ii) "Min"

Source: Internet
Author: User

Dynamic storage Management: Partner Systems Allocation Memory algorithm idea:

When the program presents a memory allocation request of size n, first find the sub-table in the available table that matches the size of N.
If there is an idle sub-table node for 2^ (k-1) < n≤2^k-1: Any node in the child table is assigned;
If there is no free sub-table node for 2^ (k-1) < n≤2^k-1: An idle node is found in the child table with node size 2^k, half of which is allocated to the program, and half of the remaining is inserted into the child table with node size 2k-1.

Note: In the case of memory allocation requests with size n (2^ (k-i-1) < n≤2^ (k-i) -1,i=1,2,..., k-1), if all sub-tables that are less than 2^k are empty (no idle nodes), then it is also necessary to find an idle node from a child table of size 2^k, where 2^ ( k-i) A small portion is allocated to the user, and the remainder is split into several nodes and inserted into the corresponding sub-table respectively.

Recovery algorithm:

When a program frees a block that is occupied, the system inserts the new free block into the available free table and needs to consider merging into chunks. In a partner system, only the two sub-blocks of each partner are merged when they are idle, and even if there are two contiguous, equal-sized free blocks, they are not merged if they are not ' partners ' (split from the same chunk).

    1. determine the partner idle block:
      Set P is the first address of an idle block of size 2^k, and P MOD 2^ (k+1) = 0, the first address is the two free blocks of P and p+2^k "mutual partners".
      The first address is a memory block with a p size of 2^k, and its partner's first address is:
      If P MOD 2^ (k+1) = 0 "Previous", then Buddy (p,k) =p+2^k;
      Otherwise p MOD 2^ (k+1) = 2^k "After one", then Buddy (p,k) =p-2^k;
    2. The idea of recycling algorithm
      ⑴ to determine if the two free blocks of their mutual partners are empty:
      If not empty, only the free blocks to be reclaimed are inserted directly into the corresponding sub-table; otherwise ⑵;
      ⑵ follow these steps to merge the free blocks:
      Find its partner in the corresponding sub-table and delete it;
      Merge two free blocks;
      ⑶ repeats the ⑵ until the partner of the merged free block is not a free block.
Balanced two-pronged tree

A balanced binary sort tree with a depth of H has a minimum number of nodes in NH, which is known by the nature of the balanced binary sort tree:
n0=0,n1=1,n2=2, nh= nh-1+nh-2

Index lookup: B-Tree

The

Balanced binary sorting tree is convenient for dynamic search, so it is a feasible option to organize the index table with the balanced binary sorting tree. When used in large databases, all data and indexes are stored in external memory, therefore, it involves the frequent exchange of data between the inner and the external memory, which becomes the bottleneck of restricting the dynamic search speed. If the node of a two-fork tree is used as a data exchange unit between external memory, it is not tolerated to ㏒2n the disk on average when looking for a given keyword, so you must choose an index organization that minimizes the number of disk I/O times. The size of the tree nodes is as close to the size of the page as possible. The
b_ tree is primarily used in the file system, where the size of each node in the B_ tree is one disk page, and the number of keywords contained in the nodes and their children depends on the size of the page. b_ tree with a degree m is called M-order b_ tree
A M-order b_ tree, or an empty tree, or a M-fork tree that satisfies the following properties:
⑴ root node or leaf, or at least two subtrees tree, at most m subtrees tree;
⑵ root node. All non-terminal nodes have at least m/2 "rounded up" subtrees tree, with at most m subtrees tree;
⑶ all leaf nodes are on the same layer of the tree;
Set N is the number of keywords in the node, and M/2 "lower limit" -1≤n≤m-1,n+1 the number of subtrees.

    • B-Tree Delete
      Delete a keyword k on the b_ tree, first find the node n where the keyword is located, and then delete the keyword k in n.
      1). If n is not a leaf node, and k is the I keyword in n, then the pointer Ai-1 the maximum keyword (or minimum keyword) k ' in the pointed subtree (k), then delete K ', and K ' must be on the leaf node. 9-15 (b), delete the keyword h, replace the position of H with the keyword G, and then delete the keyword g from the leaf node.
      2). The case of deleting a keyword from a leaf node is:
      ⑴ if the number of keywords in node n >m/2-1 "take the upper limit": Delete the keyword K directly in the node.
      ⑵ if the number of keywords in node n =m/2-1 "Upper limit": If the number of keywords in the left (right) sibling node of node n >m/2-1 "Upper Limit", the maximum (or minimum) keyword in the left (or right) sibling node of node n is moved to its parent node, and the parent node is greater than (or less than) And the keyword next to the top move down to node n;
      ⑶ the key words in the node n and its sibling nodes =m/2-1 "Take the upper limit": Delete the keyword in node n, and then combine the keyword in node n, the pointer and its sibling node, and a keyword in the parent node of the split to a node, so that the number of keywords in the parent node
B + Tree

In the actual file system, the B_ tree is basically not used, but a variant of the b_ tree is used, called the M-order B + tree. Its main difference from the B_ tree is that it stores records in leaf nodes. In a B + tree, all non-leaf nodes can be viewed as indexes, and the keywords are used as "demarcation keywords" to define the subtree in which a key word is recorded. The main differences between a M-order B + tree and M-order b_ tree are:
⑴ If a node has n subtrees tree, it must contain n keywords;
⑵ all the leaf nodes contain the key information of all the records and the pointers of these key records, and the leaf nodes are linked from small to large by the size of the keywords;
⑶ all non-leaf nodes can be viewed as part of the index, and the nodes contain only the largest (or smallest) keywords in the root node of their subtree.
Compared with the b_ tree, the B + tree can be searched randomly from the root node by keyword, and it can be searched sequentially from the minimum keywords, by the link order of leaf nodes. The process of randomly locating, inserting, and deleting on B + trees is basically similar to the b_ tree.
When searching randomly on a B + tree, the key that is not the leaf node equals the given k value, does not terminate, but continues downward until the leaf node (only the leaf node stores the record), that is, whether the search succeeds or not, has walked a path from the root node to the leaf node.
The insertion of B + trees is performed only on the leaf nodes. When the number of keywords in the leaf node is greater than M, "split" is two nodes, two nodes contain the number of keywords (m+1)/2 "Lower limit" and (m+1)/2 "Take the upper limit", and the two nodes in the largest keyword promoted to the parent node, Used to replace the keyword in the parent node of the original node. Ascending stepfather nodes can also be split, and so on.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Basic data structure notes (ii) "Min"

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.