2-3-4树(1)概念
2010-10-11 11:08 Clingingboy 阅读(1572) 评论(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
如下图
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:
- {30,35}<50
- 50<{55}<75
- 75<{78}<95
- 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.
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.
如下图
- C(104)被移到新的节点中
- B(92)被移动到父节点
- A(83)保持不变
- 断开右侧两个子节点({97,99},{112}),并与新节点连接(104) ,将新节点与父节点连接
经过上面三部,节点分裂后如下,在此情况下,底层子节点均未满,就不需要再分裂了
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.
如下图
- 创建了2个新节点
- C(成为兄弟节点)
- B成为新的根节点,并成为A,C的父节点
- A保持不变
- 右侧2个节点断开与C连接,左侧2个节点保持不变
在分裂后(未插入)如下图
Demo(一个插入过程)
- 70,50,30
- 40
- 20,80
- 25,90
- 75
- 10
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现