Implement a BST class that keeps a pointer to the TNode that is the root of the
ID: 3573963 • Letter: I
Question
Implement a BST class that keeps a pointer to the TNode that is the root of the BST. Implement two constructors, a no parameters constructor and a copy constructor. Also implement an insert method that inserts new data into the tree.
This is only my first C++ class at school so do not make the answer contain things that are really advanced. We only recently did pointers about a week ago, if that gives you an idea as to how much i know already. I just don't know how to begin this.
This is what we were given: class TNode
{
public:
int data;
int x;
TNode *left;
TNode *right;
TNode(int n) { data = n; left = right = 0; }
TNode(int m, TNode *left, TNode *right) {
data = m;
}
};
void printTree(TNode *t)
{
if (!t) return;
printTree(t->left);
cout << t->data << " ";
printTree(t->right);
}
I know how to output a BST but not how to do this assignment
Explanation / Answer
void printTree(TNode *t)
{
if(t != NULL)
{
if(t->left) printTree(t->left);
if(t->right) printTree(t->right);
cout<<t->data<<" ";
}
else return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.