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
eb707aea
Commit
eb707aea
authored
3 years ago
by
Florian Burgener
Browse files
Options
Downloads
Patches
Plain Diff
Refactoring
parent
98acc8e4
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
__main__.py
+45
-6
45 additions, 6 deletions
__main__.py
with
45 additions
and
6 deletions
__main__.py
+
45
−
6
View file @
eb707aea
...
@@ -11,8 +11,7 @@ class Node:
...
@@ -11,8 +11,7 @@ class Node:
self
.
children
=
[]
self
.
children
=
[]
# lower_bound
def
lower_bound
(
array
,
value
):
def
array_binary_search
(
array
,
value
):
low
=
0
low
=
0
high
=
len
(
array
)
-
1
high
=
len
(
array
)
-
1
...
@@ -28,9 +27,25 @@ def array_binary_search(array, value):
...
@@ -28,9 +27,25 @@ def array_binary_search(array, value):
return
low
return
low
def
is_value_in_array
(
array
,
value
):
low
=
0
high
=
len
(
array
)
-
1
while
low
<=
high
:
m
=
(
low
+
high
)
//
2
if
array
[
m
]
<
value
:
low
=
m
+
1
elif
array
[
m
]
>
value
:
high
=
m
-
1
else
:
return
True
return
False
def
array_insert_sorted
(
array
,
value
):
def
array_insert_sorted
(
array
,
value
):
index
=
array_binary_search
(
array
,
value
)
index
=
lower_bound
(
array
,
value
)
array
.
insert
(
index
,
value
)
array
.
insert
(
index
,
value
)
return
index
return
index
...
@@ -41,7 +56,7 @@ def find_leaf(root, key):
...
@@ -41,7 +56,7 @@ def find_leaf(root, key):
while
not
current
.
is_leaf
:
while
not
current
.
is_leaf
:
parents
.
append
(
current
)
parents
.
append
(
current
)
children_index
=
array_binary_search
(
current
.
keys
,
key
)
children_index
=
lower_bound
(
current
.
keys
,
key
)
current
=
current
.
children
[
children_index
]
current
=
current
.
children
[
children_index
]
return
parents
,
current
return
parents
,
current
...
@@ -66,7 +81,7 @@ def redistribute_keys(left_node, right_node, left_index, right_index):
...
@@ -66,7 +81,7 @@ def redistribute_keys(left_node, right_node, left_index, right_index):
def
split_leaf
(
node
,
key
):
def
split_leaf
(
node
,
key
):
virtual_insertion_index
=
array_binary_search
(
node
.
keys
,
key
)
virtual_insertion_index
=
lower_bound
(
node
.
keys
,
key
)
median_index
=
len
(
node
.
keys
)
//
2
median_index
=
len
(
node
.
keys
)
//
2
right_node
=
Node
(
node
.
order
)
right_node
=
Node
(
node
.
order
)
...
@@ -99,7 +114,7 @@ def redistribute_children(left_node, right_node, left_index, right_index):
...
@@ -99,7 +114,7 @@ def redistribute_children(left_node, right_node, left_index, right_index):
def
split_internal
(
node
,
key
,
right_child_node
):
def
split_internal
(
node
,
key
,
right_child_node
):
virtual_insertion_index
=
array_binary_search
(
node
.
keys
,
key
)
virtual_insertion_index
=
lower_bound
(
node
.
keys
,
key
)
median_index
=
len
(
node
.
keys
)
//
2
median_index
=
len
(
node
.
keys
)
//
2
right_node
=
Node
(
node
.
order
)
right_node
=
Node
(
node
.
order
)
right_node
.
is_leaf
=
False
right_node
.
is_leaf
=
False
...
@@ -162,6 +177,18 @@ def insert_non_full(node, key, previous_split_right_node):
...
@@ -162,6 +177,18 @@ def insert_non_full(node, key, previous_split_right_node):
node
.
children
.
insert
(
inserted_at_index
+
1
,
previous_split_right_node
)
node
.
children
.
insert
(
inserted_at_index
+
1
,
previous_split_right_node
)
def
tree_search
(
root
,
key
):
if
root
.
is_leaf
:
return
is_value_in_array
(
root
.
keys
,
key
)
children_index
=
lower_bound
(
root
.
keys
,
key
)
if
children_index
<
len
(
root
.
keys
)
and
root
.
keys
[
children_index
]
==
key
:
children_index
+=
1
return
tree_search
(
root
.
children
[
children_index
],
key
)
def
tree_print
(
root
,
depth
=
0
):
def
tree_print
(
root
,
depth
=
0
):
print
(
"
"
*
depth
,
end
=
""
)
print
(
"
"
*
depth
,
end
=
""
)
print
(
root
.
keys
)
print
(
root
.
keys
)
...
@@ -218,6 +245,18 @@ def main():
...
@@ -218,6 +245,18 @@ def main():
extracted_keys
=
extract_all_keys
(
root
)
extracted_keys
=
extract_all_keys
(
root
)
assert
extracted_keys
==
sorted
(
keys
)
assert
extracted_keys
==
sorted
(
keys
)
for
key
in
keys
:
assert
tree_search
(
root
,
key
)
for
_
in
range
(
5
):
while
True
:
random_key
=
random
.
randint
(
1
,
99
)
if
random_key
not
in
keys
:
break
assert
not
tree_search
(
root
,
random_key
)
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
main
()
main
()
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