Hi everyone, so I'm trying to build a simple binary tree that has two keys, and estimates the amount to sort it. Here's what it looks 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 check this 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 int main()':| C:\Users\Axel\Desktop\coding\C++ Projects\BTree\main.cpp|12|error: request for member
function int main()':| C:\Users\Axel\Desktop\coding\C++ Projects\BTree\main.cpp|12|error: request for member
int main()':| C:\Users\Axel\Desktop\coding\C++ Projects\BTree\main.cpp|12|error: request for member
insert 'in sbt', which is of non-class type
SumBTree () () '| || === Assembly completed: 1 error, 0 warnings === |
I canβt understand what I am doing wrong! Are these overloaded features? I do not understand. Does anyone know about this?