Skip to content

Commit

Permalink
adding BST project
Browse files Browse the repository at this point in the history
  • Loading branch information
ebott committed Jul 12, 2013
1 parent c703e6f commit 9d5c6b5
Show file tree
Hide file tree
Showing 13 changed files with 492 additions and 0 deletions.
1 change: 1 addition & 0 deletions BST/.main.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.main.o: main.cpp
Binary file added BST/.main.o
Binary file not shown.
Binary file added BST/.test-BST
Binary file not shown.
2 changes: 2 additions & 0 deletions BST/.test-BST.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.test-BST.o: test-BST.cpp .test.hpp stream_saver_t.hpp BST.hpp \
BST_node.hpp
Binary file added BST/.test-BST.o
Binary file not shown.
Empty file added BST/.test-BST.passed
Empty file.
61 changes: 61 additions & 0 deletions BST/.test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @file
* @copyright Ken Smith kgsmith at gmail.com, 2013.
* @license This software is released under the Boost
* Software License, version 1.0.
* See LICENSE_1_0.txt or
* http://www.boost.org/LICENSE_1_0.txt
*/

#pragma once

#define TEST BOOST_AUTO_TEST_CASE
#define FTST BOOST_FIXTURE_TEST_CASE
#define EQ BOOST_REQUIRE_EQUAL
#define NE BOOST_REQUIRE_NE
#define THROW BOOST_REQUIRE_THROW
#define NO_THROW BOOST_REQUIRE_NO_THROW

#include <iostream>
#include <string>
#include <vector>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include "stream_saver_t.hpp"

inline std::string vecstr(const std::vector<char>& rhs)
{
std::string s(rhs.begin(), rhs.end());
return s;
}

inline std::vector<char> vecstr(const std::string& rhs)
{
std::vector<char> v(rhs.begin(), rhs.end());
return v;
}

inline std::ostream& operator<<(std::ostream& os, const std::nullptr_t& rhs)
{
os << "nullptr";
return os;
}

inline std::ostream& operator<<(std::ostream& os, const std::vector<char>& rhs)
{
stream_saver_t saver(os);

os
<< "["
<< rhs.size()
<< "]"
<< std::hex << "{";
for (char byte : rhs)
{
os << "0x" << uint32_t(uint8_t(byte)) << ",";
}
os << "}";

return os;
}
244 changes: 244 additions & 0 deletions BST/BST.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
#include <iostream>
#include "BST_node.hpp"
#include <queue>
#include <functional>

using namespace std;

