-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
492 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.main.o: main.cpp |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | ||
}; |
Oops, something went wrong.