views:

130

answers:

1

Hey all, so I am trying to build a simple binary tree that has two keys and evaluates the sum for its sorting. Here is what it's looking like:

struct SumNode
{
    int keyA;
    int keyB;
    SumNode *left;
    SumNode *right;
};

class SumBTree
{
    public:
        SumBTree();
        ~SumBTree();

        void insert(int, int);
        SumNode *search(int, int);
        SumNode *search(int);
        void destroy_tree();

    private:
        SumNode *root;

        void insert(int,int, SumNode*);
        SumNode *search(int,int, SumNode*);
        SumNode *search(int, SumNode*);
        void destroy_tree(SumNode*);
};


SumBTree::SumBTree()
{
    root = NULL;
}

SumBTree::~SumBTree(){};


void SumBTree::insert(int a, int b, SumNode *leaf)
{
    int sum = a + b;
    int leafsum = leaf->keyA + leaf->keyB;

    if (sum < leafsum)
    {
        if (leaf->left != NULL)
        {
            insert(a,b, leaf->left);
        }
        else
        {
            leaf->left = new SumNode;
            leaf->left->keyA = a;
            leaf->left->keyB = b;
            leaf->left->left = NULL;
            leaf->left->right = NULL;
        }
    }
    else
    {
        if (leaf -> right != NULL)
        {
            insert(a,b, leaf->right);
        }
        else
        {
            leaf->right = new SumNode;
            leaf->right->keyA = a;
            leaf->right->keyB = b;
            leaf->right->left = NULL;
            leaf->right->right = NULL;
        }
    }
}

SumNode *SumBTree::search(int a, int b, SumNode *leaf)
{
    if (leaf != NULL)
    {
         if (a == leaf->keyA && b == leaf->keyB)
            return leaf;

        int sum = a + b;
        int leafsum = leaf->keyA + leaf->keyB;

        if (sum < leafsum)
            return search(a, b, leaf->left);

        return search(a, b, leaf->right);


    }
    return NULL;
}


SumNode *SumBTree::search(int sum, SumNode *leaf)
{
    if (leaf != NULL)
    {
        int leafsum = leaf->keyA + leaf->keyB;

        if (sum == leafsum)
            return leaf;

        if (sum < leafsum)
            return search(sum, leaf->left);

        return search(sum, leaf->right);


    }
    return NULL;
}

void SumBTree::destroy_tree(SumNode *leaf)
{
    if(leaf != NULL)
    {
        destroy_tree(leaf->left);
        destroy_tree(leaf->right);
        delete leaf;
    }
}


void SumBTree::insert(int a, int b)
{
    if (root != NULL)
    {
        insert(a,b, root);
    }
    else
    {
        root = new SumNode;
        root->keyA = a;
        root->keyB = b;
        root->left = NULL;
        root->right = NULL;
    }
}

SumNode *SumBTree::search(int a, int b)
{
    return search(a, b, root);
}

SumNode *SumBTree::search(int sum)
{
    return search(sum, root);
}

void SumBTree::destroy_tree()
{
    destroy_tree(root);
}

I went to test it out, using this:

#include <iostream>
#include "SumBTree.h"

using namespace std;

int main()
{
    cout << "Initializing Tree" << endl;
    SumBTree sbt();

    cout << "Inserting (2,3)" << endl;
    sbt.insert(2,3);



    cout << "Hello world!" << endl;
    return 0;
}

But whenever I try to build it I get the following error:

||=== BTree, Debug ===| C:\Users\Axel\Desktop\coding\C++ Projects\BTree\main.cpp||In function int main()':| C:\Users\Axel\Desktop\coding\C++ Projects\BTree\main.cpp|12|error: request for memberinsert' in sbt', which is of non-class typeSumBTree ()()'| ||=== Build finished: 1 errors, 0 warnings ===|

I can't figure out what I'm doing wrong! Is it the overloaded functions? I don't get it. Does anyone here know?

+3  A: 

you need to replace SumBTree sbt(); with SumBTree sbt;

Right now it thinks sbt isn't a class. If there are no parameters in a constructor don't use empty brackets.

Chris H
+1 - note that he/she is declaring a function named sbt that takes no parameters and returns a SumBTree.
RC
Thank you both! I appreciate the help.
SpaceMunkee