struct BST
{
BST(int root_key, string root_val)
:root_(new BST_node(root_key, root_val))
{}
/*
void insert_node(int new_key, string new_val)
{
BST_node* new_node = new BST_node(new_key,new_val);
BST_node* curr_node = root_;
while(curr_node != nullptr)
{
if(new_key >= curr_node->key())
{
//move left or insert left
if(curr_node->l_child())
{
curr_node = curr_node->l_child();
}
else
{
curr_node->l_child(new_node);
new_node->parent(curr_node);
curr_node = nullptr;
}
}
else if(new_key < curr_node->key())
{
//move right or insert right
if(curr_node->r_child())
{
curr_node = curr_node->r_child();
}
else
{
curr_node->r_child(new_node);
new_node->parent(curr_node);
curr_node = nullptr;
}
}
else
{
//we shoud never get here
throw "ack!";
}
}
}
*/
void insert_node(int new_key, string new_val)
{

auto insert_fcn = [&] (BST_node* n)
{
BST_node* new_node = new BST_node(new_key,new_val);
cout << "hitting the func!" << endl;
if(new_key <= n->key())
{
//this convention means that dupe keys
//all goes to the left
n->l_child(new_node);
new_node->parent(n);
}
else if(new_key > n->key())
{
n->r_child(new_node);
new_node->parent(n);
}
};

binary_trans(root_,new_key,insert_fcn,insert_fcn);
}

void print_tree()
{
BFT(root_,
[] (BST_node* n)
{
cout << n
<< " "
<< n->key()
<< " "
<< n->value()
<< endl;
});
}

void find_value(int key_wanted)
{
cout << "Looking for key "
<< key_wanted
<< endl;

auto found = [] (BST_node* n)
{
cout << "FOUND IT!" << endl;
cout << n
<< " "
<< n->key()
<< " "
<< n->value()
<< endl;

};

auto not_found = [] (BST_node* n)
{
cout << "NOT FOUND IT!" << endl;
};

binary_trans(root_, key_wanted, found, not_found);
/*
BFT(root_,
[&] (BST_node* n)
{
if(n->key() == key_wanted)
{
cout << "FOUND IT!"
<< n->value()
<< endl;
}
});*/
}


void find_key(string val_wanted)
{
BFT(root_,
[&] (BST_node* n)
{
if(n->value() == val_wanted)
{
cout << "FOUND THIS ONE!"
<< n->key()
<< endl;
}
});
}

//REALLLLY NEED TO THINK ABOUT ~BST method!!!!
private:
struct BST_node* root_;

void print_node(BST_node* n)
{
cout << n
<< " "
<< n->key()
<< " "
<< n->value()
<< endl;
}

template <typename FunType1, typename FunType2>
void binary_trans
(BST_node* curr_node, int target_key, FunType1 hit_fcn, FunType2 miss_fcn)
{
bool searching = true;
while(searching == true)
{
if(target_key == curr_node->key())
{
hit_fcn(curr_node);
searching = false;
}
else if(target_key > curr_node->key())
{
//move left or return
if(curr_node->l_child())
{
curr_node = curr_node->l_child();
}
else
{
miss_fcn(curr_node);
searching = false;
}
}
else if(target_key < curr_node->key())
{
//move right or return
if(curr_node->r_child())
{
curr_node = curr_node->r_child();
}
else
{
miss_fcn(curr_node);
searching = false;
}
}
else
{
//we shoud never get here
throw "ack!";
}
}

}

template <typename FunType>
void DFT(BST_node* curr_node, FunType visit) const
{
if(curr_node == nullptr)
{
return;
}

DFT(curr_node->l_child(), visit);
visit(curr_node);
DFT(curr_node->r_child(), visit);
}

template <typename FunType>
void BFT(BST_node* curr_node, FunType visit)
{
queue<BST_node*> node_queue;
node_queue.push(curr_node);

while(node_queue.size() != 0)
{
if(curr_node->l_child())
{
node_queue.push(curr_node->l_child());
}
if(curr_node->r_child())
{
node_queue.push(curr_node->r_child());
}

visit(curr_node);
node_queue.pop();
curr_node = node_queue.front();
}
}
};

71 changes: 71 additions & 0 deletions BST/BST_node.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using namespace std;

/*
* the general node element for my basic BST work
* I hope that this struct will be useable across multiple
* BST applications. ebott
*/

struct BST_node
{
BST_node(int i, string str)
:key_(i)
,value_(str)
,parent_(nullptr)
,l_child_(nullptr)
,r_child_(nullptr)
{}

int key() const
{
return key_;
}

string value() const
{
return value_;
}

void value(string new_value)
{
value_ = new_value;
}

BST_node* l_child() const
{
return this->l_child_;
}

void l_child(BST_node* new_child)
{
this->l_child_ = new_child;
}

BST_node* r_child() const
{
return this->r_child_;
}

void r_child(BST_node* new_child)
{
this->r_child_ = new_child;
}

BST_node* parent() const
{
return this->parent_;
}

void parent(BST_node* new_parent)
{
this->parent_ = new_parent;
}


private:
int key_;
string value_;
struct BST_node* parent_;
struct BST_node* l_child_;
struct BST_node* r_child_;
};
Loading

0 comments on commit 9d5c6b5

Please sign in to comment.