Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
b_plus_tree
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
omar.alkheja
b_plus_tree
Commits
6d16df21
Commit
6d16df21
authored
3 years ago
by
omar.alkheja
Browse files
Options
Downloads
Patches
Plain Diff
insertion ok with a leaf +split ok
parent
6e4ab392
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
bpt.c
+93
-25
93 additions, 25 deletions
bpt.c
bpt.h
+2
-2
2 additions, 2 deletions
bpt.h
main.c
+13
-5
13 additions, 5 deletions
main.c
with
108 additions
and
32 deletions
bpt.c
+
93
−
25
View file @
6d16df21
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
node
*
creat_node
()
{
node
*
creat_node
()
{
node
*
nd
=
malloc
(
sizeof
(
node
));
node
*
nd
=
malloc
(
sizeof
(
node
));
// nd->key[0] = nb;
// nd->key[0] = nb;
for
(
int
i
=
1
;
i
<
ORDER
;
i
++
)
{
for
(
int
i
=
0
;
i
<
ORDER
+
1
;
i
++
)
{
nd
->
key
[
i
]
=
INT_MAX
;
nd
->
key
[
i
]
=
INT_MAX
;
}
}
for
(
int
i
=
0
;
i
<
ORDER
+
1
;
i
++
)
{
for
(
int
i
=
0
;
i
<
ORDER
+
1
;
i
++
)
{
...
@@ -32,22 +32,47 @@ void destroy(node *nd) {
...
@@ -32,22 +32,47 @@ void destroy(node *nd) {
void
shift
(
node
*
nd
,
int
index
)
{
void
shift
(
node
*
nd
,
int
index
)
{
nd
->
kids
[
ORDER
]
=
nd
->
kids
[
ORDER
-
1
];
nd
->
kids
[
ORDER
]
=
nd
->
kids
[
ORDER
-
1
];
for
(
int
j
=
ORDER
-
1
;
j
>
index
;
j
--
)
{
for
(
int
j
=
ORDER
-
1
;
j
>
index
;
j
--
)
{
// printf("
im here 2
%d:
et
j : %d\n", nd->NbOfElementsInTable, j);
// printf("
nb elements
%d:
and
j : %d\n", nd->NbOfElementsInTable, j);
nd
->
key
[
j
]
=
nd
->
key
[
j
-
1
];
nd
->
key
[
j
]
=
nd
->
key
[
j
-
1
];
nd
->
kids
[
j
+
1
]
=
nd
->
kids
[
j
];
nd
->
kids
[
j
+
1
]
=
nd
->
kids
[
j
];
}
}
}
}
node
*
split
(
node
*
nd
)
{
node
*
split
(
node
*
nd
)
{
node
*
tmp
=
creat_node
();
// for (int k = 0; k < nd->NbOfElementsInTable; k++) {
// printf("%d ", nd->key[k]);
// }
printf
(
"
\n
"
);
node
*
lhn
=
creat_node
();
node
*
rhn
=
creat_node
();
node
*
parent
=
creat_node
();
insert_key
(
parent
,
nd
->
key
[
ORDER
/
2
]);
parent
->
kids
[
0
]
=
lhn
;
parent
->
kids
[
1
]
=
rhn
;
for
(
int
t
=
0
;
t
<
ORDER
/
2
;
t
++
)
{
for
(
int
t
=
0
;
t
<
ORDER
/
2
;
t
++
)
{
tmp
->
key
[
t
]
=
nd
->
key
[
ORDER
/
2
+
1
];
insert_key
(
lhn
,
nd
->
key
[
t
]);
nd
->
key
[
ORDER
/
2
+
1
]
=
0
;
// lhn->key[t] = nd->key[t];
tmp
->
kids
[
t
]
=
nd
->
kids
[
ORDER
/
2
+
1
];
// lhn->key[t] = ;
nd
->
kids
[
ORDER
/
2
+
1
]
=
NULL
;
lhn
->
kids
[
t
]
=
NULL
;
// lhn->NbOfElementsInTable++;
}
for
(
int
i
=
ORDER
/
2
;
i
<
nd
->
NbOfElementsInTable
;
i
++
)
{
insert_key
(
rhn
,
nd
->
key
[
i
]);
// rhn->key[i - ORDER / 2] = nd->key[i];
// lhn->key[i] = ;
rhn
->
kids
[
i
]
=
NULL
;
// rhn->NbOfElementsInTable++;
}
if
(
is_a_leaf
(
lhn
)
&&
is_a_leaf
(
rhn
))
{
lhn
->
next
=
rhn
;
}
}
nd
->
NbOfElementsInTable
/=
2
;
tmp
->
NbOfElementsInTable
=
ORDER
/
2
;
// printf("nb of elements in lhn : %d\n", lhn->NbOfElementsInTable);
return
tmp
;
// printf("nb of elements in rhn : %d\n", rhn->NbOfElementsInTable);
// printf("nb of elements in parent : %d\n", parent->NbOfElementsInTable);
// free(nd);
return
parent
;
}
}
node
*
insert_key
(
node
*
nd
,
int
value
)
{
node
*
insert_key
(
node
*
nd
,
int
value
)
{
if
(
is_a_leaf
(
nd
))
{
if
(
is_a_leaf
(
nd
))
{
...
@@ -58,24 +83,36 @@ node *insert_key(node *nd, int value) {
...
@@ -58,24 +83,36 @@ node *insert_key(node *nd, int value) {
shift
(
nd
,
i
);
shift
(
nd
,
i
);
nd
->
key
[
i
]
=
value
;
nd
->
key
[
i
]
=
value
;
nd
->
NbOfElementsInTable
++
;
nd
->
NbOfElementsInTable
++
;
printf
(
"the kids : "
);
// printf("the kids : ");
for
(
int
k
=
0
;
k
<
nd
->
NbOfElementsInTable
;
k
++
)
{
// for (int k = 0; k < nd->NbOfElementsInTable; k++) {
printf
(
"%d "
,
nd
->
key
[
k
]);
// printf("%d ", nd->key[k]);
}
// }
printf
(
"
\n
"
);
// printf("\n");
printf
(
"Number of elements in the tab : %d
\n
"
,
nd
->
NbOfElementsInTable
);
return
nd
;
}
if
(
nd
->
key
[
i
]
==
0
)
{
nd
->
key
[
i
]
=
value
;
nd
->
NbOfElementsInTable
++
;
return
nd
;
return
nd
;
}
}
// if (nd->key[i] == 0) {
// nd->key[i] = value;
// nd->NbOfElementsInTable++;
// return nd;
// }
}
}
if
(
nd
->
NbOfElementsInTable
==
ORDER
)
{
if
(
nd
->
NbOfElementsInTable
==
ORDER
)
{
printf
(
"u have to split
\n
"
);
printf
(
"u have to split
\n
"
);
nd
->
key
[
ORDER
]
=
value
;
nd
->
NbOfElementsInTable
++
;
node
*
tmp
=
split
(
nd
);
free
(
nd
);
return
tmp
;
}
}
}
else
{
if
(
value
<
nd
->
key
)
{
insert_key
(
nd
->
kids
[
0
],
value
);
}
else
{
insert_key
(
nd
->
kids
[
1
],
value
);
}
}
}
printf
(
"
\n
"
);
printf
(
"Number of elements in the tab : %d
\n
"
,
nd
->
NbOfElementsInTable
);
return
nd
;
return
nd
;
}
}
node
search_key
(
node
*
nd
,
int
key
)
{
node
search_key
(
node
*
nd
,
int
key
)
{
...
@@ -110,7 +147,8 @@ void print_tree(node *nd, int depth) {
...
@@ -110,7 +147,8 @@ void print_tree(node *nd, int depth) {
printf
(
"the tree doesn't exist
\n
"
);
printf
(
"the tree doesn't exist
\n
"
);
return
;
return
;
}
}
for
(
int
i
=
0
;
i
<
nd
->
NbOfElementsInTable
+
1
;
i
++
)
{
for
(
int
i
=
0
;
i
<
nd
->
NbOfElementsInTable
;
i
++
)
{
printf
(
"|%d
\t
"
,
nd
->
key
[
i
]);
printf
(
"|%d
\t
"
,
nd
->
key
[
i
]);
}
}
printf
(
"|
\n
"
);
printf
(
"|
\n
"
);
...
@@ -119,6 +157,36 @@ void print_tree(node *nd, int depth) {
...
@@ -119,6 +157,36 @@ void print_tree(node *nd, int depth) {
}
}
return
;
return
;
}
}
void
bp_print
(
node
*
root
,
int
depth
)
{
// if we are on a leaf, we can print the values directly next to each other
if
(
is_a_leaf
(
root
))
{
for
(
int
i
=
0
;
i
<
depth
;
i
++
)
printf
(
" "
);
for
(
int
i
=
0
;
i
<
ORDER
;
i
++
)
{
printf
(
" %d |"
,
root
->
key
[
i
]);
// if we reach a 0, we have seen every values in the node
if
(
root
->
key
[
i
+
1
]
==
0
)
break
;
}
printf
(
"
\n
"
);
return
;
}
for
(
int
i
=
0
;
i
<
root
->
NbOfElementsInTable
;
i
++
)
{
bp_print
(
root
->
kids
[
i
],
depth
+
1
);
printf
(
"
\n
"
);
for
(
int
i
=
0
;
i
<
depth
;
i
++
)
printf
(
" "
);
printf
(
" %d |
\n
"
,
root
->
key
[
i
]);
if
(
root
->
key
[
i
+
1
]
==
0
)
bp_print
(
root
->
kids
[
i
+
1
],
depth
+
1
);
}
printf
(
"
\n
"
);
}
// fuction to hash keys and transforming them into integers
// fuction to hash keys and transforming them into integers
/*
/*
void hash(char *key, int length) {
void hash(char *key, int length) {
...
...
This diff is collapsed.
Click to expand it.
bpt.h
+
2
−
2
View file @
6d16df21
#include
<limits.h>
#include
<openssl/sha.h>
#include
<openssl/sha.h>
#include
<stdbool.h>
#include
<stdbool.h>
#include
<stdio.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdlib.h>
#include
<string.h>
#include
<string.h>
#include
<time.h>
#include
<time.h>
#include
<limits.h>
#ifndef B_PLUS_TREE
#ifndef B_PLUS_TREE
#define B_PLUS_TREE
#define B_PLUS_TREE
#define ORDER 4
#define ORDER 4
typedef
struct
node
{
typedef
struct
node
{
int
NbOfElementsInTable
;
int
NbOfElementsInTable
;
int
key
[
ORDER
];
int
key
[
ORDER
+
1
];
struct
node
*
kids
[
ORDER
+
1
];
struct
node
*
kids
[
ORDER
+
1
];
struct
node
*
next
;
struct
node
*
next
;
}
node
;
}
node
;
...
...
This diff is collapsed.
Click to expand it.
main.c
+
13
−
5
View file @
6d16df21
...
@@ -12,14 +12,22 @@ int main() {
...
@@ -12,14 +12,22 @@ int main() {
new
=
insert_key
(
new
,
3
);
new
=
insert_key
(
new
,
3
);
new
=
insert_key
(
new
,
2
);
new
=
insert_key
(
new
,
2
);
new
=
insert_key
(
new
,
1
);
new
=
insert_key
(
new
,
1
);
//
new = insert_key(new, 0);
new
=
insert_key
(
new
,
0
);
// new = insert_key(new, -1);
// new = insert_key(new, -1);
// new = insert_key(new, -2);
// new = insert_key(new, -2);
new
=
insert_key
(
new
,
-
1094795589
);
//
new = insert_key(new, -1094795589);
new
=
insert_key
(
new
,
6
);
new
=
insert_key
(
new
,
6
);
print_tree
(
new
,
0
);
printf
(
"
\n
"
);
printf
(
"--------------------------------------
\n
"
);
// new = insert_key(new, 7);
// new = insert_key(new, 4);
// new = insert_key(new, 4);
// new = insert_key(new, 5);
print_tree
(
new
,
0
);
print_tree
(
new
,
0
);
search_key
(
new
,
-
1094795589
);
// bp_print(new, 0);
destroy
(
new
);
// search_key(new, -1094795589);
return
0
;
return
0
;
}
}
\ No newline at end of file
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