代码改变世界

2-3-4树(1)概念

2010-10-11 11:08  Clingingboy  阅读(1568)  评论(0编辑  收藏  举报

 

2-3-4树概念

The 2, 3, and 4 in the name 2-3-4 tree refer to how many links to child nodes can
potentially be contained in a given node. For non-leaf nodes, three arrangements are
possible:
• A node with one data item always has two children
• A node with two data items always has three children
• A node with three data items always has four children
In short, a non-leaf node must always have one more child than it has data items. Or, to
put it symbolically, if the number of child links is L and the number of data items is D, then
L = D + 1

如下图

image

2-3-4树规则

• All children in the subtree rooted at child 0 have key values less than key 0.
• All children in the subtree rooted at child 1 have key values greater than key 0 but less
than key 1.
• All children in the subtree rooted at child 2 have key values greater than key 1 but less
than key 2.
• All children in the subtree rooted at child 3 have key values greater than key 2.

for exmaple:

  1. {30,35}<50
  2. 50<{55}<75
  3. 75<{78}<95
  4. 95<{100,105}

Searching

Finding a data item with a particular key is similar to the search routine in a binary tree.
You start at the root, and, unless the search key is found there, select the link that leads
to the subtree with the appropriate range of values.

Insertion(Rule:Split First,Then Insert)

注意:由于不是二叉树,所以并非插入一个数据项就会插入一个节点,

下面的例子,只插入了数据项,而非节点

New data items are always inserted in leaves, which are on the bottom row of the tree. If
items were inserted in nodes with children, then the number of children would need to be
changed
to maintain the structure of the tree, which stipulates that there should be one
more child than data items in a node.

scenario 1

If no full nodes are encountered during the search, insertion is easy. When the
appropriate leaf node is reached, the new data item is simply inserted into it.
shows a data item with key 18 being inserted into a 2-3-4 tree.

image

Node Splits(full node When Insert)

Insertion becomes more complicated if a full node is encountered on the path down to the
insertion point. When this happens, the node must be split. It's this splitting process that
keeps the tree balanced. The kind of 2-3-4 tree we're discussing here is often called a
top-down 2-3-4 tree because nodes are split on the way down to the insertion point.

scenario 1(assume the node being split is not the root)

• A new, empty node is created. It's a sibling of the node being split, and is placed to its
right.
• Data item C is moved into the new node.
• Data item B is moved into the parent of the node being split.
• Data item A remains where it is.
• The right most two children are disconnected from the node being split and connected
to the new node.

如下图

image[12]

  1. C(104)被移到新的节点中
  2. B(92)被移动到父节点
  3. A(83)保持不变
  4. 断开右侧两个子节点({97,99},{112}),并与新节点连接(104) ,将新节点与父节点连接

经过上面三部,节点分裂后如下,在此情况下,底层子节点均未满,就不需要再分裂了

image

Splitting the Root(在查找插入点时,若遇到满节点,就必须分裂)

• A new node is created that becomes the new root and the parent of the node being
split.
• A second new node is created that becomes a sibling of the node being split.
• Data item C is moved into the new sibling.
• Data item B is moved into the new root.
• Data item A remains where it is.
• The two rightmost children of the node being split are disconnected from it and
connected to the new right-hand node.

如下图

image[26]

  1. 创建了2个新节点
  2. C(成为兄弟节点)
  3. B成为新的根节点,并成为A,C的父节点
  4. A保持不变
  5. 右侧2个节点断开与C连接,左侧2个节点保持不变

在分裂后(未插入)如下图

image

 

Demo(一个插入过程)

  1. 70,50,30
  2. 40
  3. 20,80
  4. 25,90
  5. 75
  6. 10

image

image