Commit 44f47d78 authored by Andreas Schmidt's avatar Andreas Schmidt
Browse files

Add size function to BPTree.

parent 98809a7e
...@@ -1078,4 +1078,27 @@ void BPTree_get_range(BPTreeNode *root, List *list, int key_start, int key_end) ...@@ -1078,4 +1078,27 @@ void BPTree_get_range(BPTreeNode *root, List *list, int key_start, int key_end)
for (i = 0; i < num_found; i++) { for (i = 0; i < num_found; i++) {
List_push(list, ((BPTreeRecord *) returned_pointers[i])->value); 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
...@@ -75,5 +75,6 @@ void BPTree_get_range(BPTreeNode *root, List *list, int key_start, int key_end); ...@@ -75,5 +75,6 @@ void BPTree_get_range(BPTreeNode *root, List *list, int key_start, int key_end);
// Helpers // Helpers
void BPTree_print(BPTreeNode *root); void BPTree_print(BPTreeNode *root);
int BPTree_height(BPTreeNode *root); int BPTree_height(BPTreeNode *root);
uint32_t BPTree_size(BPTreeNode *root);
#endif //PRRT_BPTREE_H #endif //PRRT_BPTREE_H
...@@ -23,14 +23,23 @@ protected: ...@@ -23,14 +23,23 @@ protected:
TEST_F(BPlusTreeTest, InsertDelete) { TEST_F(BPlusTreeTest, InsertDelete) {
void* p = malloc(sizeof(int)); void* p = malloc(sizeof(int));
ASSERT_EQ(BPTree_height(root), 0); ASSERT_EQ(BPTree_height(root), 0);
ASSERT_EQ(BPTree_size(root), 1);
root = BPTree_insert(root, 2, p); root = BPTree_insert(root, 2, p);
ASSERT_EQ(BPTree_height(root), 0); ASSERT_EQ(BPTree_height(root), 0);
ASSERT_EQ(BPTree_size(root), 2);
root = BPTree_insert(root, 3, p); root = BPTree_insert(root, 3, p);
ASSERT_EQ(BPTree_height(root), 0); ASSERT_EQ(BPTree_height(root), 0);
ASSERT_EQ(BPTree_size(root), 3);
root = BPTree_insert(root, 4, p); root = BPTree_insert(root, 4, p);
ASSERT_EQ(BPTree_height(root), 1); ASSERT_EQ(BPTree_height(root), 1);
ASSERT_EQ(BPTree_size(root), 4);
root = BPTree_delete(root, 3); root = BPTree_delete(root, 3);
ASSERT_EQ(BPTree_height(root), 0); ASSERT_EQ(BPTree_height(root), 0);
ASSERT_EQ(BPTree_size(root), 3);
} }
TEST_F(BPlusTreeTest, InsertAndFind) { TEST_F(BPlusTreeTest, InsertAndFind) {
...@@ -42,6 +51,7 @@ TEST_F(BPlusTreeTest, InsertAndFind) { ...@@ -42,6 +51,7 @@ TEST_F(BPlusTreeTest, InsertAndFind) {
ASSERT_EQ(BPTree_get(root, 3), p2); ASSERT_EQ(BPTree_get(root, 3), p2);
ASSERT_NE(BPTree_get(root, 3), p); ASSERT_NE(BPTree_get(root, 3), p);
ASSERT_EQ(BPTree_size(root), 4);
} }
TEST_F(BPlusTreeTest, FindRange) { TEST_F(BPlusTreeTest, FindRange) {
...@@ -52,6 +62,7 @@ TEST_F(BPlusTreeTest, FindRange) { ...@@ -52,6 +62,7 @@ TEST_F(BPlusTreeTest, FindRange) {
void *pVoid = malloc(sizeof(int)); void *pVoid = malloc(sizeof(int));
p[i] = pVoid; p[i] = pVoid;
root = BPTree_insert(root, i, pVoid); root = BPTree_insert(root, i, pVoid);
ASSERT_EQ(BPTree_size(root), i+1);
} }
List* list = List_create(); List* list = List_create();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment