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
fdafe0ba
Commit
fdafe0ba
authored
Feb 22, 2016
by
Andreas Schmidt
Browse files
Add BPTree.
parent
757273c9
Changes
6
Expand all
Show whitespace changes
Inline
Side-by-side
src/defines.h
View file @
fdafe0ba
...
...
@@ -17,4 +17,15 @@
#define N_START 4
#define N_P_START 1
// Uncomment the line below if you are compiling on Windows.
// #define WINDOWS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#ifdef WINDOWS
#define bool char
#define false 0
#define true 1
#endif
#endif //PRRT_DEFINES_H
src/util/CMakeLists.txt
View file @
fdafe0ba
add_library
(
UTIL ../defines.h common.c common.h list.c list.h dbg.h
)
\ No newline at end of file
add_library
(
UTIL ../defines.h common.c common.h list.c list.h dbg.h bptree.c bptree.h
)
\ No newline at end of file
src/util/bptree.c
0 → 100644
View file @
fdafe0ba
This diff is collapsed.
Click to expand it.
src/util/bptree.h
0 → 100644
View file @
fdafe0ba
/*
*
* bpt: B+ Tree Implementation
* Copyright (C) 2010 Amittai Aviram http://www.amittai.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* Author: Amittai Aviram
* http://www.amittai.com
* amittai.aviram@gmail.edu or afa13@columbia.edu
* Senior Software Engineer
* MathWorks, Inc.
* 3 Apple Hill Drive
* Natick, MA 01760
* Original Date: 26 June 2010
* Last modified: 15 April 2014
*
* This implementation demonstrates the B+ tree data structure
* for educational purposes, includin insertion, deletion, search, and display
* of the search path, the leaves, or the whole tree.
*
* Must be compiled with a C99-compliant C compiler such as the latest GCC.
*
* Usage: bpt [order]
* where order is an optional argument
* (integer MIN_ORDER <= order <= MAX_ORDER)
* defined as the maximal number of pointers in any node.
*
*/
#ifndef PRRT_BPTREE_H
#define PRRT_BPTREE_H
#include <src/defines.h>
#include "list.h"
/* Type representing a node in the B+ tree. This type is general enough to serve for both the leaf and the internal
* node. The heart of the node is the array of keys and the array of corresponding pointers. The relation between
* keys and pointers differs between leaves and internal nodes. In a leaf, the index of each key equals the index of
* its corresponding pointer, with a maximum of order - 1 key-pointer pairs. The last pointer points to the leaf to the
* right (or NULL in the case of the rightmost leaf). In an internal node, the first pointer refers to lower nodes with
* keys less than the smallest key in the keys array. Then, with indices i starting at 0, the pointer at i + 1 points
* to the subtree with keys greater than or equal to the key in this node at index i. The num_keys field is used to keep
* track of the number of valid keys. In an internal node, the number of valid pointers is always num_keys + 1.
* In a leaf, the number of valid pointers to data is always num_keys. The last leaf pointer points to the next leaf.
*/
typedef
struct
BPTreeNode
{
void
**
pointers
;
int
*
keys
;
struct
BPTreeNode
*
parent
;
bool
is_leaf
;
int
num_keys
;
struct
BPTreeNode
*
next
;
// Used for queue.
}
BPTreeNode
;
// Operations
BPTreeNode
*
BPTree_insert
(
BPTreeNode
*
root
,
int
key
,
void
*
value
);
BPTreeNode
*
BPTree_delete
(
BPTreeNode
*
root
,
int
key
);
BPTreeNode
*
BPTree_destroy
(
BPTreeNode
*
root
);
void
*
BPTree_get
(
BPTreeNode
*
root
,
int
key
);
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
);
#endif //PRRT_BPTREE_H
tests/CMakeLists.txt
View file @
fdafe0ba
add_subdirectory
(
lib/gtest-1.7.0
)
include_directories
(
SYSTEM
${
gtest_SOURCE_DIR
}
/include
${
gtest_SOURCE_DIR
}
)
add_executable
(
prrtTests forward_packet_table_tests.cpp
)
add_executable
(
prrtTests forward_packet_table_tests.cpp
bptree_tests.cpp
)
target_link_libraries
(
prrtTests LINK_PUBLIC PRRT UTIL gtest gtest_main
)
\ No newline at end of file
tests/bptree_tests.cpp
0 → 100644
View file @
fdafe0ba
#include <gtest/gtest.h>
extern
"C"
{
#include "src/util/bptree.h"
}
class
BPlusTreeTest
:
public
::
testing
::
Test
{
protected:
virtual
void
SetUp
()
{
root
=
NULL
;
void
*
p
=
malloc
(
sizeof
(
int
));
root
=
BPTree_insert
(
root
,
1
,
p
);
}
virtual
void
TearDown
()
{
root
=
BPTree_destroy
(
root
);
}
BPTreeNode
*
root
;
};
TEST_F
(
BPlusTreeTest
,
InsertDelete
)
{
void
*
p
=
malloc
(
sizeof
(
int
));
ASSERT_EQ
(
BPTree_height
(
root
),
0
);
root
=
BPTree_insert
(
root
,
2
,
p
);
ASSERT_EQ
(
BPTree_height
(
root
),
0
);
root
=
BPTree_insert
(
root
,
3
,
p
);
ASSERT_EQ
(
BPTree_height
(
root
),
0
);
root
=
BPTree_insert
(
root
,
4
,
p
);
ASSERT_EQ
(
BPTree_height
(
root
),
1
);
root
=
BPTree_delete
(
root
,
3
);
ASSERT_EQ
(
BPTree_height
(
root
),
0
);
}
TEST_F
(
BPlusTreeTest
,
InsertAndFind
)
{
void
*
p
=
malloc
(
sizeof
(
int
));
void
*
p2
=
malloc
(
sizeof
(
int
));
root
=
BPTree_insert
(
root
,
2
,
p
);
root
=
BPTree_insert
(
root
,
3
,
p2
);
root
=
BPTree_insert
(
root
,
4
,
p
);
ASSERT_EQ
(
BPTree_get
(
root
,
3
),
p2
);
ASSERT_NE
(
BPTree_get
(
root
,
3
),
p
);
}
TEST_F
(
BPlusTreeTest
,
FindRange
)
{
int
n
=
10
;
void
*
p
[
10
];
root
=
BPTree_delete
(
root
,
1
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
void
*
pVoid
=
malloc
(
sizeof
(
int
));
p
[
i
]
=
pVoid
;
root
=
BPTree_insert
(
root
,
i
,
pVoid
);
}
List
*
list
=
List_create
();
BPTree_get_range
(
root
,
list
,
2
,
3
);
ASSERT_EQ
(
List_count
(
list
),
2
);
ASSERT_EQ
(
List_shift
(
list
),
p
[
2
]);
ASSERT_EQ
(
List_shift
(
list
),
p
[
3
]);
ASSERT_EQ
(
List_count
(
list
),
0
);
List_destroy
(
list
);
list
=
List_create
();
BPTree_get_range
(
root
,
list
,
1
,
3
);
ASSERT_EQ
(
List_count
(
list
),
3
);
BPTree_print
(
root
);
}
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