2-3-4树

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

2-3-4树

时间: 2024-08-30 07:23:15

2-3-4树的相关文章

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

【树4】二叉树的遍历

简介 遍历二叉树就是按照某种顺序,将树中的结点都枚举一遍,且每个结点仅仅访问一次.因为树不是线性的结构,遍历不像线性表那样简单,因此他的遍历需要特点的算法来完成. 从某种角度讲,对二叉树的遍历就是将树形结构转换为线性结构的操作. 二叉树的遍历方法主要有如下几种: 先序遍历:先访问root结点,再先序遍历左子树,再先序遍历右子树. 中序遍历:先中序遍历左子树,再访问root结点,再中序遍历右子树. 后序遍历:先后序遍历左子树,再后序遍历右子树,再访问root结点. 层遍历:从上到下,从左到右,一层

关于左偏树的一些东东

大概所有的预备知识这里都有https://baike.baidu.com/item/%E5%B7%A6%E5%81%8F%E6%A0%91/2181887?fr=aladdin 例题1:洛谷 P3377 [模板]左偏树(可并堆) 383通过 1.2K提交 题目提供者HansBug 站长团 标签 难度提高+/省选- 时空限制1s / 128MB 提交 讨论 题解 最新讨论更多讨论 加了路径压缩就WA,路过dal… 左偏树用指针写会MLE吗..… m,n写反了也可以过,数据有… 哪位大神有pbds库

ZJOI 2008 树的统计

ZJOI2008 树的统计 题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节点的权值和 注意:从点u到点v的路径上的节点包括u和v本身 输入输出格式 输入格式: 输入文件的第一行为一个整数n,表示节点的个数. 接下来n – 1行,每行2个整数

luoguP2590 [ZJOI2008]树的统计 [树链剖分] [TLE的LCT]

题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节点的权值和 注意:从点u到点v的路径上的节点包括u和v本身 输入输出格式 输入格式: 输入文件的第一行为一个整数n,表示节点的个数. 接下来n – 1行,每行2个整数a和b,表示节点a和节点b之

(POJ 3067) Japan (慢慢熟悉的树状数组)

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29295   Accepted: 7902 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coas

[poj2104]可持久化线段树入门题(主席树)

解题关键:离线求区间第k小,主席树的经典裸题: 对主席树的理解:主席树维护的是一段序列中某个数字出现的次数,所以需要预先离散化,最好使用vector的erase和unique函数,很方便:如果求整段序列的第k小,我们会想到离散化二分和线段树的做法, 而主席树只是保存了序列的前缀和,排序之后,对序列的前缀分别做线段树,具有差分的性质,因此可以求任意区间的第k小,如果主席树维护索引,只需要求出某个数字在主席树中的位置,即为sort之后v中的索引:若要求第k大,建树时反向排序即可 1 #include

【BZOJ 3551】[ONTAK2010] Peaks加强版 Kruskal重构树+树上倍增+主席树

这题真刺激...... I.关于Kruskal重构树,我只能开门了,不过补充一下那玩意还是一棵满二叉树.(看一下内容之前请先进门坐一坐) II.原来只是用树上倍增求Lca,但其实树上倍增是一种方法,Lca只是他的一种应用,他可以搞各种树上问题,树上倍增一般都会用到f数组. |||.我们跑出来dfs序就能在他的上面进行主席树了. IV.别忘了离散. V.他可能不连通,我一开始想到了,但是我觉得出题人可能会是好(S)人(B),但是...... #include <cstdio> #include

【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依次更新这log位,如果最高位依然有进位,那么找到最高位后面的第一个0,将中间的所有1变成0,那个0变成1.这个显然要用到线段树,但是复杂度是nlog2n的,肯定过不去. 于是我在考场上yy了一下,这log位是连续的,我们每次都要花费log的时间去修改一个岂不是很浪费?我们可以先在线段树上找到这段区间