I have a tree with numbers and a boolean to see if the numbers have been summed this is the code:
#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/lambda/bind.hpp>
using namespace std;
class ThreadBase
{
private:
boost::shared_ptr<boost::thread> m_thread_ptr;
public:
ThreadBase() : m_thread_ptr() { }
virtual ~ThreadBase() { }
virtual void run() = 0;
void start()
{
if (m_thread_ptr == NULL)
{
m_thread_ptr.reset(
new boost::thread(
boost::lambda::bind(&ThreadBase::run, this)));
}
else
{
throw std::runtime_error("multiple start");
}
}
void join()
{
if (m_thread_ptr)
{
m_thread_ptr->join();
}
}
};
struct NodeT
{
int key_value;
NodeT *left;
NodeT *right;
bool done;
};
class Btree
{
public:
Btree();
~Btree();
void insert(int key);
NodeT *search(int key);
void destroy_tree();
void inOrder(NodeT *leaf);
NodeT *root;
void display(NodeT *leaf, string where);
private:
void destroy_tree(NodeT *leaf);
void insert(int key, NodeT *leaf);
NodeT *search(int key, NodeT *leaf);
};
Btree::Btree()
{
cout << "creating tree" << endl;
root=NULL;
}
Btree::~Btree()
{
cout << "deleting tree end" << endl;
destroy_tree() ;
}
void Btree::destroy_tree(NodeT *leaf)
{
if (leaf != NULL)
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
cout << "tree deleted " << leaf->key_value << endl;
}
}
void Btree::insert(int key, NodeT *leaf)
{
if (key < leaf->key_value)
{
if (leaf->left != NULL)
insert(key, leaf->left);
else
{
cout << "creating new node to insert with less value" << key << endl;
leaf->left = new NodeT;
leaf->left->key_value=key;
leaf->left->left=NULL; //sets the left child to null
leaf->left->right=NULL; //sets the right child to null
}
}
else if (key >=leaf->key_value)
{
if (leaf->right != NULL)
insert(key, leaf->right);
else
{
cout << "creating node with more or equal value" << key << endl;
leaf->right = new NodeT;
leaf->right->key_value=key;
leaf->right->left=NULL; //sets the left node to null
leaf->right->right=NULL; //sets the right node to nulll
}
}
}
NodeT *Btree::search(int key, NodeT *leaf)
{
cout << "searching node" << key << endl;
if (leaf != NULL)
{
if(key == leaf->key_value)
{
cout << "finded in first attempt " << endl;
return leaf;
}
if(key < leaf->key_value)
{
cout << "looking again with less value to the left" << endl;
return search(key, leaf->left);
}
else
{
cout << "looking again more value to the right" << endl;
return search(key, leaf->right);
}
}
else
{
cout << "not found" << endl;
return NULL;
}
}
void Btree::insert(int key)
{
if (root != NULL)
insert(key, root);
else
{
cout << "inserting new node private func" << key << endl;
root=new NodeT;
root->key_value=key;
root->right=NULL;
root->left=NULL;
}
}
NodeT *Btree::search(int key)
{
cout << "searhing node private func" << endl;
return search(key, root);
}
void Btree::destroy_tree()
{
cout << "deleting tree public" << endl;
destroy_tree(root);
}
void Btree::inOrder(NodeT *leaf)
{
if(leaf!= NULL)
{
inOrder(leaf->left);
cout << "search inorder" << leaf->key_value << endl;
inOrder(leaf->right);
}
}
void Btree::display(NodeT *leaf, string where)
{
cout << where << endl;
if (leaf != NULL)
{
cout << leaf->key_value << " t or f"<< leaf->done << endl;
display(leaf->left, "left");
display(leaf->right, "rigth");
}
}
class MyThread : public ThreadBase
{
public:
void run()
{
}
int sum( Btree *ptrRoot)
{
//pointer to next node or to parent
NodeT *next = ptrRoot->root;
//if we are in some root we should be here so leave
if (next->left == NULL && next->right == NULL)
{
return 0;
}
//we are in a parent with 2 nodes
else if(next->left->left != NULL && next->right->right != NULL && next->left->done == false && next->right->done == false)
{
//sum values and put it into this node and set bool to true
//to not go again
next->key_value = next->left->key_value + next->right->key_value;
next->left->done = true;
next->right->done = true;
//all good
return 0;
}
//we are in a parent with 1 node the right one
else if(next->left->left == NULL && next->right->right != NULL && next->right->done == false)
{
//sum value + 0 and set flag to true
next->key_value = next->right->key_value + 0;
next->right->done = true;
//all is good
return 0;
}
//we are in a parent with the left node
else if (next->left->left != NULL && next->right->right == NULL && next->left->done == false)
{
//sum value + 0 and set flag to true
next->key_value = next->left->key_value + 0;
next->left->done = true;
//all is good
return 0;
}
cout << "we are at here " << next->key_value << endl;
return 0;
}
};
int THREADS_HOW_MANY = 0;
int main()
{
cout << "Trees example" << endl;
cout << "make yoir choice" << endl;
cout << "1 insert 2 delete 3 search" << endl;
Btree bt;
bt.insert(10);
bt.insert(15);
bt.insert(4);
bt.insert(2);
bt.insert(8);
bt.search(4);
bt.search(2);
bt.search(20);
bt.inOrder(bt.root);
bt.display(bt.root, "root");
cout << "end everyrhing" << endl;
MyThread mt;
mt.start();
mt.run();
mt.sum(&bt);
THREADS_HOW_MANY = boost::thread::hardware_concurrency();
std::cout << THREADS_HOW_MANY << std::endl;
return 0;
}
What I want to do is to go to all the tree and see the leaves that have false and sum it in the parent and again with the parents etc until the root, this has to be threaded so 1 thread sums the first 2 and the another the next ad infinitum, now I think the way I'm going is a little off to say because the function sum is not done yet, so can you give some tips.