2-3-4樹

來源:互聯網
上載者:User

標籤:des   style   class   blog   http   ext   

2-3-4 樹在電腦科學中是階為 4 的B樹。

大體上同B樹一樣,2-3-4 樹是可以用做字典的一種自平衡資料結構。它可以在O(log n)時間內尋找、插入和刪除,這裡的 n 是樹中元素的數目。

2-3-4 樹在多數程式設計語言中實現起來相對困難,因為在樹上的操作涉及大量的特殊情況。紅/黑樹狀結構實現起來更簡單一些,所以可以用它來替代。

(http://en.wikipedia.org/wiki/2%E2%80%933%E2%80%934_tree)

 In a binary tree, each node has one data item and can have up to two children. If we allow more data items and children per node, the result is a multiway tree.

  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:
1. A node with one data item always has two children
2. A node with two data items always has three children
3. A node with three data items always has four children

  In a 2-3-4 tree, on the other hand, nodes with a single link are not permitted. A node with one data item must always have two links, unless it‘s a leaf, in which case it has no links.
(因為小於4就在一個節點中,大於4就會分裂)
(二叉樹中,節點最多有兩個子節點的連結。它當然可以只有一個連結,指向它的左子節點或右子節點。它的另一個連結可以是null值。然而,在2-3-4樹中,不允許只有一個連結。有一個資料項目的節點必須總是保持有兩個連結,除非它是分葉節點,在那種情況下沒有連結。
有兩個連結的節點稱為2-節點,有3個連結的稱為3-節點,有四個節點的稱為4-節點,但沒有成為1-節點的節點)

Insertion
  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.
(新的資料項目總是插在分葉節點裡,在樹的最底層。如果插入到有子節點的節點裡,子節點的編號就要發生變化以此來保持樹的結構,這保證了節點的子節點比資料項目多1.)

  the process begins by searching for the appropriate leaf node. 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.

Insertion may involve moving one or two other items in a node so the keys will be
in the correct order after the new item is inserted. In this example the 23 had to be
shifted right to make room for the 18.

(3)
Node Splits
  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.
(如果往下尋找要插入位置的路途中,節點已經滿了,插入就變得複雜了。發生這種情況時,節點必須分裂(split)。正是這種分裂過程保證了樹的平衡。這裡討論的2-3-4樹的是一種稱為自頂向下的(top-down)2-3-4樹,因為是在向下找到插入點的路途中節點發生分裂。)

遇到滿節點就分裂,

Let’s name the data items in the node that’s about to be split A, B, and C. Here’s
what happens in a split. (We assume the node being split is not the root; we’ll
examine splitting the root later.)
• 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 rightmost two children are disconnected from the node being split and
connected to the new node.
An example of a node split is shown in Figure 10.5. Another way of describing a
node split is to say that a 4-node has been transformed into two 2-nodes.

FIGURE 10.5 Splitting a node.
Notice that the effect of the node split is to move data up and to the right. It is this
rearrangement that keeps the tree balanced.
Here the insertion required only one node split, but more than one full node may be
encountered on the path to the insertion point. When this is the case, there will be
multiple splits.

 

Splitting the Root
When a full root is encountered at the beginning of the search for the insertion
point, the resulting split is slightly more complicated:

search for the insertion
point, the resulting split is slightly more complicated:
• A new root is created. It becomes the parent of the node being split.
• A second new node is created. It 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.

Figure 10.6 shows the root being split. This process creates a new root that’s at a
higher level than the old one. Thus, the overall height of the tree is increased by
one. Another way to describe splitting the root is to say that a 4-node is split into
three 2-nodes.

FIGURE 10.6 Splitting the root.
Following a node split, the search for the insertion point continues down the tree. In
Figure 10.6, the data item with a key of 41 is inserted into the appropriate leaf.

 

 

Splitting on the Way Down
  Notice that, because all full nodes are split on the way down, a split can‘t cause an effect that ripples back up through the tree. The parent of any node that‘s being split is guaranteed not to be full, and can therefore accept data item B without itself needing to be split. Of course, if this parent already had two children when its child was split, it will become full. However, that just means that it will be split when the next search encounters it.
(注意,因為所有滿的節點都是在下行路途中分裂的,分裂不可能向回波及到樹上面的節點。任何要分裂節點的父節點肯定不是滿的,因此該節點不需要分裂就可以插入資料項目B。當然,如果父節點的子節點分裂時它已經有兩個子節點了,它就變滿了。但是,這隻是意味著下次尋找碰到它時才需要分裂。)

 

http://blog.sina.com.cn/s/blog_441997d20100ehar.html

http://blog.chinaunix.net/uid-23629988-id-3152495.html?page=2

http://blog.csdn.net/v_JULY_v/article/details/6531399

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.