Apr 23, 2013 at 6:15pm UTC
Why can't I create a new tnode in the bstree (Binary Search Tree) class?
TNode.h------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <iostream>
#include <cstddef>
#include <vector>
#ifndef TNode_h
#define TNode_h
using namespace std;
template <class T>
class tnode{
public :
tnode *left;
tnode *right;
T key;
tnode(T data)
{
key = data;
left = NULL;
right = NULL;
}
};
#endif
bst.h---------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <iostream>
#include <cstddef>
#include "TNode.h"
using namespace std;
template <class T>
class bstree
{
public :
bstree()
{
root = NULL;
}
void insert(T &k)
{
if (root == NULL)
root = new tnode(k);
}
private :
tnode *root; //error right here
};
Last edited on Apr 23, 2013 at 6:15pm UTC
Apr 23, 2013 at 6:46pm UTC
Haha bananas. Well, let's say I am to make a BST, which takes in letters, numbers, and operators such as " + - * / ". How would I go abou that. Would I have to make a new object everytime in order to add a different value everytime?
Apr 23, 2013 at 6:59pm UTC
So you want ot store different data types in one container? And how will you differentiate between them?
Apr 23, 2013 at 7:08pm UTC
Yea, for example
obj.insert(5);
obj.insert('d');
obj.insert('*');
obj.insert('+');
and I will use the BST class to give them priorities!
Apr 23, 2013 at 7:18pm UTC
And how would you do that? I am not saying it is impossible, but it is above-average level at the best.
Apr 23, 2013 at 7:24pm UTC
That's what I'm trying to figure out because I am new to templates.
Also in the bst class whenever I insert the "* - + /" it will have higher priority making it the root node and making the older nodes it's children based on value!
Last edited on Apr 23, 2013 at 7:29pm UTC
Apr 23, 2013 at 8:33pm UTC
I still can't figure it out.