JAVA Implementation of Tree B

Source: Internet
Author: User

When I saw mit last week and <Introduction to algorithms>, I thought it was a bit difficult to implement B-tree. I could just practice it.

It took a day to write the script. It was very inefficient.

The logic is not complex, but many branches. The key is that the subscript is prone to errors. The deletion method in the book is not very clear, and it still takes some twists and turns. Before you start writing, the key is to understand how B-tree is implemented.

I implemented Insert first, because this method is simpler, and I also used the opportunity to deepen my understanding of B-tree. Then the deletion method is implemented.

Key comments are included in the source code (download. Insert is half written, and I found that I had a low-level error: The passing parameters of the method are chaotic. For example, insert () is in the node, so it must be called by a node, the method also requires passing a node as the parameter. In this way, one node can modify another node. If one node is required to be able to modify another node, you can set the method to static. If not, the parameters will be transmitted again. But I didn't change it. All the subsequent methods are in this mode:
Node. Method (node ,...)

I just did a simple test. The insert method inserts values [1, 18] in sequence. Each insert value is compared with the tree referenced in [1] to see if it is the same. The test of Delete is to first build a tree containing the value [1, 18], then delete and verify from 18 to 1.

The hands are sour ..

Refer:

1. animation of 2-3-4 tree, source code also available. This website provides a graphical interface to demonstrate 2-3-4 trees

2. Collection of btree info

3. MIT <Introduction to algorithms>

For more information, see the method provided in section 3. I added some notes:

Insert

B-TREE-SPLIT-CHILD(x, i, y)
1 z ← ALLOCATE-NODE()
2 leaf[z] ← leaf[y]
3 n[z] ← t - 1
4 for j ← 1 to t - 1
5 do keyj[z] ← keyj+t[y]
6 if not leaf [y]
7 then for j ← 1 to t
8 do cj[z] ← cj+t[y]
9 n[y] ← t - 1
10 for j ← n[x] + 1 downto i + 1
11 do cj+1[x] ← cj [x]
12 ci+1[x] ← z
13 for j ← n[x] downto i
14 do keyj+1[x] ← keyj[x]
15 keyi[x] ← keyt[y]
16 n[x] ← n[x] + 1
17 DISK-WRITE(y)
18 DISK-WRITE(z)
19 DISK-WRITE(x)


B-TREE-INSERT(T, k)
1 r ← root[T]
2 if n[r] = 2t - 1
3 then s ← ALLOCATE-NODE()
4 root[T] ← s
5 leaf[s] ← FALSE
6 n[s] ← 0
7 c1[s] ← r
8 B-TREE-SPLIT-CHILD(s, 1, r)
9 B-TREE-INSERT-NONFULL(s, k)
10 else B-TREE-INSERT-NONFULL(r, k)


B-TREE-INSERT-NONFULL(x, k)
1 i ← n[x]
2 if leaf[x]
3 then while i ≥ 1 and k < keyi[x]
4 do keyi+1[x] ← keyi[x]
5 i ← i - 1
6 keyi+1[x] ← k
7 n[x] ← n[x] + 1
8 DISK-WRITE(x)
9 else while i ≥ 1 and k < keyi[x]
10 do i ← i - 1
11 i ← i + 1
12 DISK-READ(ci[x])
13 if n[ci[x]] = 2t - 1
14 then B-TREE-SPLIT-CHILD(x, i, ci[x])
15 if k> keyi[x]
16 then i ← i + 1
17 B-TREE-INSERT-NONFULL(ci[x], k)

Deletion

There areTwo Special CasesTo consider when deleting an element:

  1. The element in an internal node may be a separator for its Child Nodes
  2. Deleting an element may put it under the minimum number of elements and children.

Algorithm

  1. If the keyKIs in nodeXAndXIs a leaf, Delete the keyKFromX.

  2. If the keyKIs in nodeXAndXIs an internal Node, Do
    Following.

    1. If the childYThat precedesKIn NodeXHas at leastTKeys, then find the predecessorK'KIn
      The subtree rootedY. Recursively DeleteK', And replaceKByK'In
      X. (FindingK'And deleting it can be performed med in a single downward
      Pass.), that is,Replace K with the largest key of the Left subtree(?????? If y is a leaf within T keys, after the deletion, Y has t-1
      Keys. Then, it's possible that an element is deleted from Y next time,
      Which result in Y's key size to be T-2 ,??? See Rule 3)

    2. Symmetrically, if the childZThat
      FollowsKIn NodeXHas at leastTKeys, then find the successorK'KIn the subtree rootedZ. Recursively DeleteK', And replaceKByK'InX.
      (FindingK'And deleting it can
      Be writable med in a single downward pass .),That is, replace K with the smallest key of the right subtree

    3. Otherwise, if bothYAndZHave onlyT-1 keys, mergeKAnd allZIntoY, So thatXLoses bothKAnd the pointerZ, AndYNow contains 2T-1 keys. Then, free
      ZAnd recursively DeleteKFromY. That is, merge the children, that is,Merge the two children

----- Borrow
An element from the children, otherwise, merge, to minimize
Operation on Delete, that is, only the key is seemed to be replaced in
The internal node (the Special Case 1)

  1. If the keyKIs not present in
    Internal NodeX, Determine the rootCi[X] Of the appropriate
    Subtree that must containK, IfKIs
    In the tree at all. IfCi[X] Has onlyT-1 keys, execute step 3A
    Or 3B as necessary to guarantee that we descend to a node containing at leastTKeys. Then, finish by recursing on the appropriate child
    X.(While traversing down)

    1. IfCi[X] Has onlyT-1 keys but has
      Immediate sibling with at leastTKeys, giveCi[X] An extra key by moving
      A key fromXDownCi[X], Moving a key fromCi[X]'S immediate left or
      Right sibling upX, And moving the appropriate child
      Pointer from the siblingCi[X].

    2. IfCi[X] And bothCi[X]'S immediate siblings haveT-1
      Keys, mergeCi[X]
      One sibling, which involves moving a key fromXDown
      The new merged node to become the median key for that
      Node.

------ Borrow
An element from the sibling, otherwise, merge the sibling and the key
Between the sibling and CI [X]. That is, to ensure the lower bound
Every node (the special case 2)

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.