2-3 Tree Insertion
Summary
TLDR本视频脚本详细介绍了如何将一系列值插入一个新创建的空2-3树。首先插入的值成为树的根节点,随后的值根据大小插入相应的叶子节点或内部节点。当节点满时,会进行分裂操作,将中间值提升到父节点,并重新调整子节点。通过这个过程,始终保持树的有效性,最终形成一个完整的2-3树。
Takeaways
- 🌟 插入序列值到一个新的空的2-3树中。
- 📌 初始时,根节点为空,插入的第一个值50将成为树的根节点和唯一的叶子节点。
- 🔄 插入第二个值60时,直接添加到现有节点中,因为它有空间。
- 🌐 插入第三个值70时,由于节点已满,需要进行拆分,并创建新的根节点。
- 🔢 插入值40和30时,它们都小于60,可以直接添加到左侧的叶子节点中。
- 🔄 当插入值20和10时,由于节点已满,需要进行拆分,并将中间值推到父节点。
- 🌳 插入值80和90时,它们都大于40且小于70,但插入90时需要再次拆分,并将80推到父节点。
- 🏁 最后插入值100,由于有足够的空间,它可以直接添加到现有的节点中。
- 📈 在整个插入过程中,始终保持2-3树的有效性。
- 🔑 每个节点最多包含两个键值和一个指针,最少包含一个键值。
- 🎯 插入操作始终在叶子节点进行,保持了2-3树的平衡性。
Q & A
二三树的初始状态是什么?
-二三树初始状态为空,根节点为null。
插入第一个值50时,会发生什么?
-插入第一个值50时,会创建一个新的节点,这个节点既是树的根节点也是唯一的叶子节点。
插入第二个值60后,60会被添加到哪里?
-插入第二个值60后,60会被添加到现有的节点中,因为该节点有空间容纳60。
插入第三个值70导致什么操作?
-插入第三个值70时,因为节点已满,需要进行分裂操作,并创建一个新的根节点。
在插入过程中,如何保持二三树的有效性?
-在插入过程中,每次插入后都会检查节点是否满足二三树的性质,即左子节点的值小于中间值,中间值小于右子节点的值。
插入值40和30时,它们会被添加到哪里?
-插入值40和30时,它们会被添加到包含60的节点的左侧,因为它们的值小于60。
当插入值20和10时,它们会被添加到哪里?
-插入值20和10时,它们会被添加到包含30的叶子节点中,因为它们的值小于30,且该节点有空间。
插入值10后,为什么需要进行操作?
-插入值10后,因为节点已满,需要将中间值20推上到父节点,并将10作为新的左孩子节点。
插入值80和90后,为什么需要进行节点分裂?
-插入值80和90后,因为节点已满,需要进行节点分裂,将中间值80推上到父节点,并创建新的节点来存放90。
最后一个值100是如何被添加到树中的?
-最后一个值100因为大于80,所以被添加到包含90的节点中,因为那里有空间。
在插入过程中,如何确保二三树的平衡?
-在插入过程中,每次插入新值或者进行节点分裂时,都会按照二三树的性质重新调整节点,确保树始终保持平衡。
Outlines
🌳 二三树插入过程概述
本段落介绍了二三树的基本概念和插入过程。首先,插入一个值为50的节点作为树的根节点。随后,按照二三树的性质,依次插入60和70,其中60成为叶子节点,而70由于无法放入现有节点,触发节点分裂,创建了一个新的根节点。接下来,继续插入40、30、20、10等值,每次插入都遵循二三树的规则,当节点满时进行分裂,将中间值提升到父节点。这个过程确保了在任何插入操作中,二三树始终保持有效状态。
📈 插入操作的详细步骤
此段落详细描述了插入操作的具体步骤。首先,插入80和90的值,80作为叶子节点的最后一个值,而90由于没有足够的空间,导致节点分裂,80被提升到父节点。最后,插入100的值,它直接插入到现有的节点中,完成了二三树的构建。整个过程中,每次插入都保持了二三树的性质,即左子节点的值小于中间值,中间值小于右子节点的值。
Mindmap
Keywords
💡二三树
💡根节点
💡叶子节点
💡节点分裂
💡插入
💡键
💡子节点
💡平衡
💡左孩子
💡右孩子
💡中值
Highlights
介绍了如何向一个空的2-3树中插入一系列值。
插入第一个值50时,会创建一个新的节点,它既是根节点也是唯一的叶子节点。
插入第二个值60时,它会成为现有节点的一部分,因为有足够的空间。
插入第三个值70时,由于节点已满,需要进行分裂,并创建一个新的根节点。
值60被提升为新根节点的一部分,而值50和70分别成为左右子节点。
插入值40时,它会被添加到左侧的叶子节点中,因为40小于60。
插入值30时,尽管它小于60,但当前叶子节点已满,需要进行节点分裂。
值40被提升到父节点,而值30成为新的左孩子节点。
每次插入操作后,2-3树都保持有效,左孩子小于T,右孩子大于T。
插入值20和10时,它们都小于40,并且可以被添加到现有的叶子节点中。
当插入值10时,由于没有更多空间,需要将中间值40提升到根节点,并创建两个新的节点。
值80和90的插入导致进一步的节点分裂,形成更复杂的树结构。
最后插入的值100很容易地被添加到现有的节点中,因为它大于40、80,且小于已存在的所有值。
整个插入过程中,始终保持了2-3树的特性,即每个节点最多有两个键值和一个子指针。
通过这个过程,展示了2-3树的动态插入和自我平衡的能力。
插入操作的每一步都详细说明了节点如何被创建、分裂和重新组织。
这个讲解提供了对2-3树数据结构的深入理解,特别是关于它的插入机制和维护树的平衡。
Transcripts
welcome to the comp 21:40 walkthrough on
two three tree insertion what we're
going to be doing is we're going to be
inserting a sequence of values into a
new empty two three tree
now since the tree is empty our initial
root node is going to be null so the
first value we insert 50 is going to
require a new node be created for it and
that's going to be both the root of our
tree and the only leaf node contained in
our tree now the second value we insert
60 is going to be inserted a leaf and
since there's already room for the value
60 inside of this node we're simply
going to add it to the existing node now
the third value we insert 70 is not
going to fit in this node so when we try
to insert it we're going to be required
to perform a split now in this case
because the node is also a root node a
split is going to require the creation
of a new root node for our tree now
we're going to take the middle of the
three values that we've inserted so in
this case 60 and push it up into the new
root node of the tree
the valley to the left of it is going to
in this case just stay where it is
because it's going to be the left child
and the value to the right of it is
going to be moved to the right child
the next value we insert 40 we're going
to look for the correct leaf node to
insert it into now since 40 is less than
60 it's going to be on the left of this
node which means that we're going to try
to insert it into this leaf node well as
it turns out there's room for it so all
we have to do is add the valley to that
node the next value we insert 30 is also
less than 60 so we're also going to try
to insert it into the same leaf node
however in this case there's not room
for it so what we're going to need to do
is we're going to need to take again the
middle of the three values and push it
up to the parent now in this case the
parent only contains one value so we
don't need to perform a split likely
like we did before
instead all we're going to need to do is
move the value 40 up into the parent
node now the value on the left the
smaller value again is going to be the
left child and the value on the right
the larger value is going to stay as the
right child so 40 gets moved up into the
parent just redraw this a little bit to
make it neater
and there we go now pause for a second
and take a look as you can see at any
point in this insertion process we're
always going to be left with a valid two
three tree so in other words here the
leftmost child 30 is less than the value
for T the value 50 is in between 40 and
60 and the value 70 the right child is
greater than 60 the next number we
insert 20 again is less than 40 so it's
going to be going in this child over
here since this is a leaf node and since
there is room for it we can simply add
the value 20 to this existing node for
the next value we insert 10 well once
again 10 is less than 40 so we're going
to be inserting it into this node as
well but once again in this case there's
no longer any room for it so we're going
to have to take the middle value of the
3 and try to push it up now as we can
see there's not going to be any room for
it in the parent so this is going to
require a little bit more work so let's
take a new node here that's going to be
the left child it's going to contain
10 the value 20 is going to get pushed
up into the parent in this case the root
but there's not going to be any room for
it now since this is a root node and
since we're splitting the root node it's
going to require the creation of two new
notes so the first of these two new
notes is going to contain the value
that's the smallest of these three
values in other words 20 and the middle
of these three values is going to get
pushed up into the new root node of our
tree so 40 is moved up into the new root
node of the tree its left child will be
20 hits right child will be this node
containing 60 and then 20 will have left
and right children 10 and 30 all right
the next value we insert 80 well 80 is
greater than 40 which means that it's
going to be on this side of the tree 80
is greater than 60 which means it's
going to be in this child and since
we're always inserting into a leaf node
well we found our way down to a leaf
node and there's room in this leaf node
so we can simply add 80 to this existing
node of the tree the next value we
insert 90 is also going to fit to this
same node
except of course there's no room for it
which means that we're going to have to
take the middle of three values once
again and push it up so 80 is going to
be pushed up into the parent the value
on the left will stay where it is the
value on the right the greater of the
three values 90 is going to be moved
into this new node that we create for it
and when 80 gets pushed up into the
parent well this node over here is now
going to have three children and let's
just redraw it to make it clearer then
in fact this is going to be less than 60
this value here is going to be between
60 and the value we pushed up which is
80 and this child over here contains
value greater than 80 which is 90 so the
last value we insert 100 is going to be
an easy one 100 is greater than 40 100
is greater than 80 and 100 fits neatly
into this existing node over here and
now our 2-3 tree is complete
5.0 / 5 (0 votes)