Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8058091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:14:37+00:00 2026-06-05T09:14:37+00:00

I have a tree with numbers and a boolean to see if the numbers

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:14:39+00:00Added an answer on June 5, 2026 at 9:14 am

    You will get to the same node from different threads, and thus you need to use locking when you access the node.done or use “lock free” techniques, e.g. compare-and-swap of the node.done member. You’ll need to modify the code to change the value of “done” before you sum the node.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code about binary search tree , I want Calculated efficancy for
I have the following tree- http://jsfiddle.net/anirbankundu/wzEBW/ This tree can have n number of nodes.
I have defined a pyparsing rule to parse this text into a syntax-tree... TEXT
I have a segment tree which holds data for a range of numbers (data
I have tree, T , structure in an Eclipse plugin which has a different
I have a tree view using html tags, when the tvcheckbox attrParent is checked,
I have a tree-like model I'd like to show in an NSOutlineView using an
I have table tree. I have query: SELECT * FROM tree WHERE pid =10
I have a tree of divs: <div id=a onclick=func> <div id=b onclick=func> <div id=c
I have an own Tree object implemented in PHP. Say we have a tree

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.