Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ISC_144 - B+ Tree Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
florian.burgener
ISC_144 - B+ Tree Project
Commits
af264f7e
Commit
af264f7e
authored
3 years ago
by
Florian Burgener
Browse files
Options
Downloads
Patches
Plain Diff
Deletion step 2
parent
1b3e4265
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
__main__.py
+108
-98
108 additions, 98 deletions
__main__.py
rebu.py
+89
-0
89 additions, 0 deletions
rebu.py
with
197 additions
and
98 deletions
__main__.py
+
108
−
98
View file @
af264f7e
...
...
@@ -52,7 +52,7 @@ def find_value_index_in_array(array, value):
return
None
def
array_insert
_sorted
(
array
,
value
):
def
sorted_
array_insert
(
array
,
value
):
index
=
lower_bound
(
array
,
value
)
array
.
insert
(
index
,
value
)
return
index
...
...
@@ -95,15 +95,15 @@ def split_leaf(node, key):
if
virtual_insertion_index
<
median_index
:
median_value
=
node
.
keys
[
median_index
-
1
]
redistribute_keys
(
node
,
right_node
,
median_index
-
1
,
median_index
-
1
)
array_insert
_sorted
(
node
.
keys
,
key
)
sorted_
array_insert
(
node
.
keys
,
key
)
elif
virtual_insertion_index
>
median_index
:
median_value
=
node
.
keys
[
median_index
]
redistribute_keys
(
node
,
right_node
,
median_index
,
median_index
)
array_insert
_sorted
(
right_node
.
keys
,
key
)
sorted_
array_insert
(
right_node
.
keys
,
key
)
else
:
median_value
=
key
redistribute_keys
(
node
,
right_node
,
median_index
,
median_index
)
array_insert
_sorted
(
right_node
.
keys
,
key
)
sorted_
array_insert
(
right_node
.
keys
,
key
)
if
not
node
.
children
:
# The node has no link to the next node, so a link is created.
...
...
@@ -132,14 +132,14 @@ def split_internal(node, key, right_child_node):
median_value
=
node
.
keys
[
median_index
-
1
]
redistribute_keys
(
node
,
right_node
,
median_index
-
1
,
median_index
)
redistribute_children
(
node
,
right_node
,
median_index
,
median_index
)
inserted_at_index
=
array_insert
_sorted
(
node
.
keys
,
key
)
inserted_at_index
=
sorted_
array_insert
(
node
.
keys
,
key
)
node
.
children
.
insert
(
inserted_at_index
+
1
,
right_child_node
)
elif
virtual_insertion_index
>
median_index
:
# The key is virtually inserted to the right of the median index.
median_value
=
node
.
keys
[
median_index
]
redistribute_keys
(
node
,
right_node
,
median_index
,
median_index
+
1
)
redistribute_children
(
node
,
right_node
,
median_index
+
1
,
median_index
+
1
)
inserted_at_index
=
array_insert
_sorted
(
right_node
.
keys
,
key
)
inserted_at_index
=
sorted_
array_insert
(
right_node
.
keys
,
key
)
right_node
.
children
.
insert
(
inserted_at_index
+
1
,
right_child_node
)
else
:
# The key is virtually inserted at the median index.
...
...
@@ -179,7 +179,7 @@ def insert_full(root, parents, node, key, previous_split_right_node):
def
insert_non_full
(
node
,
key
,
previous_split_right_node
):
inserted_at_index
=
array_insert
_sorted
(
node
.
keys
,
key
)
inserted_at_index
=
sorted_
array_insert
(
node
.
keys
,
key
)
if
previous_split_right_node
is
not
None
:
node
.
children
.
insert
(
inserted_at_index
+
1
,
previous_split_right_node
)
...
...
@@ -222,123 +222,134 @@ def bptree_find_smallest_key(root):
def
bptree_shrink
(
root
):
right_child
=
root
.
children
[
1
]
root
.
keys
=
right_child
.
keys
root
.
children
=
right_child
.
children
child
=
root
.
children
[
0
]
root
.
keys
=
child
.
keys
.
copy
()
root
.
children
=
child
.
children
.
copy
()
root
.
is_leaf
=
child
.
is_leaf
def
bptree_delete_internal
(
root
,
parents
,
node
,
key
):
if
node
!=
root
:
parent
=
parents
.
pop
()
def
bptree_steal_internal
():
# TODO
pass
def
bptree_steal_leaf
(
parent
,
node
,
child_index
,
sibling
,
sibling_side
):
if
sibling_side
==
"
l
"
:
sibling_key
=
sibling
.
keys
[
len
(
sibling
.
keys
)
-
1
]
sorted_array_delete
(
sibling
.
keys
,
sibling_key
)
sorted_array_insert
(
node
.
keys
,
sibling_key
)
parent
.
keys
[
child_index
-
1
]
=
sibling_key
elif
sibling_side
==
"
r
"
:
sibling_key
=
sibling
.
keys
[
0
]
sorted_array_delete
(
sibling
.
keys
,
sibling_key
)
sorted_array_insert
(
node
.
keys
,
sibling_key
)
parent
.
keys
[
child_index
]
=
sibling
.
keys
[
0
]
def
_bptree_merge
(
parent
,
main_node
,
secondary_node
,
pivot_index
):
if
not
main_node
.
is_leaf
:
parent_key
=
parent
.
keys
[
pivot_index
]
main_node
.
keys
.
insert
(
0
,
parent_key
)
if
len
(
node
.
keys
)
<
node
.
order
and
node
!=
root
:
print
(
"
Merge 2
"
)
# children_index = None
for
i
in
reversed
(
range
(
len
(
secondary_node
.
keys
))):
main_node
.
keys
.
insert
(
0
,
secondary_node
.
keys
[
i
])
# for i, child in enumerate(parent.children)
:
# if node == child
:
#
children
_
in
dex = i
if
not
main_node
.
is_leaf
:
for
i
in
reversed
(
range
(
len
(
secondary_node
.
children
)))
:
main_node
.
children
.
in
sert
(
0
,
secondary_node
.
children
[
i
])
# if children_index == 0:
# sibling = parent.children[children_index + 1]
# parent_key = parent.keys[children_index]
# parent.keys.pop(find_value_index_in_array(parent.keys, parent_key))
# array_insert_sorted(sibling.keys, parent_key)
# array_insert_sorted(sibling.keys, node.keys[0])
# sibling.children = node.children + sibling.children
# else:
# sibling = parent.children[children_index - 1]
# parent_key = parent.keys[children_index - 1]
# parent.keys.pop(find_value_index_in_array(parent.keys, parent_key))
# array_insert_sorted(node.keys, parent_key)
parent
.
keys
.
pop
(
pivot_index
)
parent
.
children
.
pop
(
pivot_index
)
# for key in sibling.keys:
# array_insert_sorted(node.keys, key)
# node.children = sibling.children + node.children
def
bptree_merge
(
parent
,
node
,
child_index
,
sibling
,
sibling_side
):
if
sibling_side
==
"
l
"
:
# The sibling is merged into the node.
_bptree_merge
(
parent
,
node
,
sibling
,
child_index
-
1
)
elif
sibling_side
==
"
r
"
:
# The node is merged into the sibling.
_bptree_merge
(
parent
,
sibling
,
node
,
child_index
)
# if parent == root and len(parent.keys) == 0:
# bptree_shrink(root)
if
is_value_in_array
(
node
.
keys
,
key
)
and
len
(
node
.
children
)
>
0
:
index
=
find_value_index_in_array
(
node
.
keys
,
key
)
node
.
keys
[
index
]
=
bptree_find_smallest_key
(
node
.
children
[
index
+
1
])
def
find_child_index
(
parent
,
child
):
for
i
in
range
(
len
(
parent
.
children
)):
if
child
==
parent
.
children
[
i
]:
return
i
if
node
!=
root
:
bptree_delete_internal
(
root
,
parents
,
parent
,
key
)
return
None
def
bptree_find_sibling
(
parent
,
child
ren
_index
):
if
child
ren
_index
==
0
:
def
bptree_find_sibling
(
parent
,
child_index
):
if
child_index
==
0
:
# Must take the sibling on the right.
sibling
=
parent
.
children
[
1
]
sibling_position
=
"
r
"
elif
child
ren
_index
==
2
*
parent
.
order
-
1
:
elif
child_index
==
len
(
parent
.
children
)
-
1
:
# Must take the sibling on the left.
sibling
=
parent
.
children
[
2
*
parent
.
order
-
2
]
sibling
=
parent
.
children
[
len
(
parent
.
children
)
-
2
]
sibling_position
=
"
l
"
else
:
# Can take the sibling from left or right.
if
len
(
parent
.
children
[
child
ren
_index
-
1
].
keys
)
>
parent
.
order
:
if
len
(
parent
.
children
[
child_index
-
1
].
keys
)
>
parent
.
order
:
# The left sibling has enough keys to borrow one.
sibling
=
parent
.
children
[
child
ren
_index
-
1
]
sibling
=
parent
.
children
[
child_index
-
1
]
sibling_position
=
"
l
"
elif
len
(
parent
.
children
[
child
ren
_index
+
1
].
keys
)
>
parent
.
order
:
elif
len
(
parent
.
children
[
child_index
+
1
].
keys
)
>
parent
.
order
:
# The right sibling has enough keys to borrow one.
sibling
=
parent
.
children
[
child
ren
_index
+
1
]
sibling
=
parent
.
children
[
child_index
+
1
]
sibling_position
=
"
r
"
else
:
# A merge between the node and the left sibling is required.
sibling
=
parent
.
children
[
children_index
-
1
]
sibling
=
parent
.
children
[
child_index
-
1
]
sibling_position
=
"
l
"
return
sibling
,
sibling_position
def
bptree_delete_leaf
(
root
,
parents
,
leaf
,
key
):
if
leaf
!=
root
:
def
_bptree_delete
(
root
,
parents
,
node
,
key
):
parent
=
None
if
len
(
parents
)
>
0
:
parent
=
parents
.
pop
()
if
len
(
leaf
.
keys
)
>
leaf
.
order
or
leaf
==
root
:
sorted_array_delete
(
leaf
.
keys
,
key
)
if
node
.
is_leaf
:
keys_length
=
len
(
node
.
keys
)
sorted_array_delete
(
node
.
keys
,
key
)
if
keys_length
==
node
.
order
and
node
!=
root
:
child_index
=
find_child_index
(
parent
,
node
)
sibling
,
sibling_side
=
bptree_find_sibling
(
parent
,
child_index
)
if
len
(
sibling
.
keys
)
==
sibling
.
order
:
# There is not enough key in the left and right siblings.
bptree_merge
(
parent
,
node
,
child_index
,
sibling
,
sibling_side
)
if
parent
==
root
and
len
(
parent
.
keys
)
==
0
:
bptree_shrink
(
root
)
parent
=
None
else
:
# There are enough keys in the sibling to steal one.
bptree_steal_leaf
(
parent
,
node
,
child_index
,
sibling
,
sibling_side
)
else
:
deletion_index
=
find_value_index_in_array
(
leaf
.
keys
,
key
)
leaf
.
keys
.
pop
(
deletion_index
)
children_index
=
None
for
i
,
child
in
enumerate
(
parent
.
children
):
if
leaf
==
child
:
children_index
=
i
sibling
,
sibling_position
=
bptree_find_sibling
(
parent
,
children_index
)
if
len
(
sibling
.
keys
)
==
sibling
.
order
:
print
(
"
Merge 1
"
)
pass
# if children_index == 0:
# # Sibling not on the left.
# array_insert_sorted(sibling.keys, leaf.keys[0])
# parent.children.pop(children_index)
# parent.keys.pop(children_index)
# else:
# for key in sibling.keys:
# array_insert_sorted(leaf.keys, key)
# parent.children.pop(children_index - 1)
# parent.keys.pop(children_index - 1)
else
:
if
sibling_position
==
"
l
"
:
sibling_key
=
sibling
.
keys
[
len
(
sibling
.
keys
)
-
1
]
sorted_array_delete
(
sibling
.
keys
,
sibling_key
)
array_insert_sorted
(
leaf
.
keys
,
sibling_key
)
parent
.
keys
[
children_index
-
1
]
=
sibling_key
if
node
!=
root
and
len
(
node
.
keys
)
<
node
.
order
:
child_index
=
find_child_index
(
parent
,
node
)
sibling
,
sibling_side
=
bptree_find_sibling
(
parent
,
child_index
)
if
len
(
sibling
.
keys
)
==
sibling
.
order
:
bptree_merge
(
parent
,
node
,
child_index
,
sibling
,
sibling_side
)
if
parent
==
root
and
len
(
parent
.
keys
)
==
0
:
bptree_shrink
(
root
)
parent
=
None
else
:
sibling_key
=
sibling
.
keys
[
0
]
sorted_array_delete
(
sibling
.
keys
,
sibling_key
)
array_insert_sorted
(
leaf
.
keys
,
sibling_key
)
parent
.
keys
[
children_index
]
=
sibling
.
keys
[
0
]
print
(
"
???????????
"
)
if
len
(
node
.
children
)
>
0
and
is_value_in_array
(
node
.
keys
,
key
):
index
=
find_value_index_in_array
(
node
.
keys
,
key
)
node
.
keys
[
index
]
=
bptree_find_smallest_key
(
node
.
children
[
index
+
1
])
bptree_delete_internal
(
root
,
parents
,
parent
,
key
)
if
parent
is
not
None
:
_bptree_delete
(
root
,
parents
,
parent
,
key
)
def
deletion_find_leaf
(
root
,
key
):
...
...
@@ -363,14 +374,13 @@ def bptree_delete(root, key):
if
not
is_value_in_array
(
leaf
.
keys
,
key
):
return
bptree_delete
_leaf
(
root
,
parents
,
leaf
,
key
)
_
bptree_delete
(
root
,
parents
,
leaf
,
key
)
# DELETION : END
def
bptree_search
(
root
,
key
):
# BUG !!!
if
root
.
is_leaf
:
return
is_value_in_array
(
root
.
keys
,
key
)
...
...
@@ -455,19 +465,19 @@ def main():
# assert not bptree_search(root, random_key)
root
=
Node
(
order
)
keys
=
generate_random_keys
(
18
,
1
,
99
)
keys
=
generate_random_keys
(
30
,
1
,
99
)
print
(
keys
)
print
(
"
=====
"
)
for
key
in
keys
:
bptree_insert
(
root
,
key
)
bptree_delete
(
root
,
56
)
bptree_delete
(
root
,
12
)
bptree_delete
(
root
,
21
)
bptree_delete
(
root
,
65
)
bptree_delete
(
root
,
57
)
bptree_delete
(
root
,
47
)
bptree_delete
(
root
,
11
)
bptree_delete
(
root
,
86
)
# bptree_delete(root, 65)
bptree_print
(
root
)
...
...
This diff is collapsed.
Click to expand it.
rebu.py
0 → 100644
+
89
−
0
View file @
af264f7e
def
bptree_delete_leaf
(
root
,
parents
,
leaf
,
key
):
if
leaf
!=
root
:
parent
=
parents
.
pop
()
if
len
(
leaf
.
keys
)
>
leaf
.
order
or
leaf
==
root
:
sorted_array_delete
(
leaf
.
keys
,
key
)
else
:
deletion_index
=
find_value_index_in_array
(
leaf
.
keys
,
key
)
leaf
.
keys
.
pop
(
deletion_index
)
children_index
=
None
for
i
,
child
in
enumerate
(
parent
.
children
):
if
leaf
==
child
:
children_index
=
i
sibling
,
sibling_position
=
bptree_find_sibling
(
parent
,
children_index
)
if
len
(
sibling
.
keys
)
==
sibling
.
order
:
print
(
"
Merge 1
"
)
pass
# if children_index == 0:
# # Sibling not on the left.
# array_insert_sorted(sibling.keys, leaf.keys[0])
# parent.children.pop(children_index)
# parent.keys.pop(children_index)
# else:
# for key in sibling.keys:
# array_insert_sorted(leaf.keys, key)
# parent.children.pop(children_index - 1)
# parent.keys.pop(children_index - 1)
else
:
if
sibling_position
==
"
l
"
:
sibling_key
=
sibling
.
keys
[
len
(
sibling
.
keys
)
-
1
]
sorted_array_delete
(
sibling
.
keys
,
sibling_key
)
array_insert_sorted
(
leaf
.
keys
,
sibling_key
)
parent
.
keys
[
children_index
-
1
]
=
sibling_key
else
:
sibling_key
=
sibling
.
keys
[
0
]
sorted_array_delete
(
sibling
.
keys
,
sibling_key
)
array_insert_sorted
(
leaf
.
keys
,
sibling_key
)
parent
.
keys
[
children_index
]
=
sibling
.
keys
[
0
]
bptree_delete_internal
(
root
,
parents
,
parent
,
key
)
def
bptree_delete_internal
(
root
,
parents
,
node
,
key
):
if
node
!=
root
:
parent
=
parents
.
pop
()
if
len
(
node
.
keys
)
<
node
.
order
and
node
!=
root
:
print
(
"
Merge 2
"
)
# children_index = None
# for i, child in enumerate(parent.children):
# if node == child:
# children_index = i
# if children_index == 0:
# sibling = parent.children[children_index + 1]
# parent_key = parent.keys[children_index]
# parent.keys.pop(find_value_index_in_array(parent.keys, parent_key))
# array_insert_sorted(sibling.keys, parent_key)
# array_insert_sorted(sibling.keys, node.keys[0])
# sibling.children = node.children + sibling.children
# else:
# sibling = parent.children[children_index - 1]
# parent_key = parent.keys[children_index - 1]
# parent.keys.pop(find_value_index_in_array(parent.keys, parent_key))
# array_insert_sorted(node.keys, parent_key)
# for key in sibling.keys:
# array_insert_sorted(node.keys, key)
# node.children = sibling.children + node.children
# if parent == root and len(parent.keys) == 0:
# bptree_shrink(root)
if
is_value_in_array
(
node
.
keys
,
key
)
and
len
(
node
.
children
)
>
0
:
index
=
find_value_index_in_array
(
node
.
keys
,
key
)
node
.
keys
[
index
]
=
bptree_find_smallest_key
(
node
.
children
[
index
+
1
])
if
node
!=
root
:
bptree_delete_internal
(
root
,
parents
,
parent
,
key
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment