博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Algorithm,DS,2-3-4 tree
阅读量:5112 次
发布时间:2019-06-13

本文共 5220 字,大约阅读时间需要 17 分钟。

 


 

2–3–4 tree

From Wikipedia, the free encyclopedia
  (Redirected from  )
 

In , a 2–3–4 tree (also called a 2–4 tree) is a self-balancing that is commonly used to implement . The numbers mean a  where every  with children () has either two, three, or four child nodes:

  • a 2-node has one , and if internal has two child nodes;
  • a 3-node has two data elements, and if internal has three child nodes;
  • a 4-node has three data elements, and if internal has four child nodes.

2–3–4 trees are  of order 4 (); like B-trees in general, they can search, insert and delete in (log n) time. One property of a 2–3–4 tree is that all external nodes are at the same depth.

2–3–4 trees are an  of , meaning that they are equivalent data structures. In other words, for every 2–3–4 tree, there exists at least one red–black tree with data elements in the same order. Moreover, insertion and deletion operations on 2–3–4 trees that cause node expansions, splits and merges are equivalent to the color-flipping and rotations in red–black trees. Introductions to red–black trees usually introduce 2–3–4 trees first, because they are conceptually simpler. 2–3–4 trees, however, can be difficult to implement in most programming languages because of the large number of special cases involved in operations on the tree.  are simpler to implement, so tend to be used instead.

Contents

 
 [] 

Properties[ | ]

  • Every node (leaf or internal) is a 2-node, 3-node or a 4-node, and holds one, two, or three data elements, respectively.
  • All leaves are at the same depth (the bottom level).
  • All data are kept in sorted order.

Insertion[ | ]

To insert a value, we start at the root of the 2–3–4 tree(参照红黑的增加看uncle结点,以4个结点为单位操作,uncle红则向上迭代,uncle黑可终结)

  1. If the current node is a 4-node:
    • Remove and save the middle value to get a 3-node.
    • Split the remaining 3-node up into a pair of 2-nodes (the now missing middle value is handled in the next step).
    • If this is the root node (which thus has no parent):
      • the middle value becomes the new root 2-node and the tree height increases by 1. Ascend into the root.
    • Otherwise, push the middle value up into the parent node. Ascend into the parent node.
  2. Find the child whose interval contains the value to be inserted.
  3. If that child is a leaf, insert the value into the child node and finish.
    • Otherwise, descend into the child and repeat from step 1.

Example[ | ]

To insert the value "25" into this 2–3–4 tree:

  • Begin at the root (10, 20) and descend towards the rightmost child (22, 24, 29). (Its interval (20, ∞) contains 25.)
  • Node (22, 24, 29) is a 4-node, so its middle element 24 is pushed up into the parent node.
  • The remaining 3-node (22, 29) is split into a pair of 2-nodes (22) and (29). Ascend back into the new parent (10, 20, 24).
  • Descend towards the rightmost child (29). (Its interval (24, ∞) contains 25.)
  • Node (29) has no leftmost child. (The child for interval (-∞, 29) is empty.) Stop here and insert value 25 into this node.

Deletion[ | ]

Consider just leaving the element there, marking it “deleted,” possibly to be re-used for a future insertion.

To remove a value from the 2–3–4 tree:

  1. Find the element to be deleted.
    • If the element is not in a leaf node, remember its location and continue searching until a leaf, which will contain the element’s successor, is reached. The successor can be either the largest key that is smaller than the one to be removed, or the smallest key that is larger than the one to be removed. It is simplest to make adjustments to the tree from the top down such that the leaf node found is not a 2-node. That way, after the swap, there will not be an empty leaf node.
    • If the element is in a 2-node leaf, just make the adjustments below.

Make the following adjustments when a 2-node – except the root node – is encountered on the way to the leaf we want to remove:

  1. If a sibling on either side of this node is a 3-node or a 4-node (thus having more than 1 key), perform a rotation with that sibling:
    • The key from the other sibling closest to this node moves up to the parent key that overlooks the two nodes.
    • The parent key moves down to this node to form a 3-node.
    • The child that was originally with the rotated sibling key is now this node's additional child.
  2. If the parent is a 2-node and the sibling is also a 2-node, combine all three elements to form a new 4-node and shorten the tree. (This rule can only trigger if the parent 2-node is the root, since all other 2-nodes along the way will have been modified to not be 2-nodes. This is why "shorten the tree" here preserves balance; this is also an important assumption for the fusion operation.)
  3. If the parent is a 3-node or a 4-node and all siblings are 2-nodes, do a fusion operation with the parent and an adjacent sibling:
    • The adjacent sibling and the parent key overlooking the two sibling nodes come together to form a 4-node.
    • Transfer the sibling's children to this node.

Once the sought value is reached, it can now be placed at the removed entry's location without a problem because we have ensured that the leaf node has more than 1 key.

Deletion in a 2–3–4 tree is O(log n), assuming transfer and fusion run in constant time ( O(1) ).

See also[ | ]

转载于:https://www.cnblogs.com/threef/p/3294482.html

你可能感兴趣的文章
输入10个整数,将它们从大到小排序后输出。
查看>>
poj1178 floyd+枚举
查看>>
A2-01-02.Install MySQL
查看>>
Mybatis深入之事务管理
查看>>
Win7 64位 php-5.5.13+Apache 2.4.9+mysql-5.6.19 配置
查看>>
Android Bluetooth Stack: Bluedroid(五岁以下儿童):The analysis of A2DP Source
查看>>
android WebView总结
查看>>
选项卡登录
查看>>
python目录
查看>>
django+uwsgi+nginx+sqlite3部署+screen
查看>>
Andriod小型管理系统(Activity,SQLite库操作,ListView操作)(源代码下载)
查看>>
C#网络爬虫
查看>>
CentOS 6及7 丢失root密码解决方案
查看>>
在Server上得到数据组装成HTML后导出到Excel。两种方法。
查看>>
用PowerShell脚本删除SharePoint 的 Page中的WebPart
查看>>
VMware网络设置
查看>>
浅谈项目需求变更管理
查看>>
经典算法系列一-快速排序
查看>>
工作中的优化之数字键盘优化
查看>>
设置java web工程中默认访问首页的几种方式
查看>>