diff --git a/BST/.main.d b/BST/.main.d new file mode 100644 index 0000000..f0df3e7 --- /dev/null +++ b/BST/.main.d @@ -0,0 +1 @@ +.main.o: main.cpp diff --git a/BST/.main.o b/BST/.main.o new file mode 100644 index 0000000..6daba71 Binary files /dev/null and b/BST/.main.o differ diff --git a/BST/.test-BST b/BST/.test-BST new file mode 100755 index 0000000..221c0f7 Binary files /dev/null and b/BST/.test-BST differ diff --git a/BST/.test-BST.d b/BST/.test-BST.d new file mode 100644 index 0000000..6593702 --- /dev/null +++ b/BST/.test-BST.d @@ -0,0 +1,2 @@ +.test-BST.o: test-BST.cpp .test.hpp stream_saver_t.hpp BST.hpp \ + BST_node.hpp diff --git a/BST/.test-BST.o b/BST/.test-BST.o new file mode 100644 index 0000000..96e62c0 Binary files /dev/null and b/BST/.test-BST.o differ diff --git a/BST/.test-BST.passed b/BST/.test-BST.passed new file mode 100644 index 0000000..e69de29 diff --git a/BST/.test.hpp b/BST/.test.hpp new file mode 100644 index 0000000..b731eed --- /dev/null +++ b/BST/.test.hpp @@ -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 +#include +#include +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MAIN +#include +#include "stream_saver_t.hpp" + +inline std::string vecstr(const std::vector& rhs) +{ + std::string s(rhs.begin(), rhs.end()); + return s; +} + +inline std::vector vecstr(const std::string& rhs) +{ + std::vector 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& 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; +} diff --git a/BST/BST.hpp b/BST/BST.hpp new file mode 100644 index 0000000..8267bb9 --- /dev/null +++ b/BST/BST.hpp @@ -0,0 +1,244 @@ +#include +#include "BST_node.hpp" +#include +#include + +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 + 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 + 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 + void BFT(BST_node* curr_node, FunType visit) + { + queue 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(); + } + } +}; + diff --git a/BST/BST_node.hpp b/BST/BST_node.hpp new file mode 100644 index 0000000..954afb9 --- /dev/null +++ b/BST/BST_node.hpp @@ -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_; +}; diff --git a/BST/GNUmakefile b/BST/GNUmakefile new file mode 100644 index 0000000..1dbfb04 --- /dev/null +++ b/BST/GNUmakefile @@ -0,0 +1,54 @@ +# 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 +# Copyright Ken Smith kgsmith at gmail dot com, 2013. + +# User configurable portion. +prefix := /usr/local +cpus := 4 +prog := $(notdir $(CURDIR)) + +# Implementation +MAKEFLAGS := j$(cpus) +g := g++ -flto -g -std=c++11 -Wall -Werror -pedantic +c := $(g) -c -MMD +l := $(g) +s := $(wildcard *.cpp) +o := $(addprefix .,$(patsubst %.cpp,%.o,$(s))) +d := $(o:o=d) +p := $(filter-out .test-%.o,$(o)) +t := $(patsubst %.o,%,$(filter .test-%.o,$(o))) + +.PRECIOUS\ +:$(t)\ + $(o) + +$(prog)\ +:$(p) $(addsuffix .passed,$(t))\ +#;$(l) -o $@ $(p) -lluabind + +.%.o\ +:%.cpp $(MAKEFILE_LIST)\ +;$(c) -o $@ $< + +.test-%.passed\ +:.test-%\ +;./$< --random\ +&& touch $@ + +.test-%\ +:.test-%.o\ +;$(l) -o $@ $< -lboost_unit_test_framework + +.PHONY\ +:clean\ + install + +install\ +:$(prog)\ +;cp $(prog) $(prefix)/bin + +clean\ +:\ +;rm -Rf $(prog) $(t) .*.o .*.d .*.passed html latex + +-include $(d) diff --git a/BST/main.cpp b/BST/main.cpp new file mode 100644 index 0000000..c13815c --- /dev/null +++ b/BST/main.cpp @@ -0,0 +1,4 @@ +int main() +{ + return 0; +} diff --git a/BST/stream_saver_t.hpp b/BST/stream_saver_t.hpp new file mode 100644 index 0000000..49c6a07 --- /dev/null +++ b/BST/stream_saver_t.hpp @@ -0,0 +1,31 @@ +/** + * @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 + +#include +#include + +struct stream_saver_t +{ + stream_saver_t(std::ostream & os) + :os_(os) + ,saved_flags_(os.flags()) + { + } + + ~stream_saver_t() + { + os_.flags(saved_flags_); + } +private: + std::ostream & os_; + std::ios_base::fmtflags saved_flags_; +}; + diff --git a/BST/test-BST.cpp b/BST/test-BST.cpp new file mode 100644 index 0000000..d62ce42 --- /dev/null +++ b/BST/test-BST.cpp @@ -0,0 +1,24 @@ +#include ".test.hpp" +#include "BST.hpp" + +TEST(basic) +{ + BST tree(20,"I am super excited about everything"); + tree.insert_node(10,"nintynintynine"); + tree.insert_node(5,"uno"); + tree.insert_node(17,"tooo"); + tree.insert_node(8,"benjammin1"); + tree.insert_node(50,"benjammin2"); + tree.insert_node(80,"benjammin3"); + tree.insert_node(55,"benjammin4"); + tree.insert_node(52,"benjammin5"); + tree.insert_node(31,"benjammin6"); + tree.insert_node(100,"benjammin666"); + tree.insert_node(18,"lambda methods rule!"); + + tree.print_tree(); + tree.find_value(100); + tree.find_key("tooo"); + tree.find_value(51); + +}