Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
LARN
PRRT
Commits
44f47d78
Commit
44f47d78
authored
Mar 17, 2016
by
Andreas Schmidt
Browse files
Add size function to BPTree.
parent
98809a7e
Changes
3
Show whitespace changes
Inline
Side-by-side
src/util/bptree.c
View file @
44f47d78
...
...
@@ -1079,3 +1079,26 @@ void BPTree_get_range(BPTreeNode *root, List *list, int key_start, int key_end)
List_push
(
list
,
((
BPTreeRecord
*
)
returned_pointers
[
i
])
->
value
);
}
}
uint32_t
BPTree_size
(
BPTreeNode
*
root
)
{
BPTreeNode
*
n
=
root
;
uint32_t
count
=
0
;
int
i
;
if
(
n
==
NULL
)
{
return
count
;
}
queue
=
NULL
;
enqueue
(
root
);
while
(
queue
!=
NULL
)
{
n
=
dequeue
();
if
(
!
n
->
is_leaf
)
for
(
i
=
0
;
i
<=
n
->
num_keys
;
i
++
)
enqueue
(
n
->
pointers
[
i
]);
if
(
n
->
is_leaf
)
{
count
+=
n
->
num_keys
;
}
}
return
count
;
}
\ No newline at end of file
src/util/bptree.h
View file @
44f47d78
...
...
@@ -75,5 +75,6 @@ void BPTree_get_range(BPTreeNode *root, List *list, int key_start, int key_end);
// Helpers
void
BPTree_print
(
BPTreeNode
*
root
);
int
BPTree_height
(
BPTreeNode
*
root
);
uint32_t
BPTree_size
(
BPTreeNode
*
root
);
#endif //PRRT_BPTREE_H
tests/bptree_tests.cpp
View file @
44f47d78
...
...
@@ -23,14 +23,23 @@ protected:
TEST_F
(
BPlusTreeTest
,
InsertDelete
)
{
void
*
p
=
malloc
(
sizeof
(
int
));
ASSERT_EQ
(
BPTree_height
(
root
),
0
);
ASSERT_EQ
(
BPTree_size
(
root
),
1
);
root
=
BPTree_insert
(
root
,
2
,
p
);
ASSERT_EQ
(
BPTree_height
(
root
),
0
);
ASSERT_EQ
(
BPTree_size
(
root
),
2
);
root
=
BPTree_insert
(
root
,
3
,
p
);
ASSERT_EQ
(
BPTree_height
(
root
),
0
);
ASSERT_EQ
(
BPTree_size
(
root
),
3
);
root
=
BPTree_insert
(
root
,
4
,
p
);
ASSERT_EQ
(
BPTree_height
(
root
),
1
);
ASSERT_EQ
(
BPTree_size
(
root
),
4
);
root
=
BPTree_delete
(
root
,
3
);
ASSERT_EQ
(
BPTree_height
(
root
),
0
);
ASSERT_EQ
(
BPTree_size
(
root
),
3
);
}
TEST_F
(
BPlusTreeTest
,
InsertAndFind
)
{
...
...
@@ -42,6 +51,7 @@ TEST_F(BPlusTreeTest, InsertAndFind) {
ASSERT_EQ
(
BPTree_get
(
root
,
3
),
p2
);
ASSERT_NE
(
BPTree_get
(
root
,
3
),
p
);
ASSERT_EQ
(
BPTree_size
(
root
),
4
);
}
TEST_F
(
BPlusTreeTest
,
FindRange
)
{
...
...
@@ -52,6 +62,7 @@ TEST_F(BPlusTreeTest, FindRange) {
void
*
pVoid
=
malloc
(
sizeof
(
int
));
p
[
i
]
=
pVoid
;
root
=
BPTree_insert
(
root
,
i
,
pVoid
);
ASSERT_EQ
(
BPTree_size
(
root
),
i
+
1
);
}
List
*
list
=
List_create
();
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment