updates to BTree
parent
f88438bab4
commit
675b30d1c1
43
cpp/BTree.h
43
cpp/BTree.h
|
|
@ -6,6 +6,8 @@
|
|||
#include <boost/shared_ptr.hpp>
|
||||
#include <CppUnitLite/TestHarness.h>
|
||||
#include "Key.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
// type 'a t =
|
||||
// Empty
|
||||
|
|
@ -105,13 +107,48 @@ typename Node<Key, Value>::Tree add(const Key& key, const Value& value, const ty
|
|||
}
|
||||
|
||||
template <class Key, class Value>
|
||||
Value find(const typename boost::shared_ptr<Node<Key,Value> >& tree, const Key& key){
|
||||
boost::optional<Value> find(const typename boost::shared_ptr<Node<Key,Value> >& tree, const Key& key){
|
||||
if(tree->key_ == key)
|
||||
return tree->value_;
|
||||
if(key < tree->key_)
|
||||
if(key < tree->key_ && tree->left_ != NULL)
|
||||
return find(tree->left_, key);
|
||||
else
|
||||
else if(tree->right_ != NULL)
|
||||
return find(tree->right_,key);
|
||||
return boost::none;
|
||||
}
|
||||
|
||||
template <class Key, class Value>
|
||||
typename Node<Key, Value>::Tree begin(const typename Node<Key,Value>::Tree& tree) {
|
||||
if(tree->left_ !=NULL){
|
||||
return begin(tree->left_);
|
||||
}
|
||||
else {
|
||||
return tree;
|
||||
}
|
||||
}
|
||||
|
||||
template <class Key, class Value>
|
||||
typename Node<Key, Value>::Tree end(const typename Node<Key,Value>::Tree& tree) {
|
||||
if(tree->right_ !=NULL){
|
||||
return begin(tree->right_);
|
||||
}
|
||||
else {
|
||||
return tree;
|
||||
}
|
||||
}
|
||||
|
||||
template <class Key, class Value>
|
||||
void walk(const typename boost::shared_ptr<Node<Key,Value> >& tree, std::string s) {
|
||||
if(tree->left_ !=NULL) {
|
||||
walk(tree->left_, s+"->l");
|
||||
}
|
||||
Key k = tree->key_;
|
||||
std::stringstream ss;
|
||||
ss << tree->height_;
|
||||
k.print(ss.str() +" "+ s);
|
||||
if(tree->right_ !=NULL) {
|
||||
walk(tree->right_, s+"->r");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* testBNode.cpp
|
||||
*
|
||||
* Created on: Feb 3, 2010
|
||||
* Author: cbeall3
|
||||
*/
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <CppUnitLite/TestHarness.h>
|
||||
#include "Key.h"
|
||||
#include "BTree.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace gtsam;
|
||||
|
||||
typedef pair<size_t, size_t> Range;
|
||||
//typedef boost::shared_ptr<Node<Symbol, Range> > Tree;
|
||||
typedef Node<Symbol,Range>::Tree RangeTree;
|
||||
|
||||
|
||||
/* ************************************************************************* */
|
||||
TEST( BNode, constructor )
|
||||
{
|
||||
RangeTree tree;
|
||||
CHECK(tree==NULL)
|
||||
LONGS_EQUAL(0,height(tree))
|
||||
|
||||
// check the height of tree after adding an element
|
||||
RangeTree tree1 = add(Symbol('x',1), Range(1,2), tree);
|
||||
LONGS_EQUAL(1,height(tree1))
|
||||
|
||||
boost::optional<Range> range1 = find(tree1, Symbol('x',1));
|
||||
CHECK(range1 == Range(1,2));
|
||||
|
||||
RangeTree tree2 = add(Symbol('x',5), Range(5,6), tree1);
|
||||
RangeTree tree3 = add(Symbol('x',3), Range(3,4), tree2);
|
||||
|
||||
boost::optional<Range> range2 = find(tree3, Symbol('x',5));
|
||||
boost::optional<Range> range3 = find(tree3, Symbol('x',3));
|
||||
|
||||
CHECK(range2 == Range(5,6));
|
||||
CHECK(range3 == Range(3,4));
|
||||
|
||||
// this causes Bus Error.
|
||||
//RangeTree tree4 = add(Symbol('x',2), Range(3,4), tree3);
|
||||
|
||||
//walk(tree4, "root");
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
int main() {
|
||||
TestResult tr;
|
||||
return TestRegistry::runAllTests(tr);
|
||||
}
|
||||
/* ************************************************************************* */
|
||||
Loading…
Reference in New Issue