C++ function to populate right side of a tree - c++

The Problem
We have provided you a class TreeNode. The methods are declared in TreeNode.h, and there are implementations of most of them in TreeNode.cpp.
Your work:
Implement the function TreeNode *makeFullRight(int n) that builds a full tree with
n
nodes. The way you will build the tree is that the left child of each two-child node will be a leaf. The nodes should be numbered (by populating its data_ field) from 1 to
n
, according to the following rule: if a node had number
i
, then any left child would have number
i
+
1
and any right child would have number
i
+
2
.
Put your code in TreeNode.cpp.
As an example, makeFullRight(7) will produce the tree
1
/ \
2 3
/ \
4 5
/ \
6 7
You should assume that your makeFullRight function will be given an odd number as input.
You can write this using any technique you want, but we recommend using recursion and writing a helper function.
Sample Run
Your main.cpp will exercise your code.
Ive tired to do it using recursion but i couldnt get the answer and now i am lost on how to the makeFullRight function
TreeNode.cpp- File
#include "TreeNode.h"
// Your function here
TreeNode *makeFullRight(int n)
{
}
// Methods and functions we provide following.
// You should not need to edit this code.
TreeNode::TreeNode(int data, TreeNode *left, TreeNode *right) :
data_(data), left_(left), right_(right) {}
TreeNode::~TreeNode() {
if (left_ != NULL)
delete left_;
if (right_ != NULL)
delete right_;
}
bool equal(TreeNode *n1, TreeNode *n2) {
if (n1 == NULL)
return n2 == NULL;
if (n2==NULL)
return false;
return (n1->getData() == n2->getData() &&
equal(n1->getLeft(),n2->getLeft()) &&
equal(n1->getRight(),n2->getRight()));
}
int TreeNode::getData() const {
return data_;
}
TreeNode *TreeNode::getLeft() const {
return left_;
}
TreeNode *TreeNode::getRight() const {
return right_;
}
TreeNode.h--- New File
#ifndef _TREENODE_H
#define _TREENODE_H
#include <cstddef>
class TreeNode {
public:
int data_;
TreeNode *left_;
TreeNode *right_;
TreeNode(int data=0, TreeNode *left=NULL, TreeNode *right=NULL);
~TreeNode();
int findMax() const;
int getData() const;
TreeNode *getLeft() const;
TreeNode *getRight() const;
};
// Here is the signature of the code you will write
TreeNode *makeFullRight(int n);
bool equal(TreeNode *n1, TreeNode *n2);
#endif
main.cpp ---New File
#include <iostream>
#include "TreeNode.h"
using namespace std;
const string RED_TEXT = "\033[1;31m";
const string GREEN_TEXT = "\033[1;32m";
const string RESET_TEXT = "\033[0m";
void print_pass(string message) {
cout<<GREEN_TEXT<<"TEST PASSED"<<RESET_TEXT<<": "<<message<<endl;
}
void print_fail(string message) {
cout<<RED_TEXT<<"TEST FAILED"<<RESET_TEXT<<": "<<message<<endl;
exit(1);
}
int main() {
TreeNode *example =
new TreeNode(1,
new TreeNode(2),
new TreeNode(3,
new TreeNode(4),
new TreeNode(5,
new TreeNode(6),
new TreeNode(7))));
// Try complete
TreeNode *x = makeFullRight(7);
cout << "Result of makeFullRight: " << endl;
if (equal(x,example))
print_pass("");
else
print_fail("");
// Clean up
delete x;
delete example;
}
Makefile---New File
EXENAME = main
OBJS = main.o TreeNode.o
CXX = clang++
CXXFLAGS = -std=c++0x -c -g -O0 -Wall -Wextra
LD = clang++
LDFLAGS = -std=c++0x
all: $(EXENAME)
$(EXENAME): $(OBJS)
$(LD) $^ $(LDFLAGS) -o $#
main.o: main.cpp
$(CXX) $< $(CXXFLAGS)
TreeNode.o: TreeNode.cpp TreeNode.h
$(CXX) $< $(CXXFLAGS)
clean:
-rm -f *.o $(EXENAME)

If you examine the tree, you will see that each subtree has k in the root, a leaf with k+1 in the left node, and a right-full tree with k+2 in its root in the right node.
Assume that you have the suggested helper function – let's call it "fullHelper" – that keeps track of the "current value" and produces a tree with that in its root:
// Produce a right-full tree with 'current' at the root, up until 'n'.
TreeNode* fullHelper(int n, int current);
Then you can write
TreeNode* makeFullRight(int n)
{
return fullHelper(n, 1);
}
Now, "all" you need is to write the helper function.
You need a base case and a recursive case:
If n == current, create a single node with n,
Otherwise, create a tree with current in the root, current+1 in the left leaf, and a right-full tree rooted at current+2 as its right subtree.
Translated to C++:
TreeNode* fullHelper(int n, int current)
{
return n == current
? new TreeNode(current)
: new TreeNode(current,
new TreeNode(current+1),
fullHelper(n, current+2));
}

Related

C Creating a binary tree based on a sequence

I need help adjusting the createTree function.
Which accepts a string and after that character by character traverses it, creating a binary tree based on it
If it encounters the character 0, it recursively creates two sub-branches.
If it encounters another character, it saves it in the leaf node.
For the string in the example, I need to make a tree as in the picture, but the function does not work properly for me. Thank you in advance for your advice.
int x = 0;
Node* createTree(string str, int si, int ei)
{
if (si > ei)
return NULL;
Node *root = new Node((str[si] - '0'));
if(str[si] != '0')
{
x++;
root->m_Data = (str[si] - '0');
return root;
}
if(str[si]=='0')
{
x++;
root->m_Left = createTree(str,x,ei);
root->m_Right = createTree(str,x,ei);
}
return root;
}
int main ()
{
string str = "050067089";
Node *node = createTree(str,0,str.length());
printPreorder(node);
return 0;
}
The problem can quite easily be broken down into small steps (what you partly did in your question).
Start iterating at the first character
Create the root node
If the current character is non-zero, set the value of this node to this character
If current character is a zero, set this node to zero, create a left and a right node and get back to step 3 for every one of them. (That's the recursive part.)
Below is my implementation of this algorithm.
First, a little bit of setting up:
#include <iostream>
#include <string>
#include <memory>
struct Node;
// Iterator to a constant character, NOT a constant iterator
using StrConstIt = std::string::const_iterator;
using UniqueNode = std::unique_ptr<Node>;
struct Node
{
int value;
UniqueNode p_left;
UniqueNode p_right;
Node(int value)
: value(value) {}
Node(int value, UniqueNode p_left, UniqueNode p_right)
: value(value), p_left(std::move(p_left)), p_right(std::move(p_right)) {}
};
As you can see, I'm using std::unique_ptr for managing memory. This way, you don't have to worry about manually deallocating memory. Using smart pointers is often considered the more "modern" approach, and they should virtually always be preferred over raw pointers.
UniqueNode p_createNodeAndUpdateIterator(StrConstIt& it, StrConstIt stringEnd)
{
if (it >= stringEnd)
return nullptr;
UniqueNode node;
if (*it == '0')
// Create node with appropriate value
// Create branches and increment iterator
node = std::make_unique<Node>(
0,
p_createNodeAndUpdateIterator(++it, stringEnd),
p_createNodeAndUpdateIterator(it, stringEnd)
);
else
{
// Create leaf node with appropriate value
node = std::make_unique<Node>(*it - '0');
// Increment iterator
++it;
}
return node;
}
UniqueNode p_createTree(StrConstIt begin, StrConstIt end)
{
return p_createNodeAndUpdateIterator(begin, end);
}
The first function takes a reference to the iterator to the next character it should process. That is because you can't know how much characters a branch will have in its leaf nodes beforehand. Therefore, as the function's name suggests, it will update the iterator with the processing of each character.
I'm using iterators instead of a string and indices. They are clearer and easier to work with in my opinion — changing it back should be fairly easy anyway.
The second function is basically syntactic sugar: it is just there so that you don't have to pass an lvalue as the first argument.
You can then just call p_createTree with:
int main()
{
std::string str = "050067089";
UniqueNode p_root = p_createTree(str.begin(), str.end());
return 0;
}
I also wrote a function to print out the tree's nodes for debugging:
void printTree(const UniqueNode& p_root, int indentation = 0)
{
// Print the value of the node
for (int i(0); i < indentation; ++i)
std::cout << "| ";
std::cout << p_root->value << '\n';
// Do nothing more in case of a leaf node
if (!p_root->p_left.get() && !p_root->p_right.get())
;
// Otherwise, print a blank line for empty children
else
{
if (p_root->p_left.get())
printTree(p_root->p_left, indentation + 1);
else
std::cout << '\n';
if (p_root->p_right.get())
printTree(p_root->p_right, indentation + 1);
else
std::cout << '\n';
}
}
Assuming that the code which is not included in your question is correct, there is only one issue that could pose a problem if more than one tree is built. The problem is that x is a global variable which your functions change as a side-effect. But if that x is not reset before creating another tree, things will go wrong.
It is better to make x a local variable, and pass it by reference.
A minor thing: don't use NULL but nullptr.
Below your code with that change and the class definition included. I also include a printSideways function, which makes it easier to see that the tree has the expected shape:
#include <iostream>
using namespace std;
class Node {
public:
int m_Data;
Node* m_Left = nullptr;
Node* m_Right = nullptr;
Node(int v) : m_Data(v) {}
};
// Instead of si, accept x by reference:
Node* createTree(string str, int &x, int ei)
{
if (x >= ei)
return nullptr;
Node *root = new Node((str[x] - '0'));
if(str[x] != '0')
{
root->m_Data = (str[x] - '0');
x++;
return root;
}
if(str[x]=='0')
{
x++;
root->m_Left = createTree(str,x,ei);
root->m_Right = createTree(str,x,ei);
}
return root;
}
// Overload with a wrapper that defines x
Node* createTree(string str)
{
int x = 0;
return createTree(str, x, str.length());
}
// Utility function to visualise the tree with the root at the left
void printSideways(Node *node, string tab) {
if (node == nullptr) return;
printSideways(node->m_Right, tab + " ");
cout << tab << node->m_Data << "\n";
printSideways(node->m_Left, tab + " ");
}
// Wrapper for above function
void printSideways(Node *node) {
printSideways(node, "");
}
int main ()
{
string str = "050067089";
Node *node = createTree(str);
printSideways(node);
return 0;
}
So, as you see, nothing much was altered. Just si was replaced with x, which is passed around by reference, and x is defined locally in a wrapper function.
Here is the output:
9
0
8
0
7
0
6
0
5

C++ Binary Tree Path finding

I have a question regarding finding sum of a path of a binary int tree. This is for college so the requirements are as follows:
Take your code from Lab Sheet 3B (Binary Tree) for a binary tree of integers, and code a
method called hasPathSum() which given a binary tree and a sum, return true if the tree
has a root-to-leaf path such that adding up all the values along the path equals the given
sum. Return false if no such path can be found. The function prototype is
int hasPathSum(struct node* node, int sum)
Note: a "root-to-leaf path" is a sequence of nodes in a tree starting with the root node
and proceeding downward to a leaf (a node with no children). An empty tree contains
no root-to-leaf paths. So for example, the following tree has exactly four root-to-leaf
paths:
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
Root-to-leaf paths:
path 1: 5 4 11 7
path 2: 5 4 11 2
path 3: 5 8 13
path 4: 5 8 4 1
For this problem, we will be concerned with the sum of the values of such a path -- for
example, the sum of the values on the 5-4-11-7 path is 5 + 4 + 11 + 7 = 27.
I am having trouble with this. I have a binary tree, but the function hasPathSum() requirs to pass a node, not a tree. I canot figure out how to do this. I also don't know how to find the sum of a path from root to leaf (the hasPathSum body as well). This needs to be done recursively.
Any help is greatly appreciated.
Here is my node class:
#include <stdio.h>
#pragma once
struct TreeNode
{
public:
friend class BinaryTree;
TreeNode(int theData) : data(theData) {}
bool isLeaf();
private:
int data;
TreeNode *leftlink;
TreeNode *rightLink;
};
Here is the BinaryTree header file:
#include "TreeNode.h"
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
#pragma once
class BinaryTree
{
public:
BinaryTree();
void add(int toadd);
int height();
void inorderShow() const;
int hasPathSum(TreeNode * tree, int sum);
private:
void add(TreeNode *toAdd, TreeNode *& addHere);
int height(TreeNode *& root);
TreeNode *root;
void inorderShow(TreeNode *subTree) const;
};
And my BinaryTree cpp file:
#include "BinaryTree.h"
BinaryTree::BinaryTree()
{
}
void BinaryTree::add(int toAdd)
{
TreeNode *node = new TreeNode(toAdd);
add(node, root);
}
int BinaryTree::height()
{
return height(root);
}
void BinaryTree::add(TreeNode * toAdd, TreeNode *& addHere)
{
if (addHere == NULL)
addHere = toAdd;
else if (toAdd->data < addHere->data)
add(toAdd, addHere->leftlink);
else //toAdd->data >= addHere->data
add(toAdd, addHere->rightLink);
}
int BinaryTree::height(TreeNode *& n)
{
if (n == NULL)
return -1;
else
return 1 + max(height(n->leftlink), height(n->rightLink));
}
void BinaryTree::inorderShow(TreeNode * subTree) const
{
if (subTree != NULL)
{
inorderShow(subTree->leftlink);
cout << subTree->data << " ";
inorderShow(subTree->rightLink);
}
}
void BinaryTree::inorderShow() const
{
inorderShow(root);
}
int BinaryTree::hasPathSum(TreeNode * tree, int sum)
{
}
In the main.cpp, I have a tree as follows:
#include <iostream>
#include "BinaryTree.h"
#include "TreeNode.h"
using namespace std;
int main()
{
BinaryTree tree;
tree.add(5);
tree.add(6);
tree.add(3);
tree.add(4);
tree.add(9);
tree.add(11);
cout << "Height of the tree is: ";
cout << tree.height() << " ";
cout << "\nIn Order Show:" << endl;
tree.inorderShow();
cout << "Root to leaft path: " << endl;
cout << endl;
system("pause");
return 0;
}
Is someone could explain how can I accomplish this task and meet the requirements (aka not change the function hasPathSum() parameters) I would really appreciate that.
Seems to me the requirement is wrong (or maybe confused)
and code a method called hasPathSum() which given a binary tree and a
sum
So given that this is a method of the binary tree class the tree is passed implicitly so the only explicit parameter is the sum. So the method should be declared as
class BinaryTree
{
...
bool hasPathSum(int sum);
...
};
However the given signature is
int hasPathSum(struct node* node, int sum)
which has the wrong return type (int not bool) and an unexplained node parameter.
Here's how I would organise the solution, since it involves two methods, it maybe explains the confusion.
class BinaryTree
{
...
public:
bool hasPathSum(int sum) { return hasPathSumHelper(root, sum); }
...
private:
static bool hasPathSumHelper(TreeNode* node, int sum);
};
The public hasPathSum method has the signature implied by the problem description (the only signature that makes sense). It simply calls a private method hasPathSumHelper passing the root node and the sum, this gets round the problem of how you pass the private root node.
The hasPathSumHelper method is the recursive routine where the real work is done (left for you to implement). The public hasPathSum just kicks off the calculation by calling this method.
As you think about how to implement the hasPathSumHelper you might find it useful to add additional parameters (a sum_so_far parameter which, as you descend the tree, is the sum of all the nodes above you makes sense to me). That's OK because it's a private method, you can add what you like.

Undefined reference c++, unusual case

I'm creating a queue class in c++ and am having trouble compiling with a makefile. My queue.cpp class is here
#include "queue.h"
#include <stdlib.h>
queue::queue()
{
front_p = NULL;
back_p = NULL;
current_size = 0;
}
void queue::enqueue(int item)
{
node newnode = node(item, NULL);
if (front_p == NULL) //queue is empty
{
front_p = &newnode;
back_p = &newnode;
}
else
{
back_p->next = &newnode;
back_p = &newnode;
}
current_size ++;
}
My header file (queue.h) is here
class queue
{
public:
queue(); // constructor - constructs a new empty queue.
void enqueue( int item ); // enqueues item.
int dequeue(); // dequeues the front item.
int front(); // returns the front item without dequeuing it.
bool empty(); // true iff the queue contains no items.
int size(); // the current number of items in the queue.
int remove(int item); // removes all occurrances of item
// from the queue, returning the number removed.
private:
class node // node type for the linked list
{
public:
node(int new_data, node * next_node ){
data = new_data ;
next = next_node ;
}
int data ;
node * next ;
};
node* front_p ;
node* back_p ;
int current_size ; // current number of elements in the queue.
};
test program (tester.cpp)
#include <iostream>
#include "queue.h"
#include <stdlib.h>
using namespace std;
int main(int argc, char * const argv[])
{
cout << "Lalalalala" << endl;
queue q1;
q1.enqueue(5);
}
makefile
all: tester
tester: queue.o
g++ -o tester tester.cpp
queue.o: queue.cpp queue.h
g++ -c queue.cpp
clean:
rm -f tester *.o
when I type "make" or "make all" I get this error:
g++ -o tester tester.cpp
/tmp/ccTOKLWU.o: In function `main':
tester.cpp:(.text+0x33): undefined reference to `queue::queue()'
tester.cpp:(.text+0x44): undefined reference to `queue::enqueue(int)'
collect2: ld returned 1 exit status
make: *** [tester] Error 1
The unusual thing about it, is that when compiled in visual studio on a windows machine, there are no errors. I haven't the faintest idea why it shouldn't compile on a linux machine in the way I am doing so. Would anyone kindly explain?
Your makefile is incorrect - it compiles tester.cpp with a dependency on queue.o, but it does not link queue.o at all. That is why the compilation of tester.cpp results in an unresolved reference.
You should change your make file as follows:
all: tester
tester: queue.o tester.o
g++ queue.o tester.o -o tester
tester.o: tester.cpp tester.h
g++ -c tester.cpp
queue.o: queue.cpp queue.h
g++ -c queue.cpp
clean:
rm -f tester *.o

Stored value disappears when setting a struct pointer to null in C++

I'm writing a C++ application to do a word search across a large database of song lyrics. to start, I'm taking each word and putting it into a Word struct that looks like this:
struct Word{
char* clean;
int size;
int position;
SongId id;
Word* same;
Word* diff;
};
I have a "makeNode" function that does the following:
takes in each word
creates a new Word struct and adds the word to it
creates a Word* called node which points to the new word
stores the pointer in a hash table.
In my makeNode function, I set node->clean to my "clean" word. I can print the word by cout'ing node->clean. But when I set node->same to NULL, I lose node->clean. I don't lose node->position or node->size. If I remove the line where I assign node->same to to NULL, I do not lose node->clean.
char* clean = cleanse(word);
Word* node = new Word;
node->size = strlen(word);
node->clean = clean;
cout<<"MADE NODE FOR "<<node->clean<<endl;
node->position = position;
cout<<"4 node clean: "<<node->clean<<endl;
node->id = id;
cout<<"5 node clean: "<<node->clean<<endl;
node->same = NULL;
cout<<"6 node clean: "<<node->clean<<endl;
cout<<"node position: "<<node->position<<endl;
cout<<"node size: "<<node->size<<endl;
node->diff = NULL;
yields the following output:
MADE NODE FOR again
4 node clean: again
5 node clean: again
6 node clean:
node position: 1739
node size: 6
0 node clean:
1 node clean:
3 node clean:
Can anyone help me get past this error? If you need more info, let me know. Thanks in advance!
EDIT: here is the cleanse function.
char* SongSearch::cleanse(char* dirty)
{
string clean;
int iter = 0;
while (!isalnum(dirty[iter]))
{
iter++;
}
while(dirty[iter]!='\0')
{
clean += dirty[iter];
iter++;
}
int backiter = clean.length() - 1;
while(!isalnum(clean[backiter]))
{
clean.erase(backiter, 1);
backiter--;
}
char c;
for (int i = 0; i<clean.length(); i++)
{
c = tolower(clean[i]);
clean[i] = c;
}
char* toReturn = (char*)(clean.c_str());
return toReturn;
}
The problem is probably that in cleanse, you return clean.c_str().
That pointer value ceases to be valid when clean ceases to exist, which is when the function exits. It is no longer guaranteed to point to anything, so it's pure luck that you're ever seeing the string "again" as expected.
What I suspect happens is that the memory that used to be occupied by the data for the string clean in cleanse, has been re-used for the structure word, but is not immediately overwritten. It just so happens that the byte that used to hold the first a now holds part of the same member of your struct. So, when you write a null pointer to node->same, it has the effect of writing a 0 byte to the location pointed to by node->clean. Thereafter, it appears to point to an empty string.
You need to reduce your code to a minimal example which displays the problem, and post that.
The following code fails to display the problem. The contents of main and the definition of Word are copied from your code, then I have added code as necessary to get it to compile:
#include <iostream>
#include <cstring>
using namespace std;
typedef int SongId;
struct Word{
char* clean;
int size;
int position;
SongId id;
Word* same;
Word* diff;
};
char *cleanse(const char *w) {
return (char *)w;
}
const char *word = "again ";
const int position = 1739;
const int id = 0;
int main() {
char* clean = cleanse(word);
Word* node = new Word;
node->size = strlen(word);
node->clean = clean;
cout<<"MADE NODE FOR "<<node->clean<<endl;
node->position = position;
cout<<"4 node clean: "<<node->clean<<endl;
node->id = id;
cout<<"5 node clean: "<<node->clean<<endl;
node->same = NULL;
cout<<"6 node clean: "<<node->clean<<endl;
cout<<"node position: "<<node->position<<endl;
cout<<"node size: "<<node->size<<endl;
node->diff = NULL;
}
Output is:
MADE NODE FOR again
4 node clean: again
5 node clean: again
6 node clean: again
node position: 1739
node size: 6
Okay, we'd need to actually see the code for some of these to be sure, but here's what the bug is telling you: at some point, you're assigning to something that overwrites or deletes your clean. Since y,ou declare it as a char *, I'm guessing you use it as a pointer to an array of characters, and the odds are good that one array is being aliased to two "clean" pointers in two different Words.
Aside from new and cout this might as well be C.
Some other reading
What are the differences between struct and class in C++?
char * Vs std::string
Remove spaces from std::string in C++
tolower function for C++ strings
How can I negate a functor in C++ (STL)?
Try the following alternative (uncompiled sample)
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
typedef int SongId;
class Word{
int position;
SongId id;
Word* same;
Word* diff;
public:
const std::string word;
const int size() const { return clean.length() };
Word( const std::string& word_, const int position_ = 1739, const int id_ = 0 )
: clean( cleanse(word_) )
, position( position_ )
, id( id_ )
, same( NULL )
, diff( NULL )
{
cout<<"MADE NODE FOR "<< word_ << "\n"
<<"node clean: "<< word << "\n"
<<"node position: "<< position << "\n";
<<"node size: "<< size() << endl;
}
static std::string cleanse( const std::string& dirty)
{
string clean( dirty );
// Remove anything thats not alpha num
clean.erase(remove_if(clean.begin(), clean.end(), std::not1(::isalnum) ), clean.end());
// make it lower case
std::transform( clean.begin(), clean.end(), clean.begin(), ::tolower); // or boost::to_lower(str);
return clean;
}
};
const char *word = "again ";
int main() {
Word* node = new Word(word);
}

help with insert on first BST

EDIT there a small thing that I am missing!! the error is still there
So I am attempting to learn how to code my first BST, and it is hard.... I am already having trouble with just a few lines of codes. the problem is in the insert, but I have included everything so that I could get some feedback on my style/other errors. I was suggested to use a pointer to pointer implementation, but we havent learned it yet, so I dont feel comfort/know how to code it yet. the
error is
[trinhc#cs1 Assignment_3]$ g++ movieList.cpp -o a.out
/tmp/ccLw6nsv.o: In function `main':
movieList.cpp:(.text+0x7a): undefined reference to `Tree::Tree()'
movieList.cpp:(.text+0xa7): undefined reference to `Tree::insert(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
the tree.h file
#ifndef TREE_H
#define TREE_H
#include <string>
#include <iostream>
using namespace std;
class Tree
{
public:
Tree();
bool insert(int k, string s);
private:
struct Node
{
int key;
string data;
Node *left;
Node *right;
};
Node* root;
bool insert(Node*& root, int k, string s);
};
#endif
tree.cpp
#include <iostream>
#include "tree.h"
#include <stack>
#include <queue>
#include <string>
using namespace std;
Tree::Tree()
{
root = NULL;
}
bool Tree::insert(int k, string s)
{
return insert(root, k, s);
}
bool Tree::insert(Node*& current_root, int k, string s)
{
if(root == NULL){
current_root = new Node;
current_root->key = k;
current_root->data = s;
current_root->left = NULL;
current_root->right = NULL;
return true;
}
else if (current_root->key == k)
return false;
else if (current_root->key > k)
insert(current_root->left, k, s);
else
insert (current_root->right,k, s);
}
movieList.cpp
#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include "tree.h"
using namespace std;
int main()
{
Tree test;
test.insert(100, "blah");
return 0;
}
Tree test(); is not how define an object of class Test, This acutally declare function named test which returns Tree.
try
Tree test;
test.instert(100, "blah");
return 0;
I copied some of your code and this is working fine for me:
main:
#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include "tree.h"
int main()
{
Tree test;
test.insert(100, "blah");
test.insert(50, "fifty");
test.insert(110, "one hundred ten");
return 0;
}
Insert function:
bool Tree::insert(Node*& currentRoot, int k, string s)
{
if(currentRoot == NULL){
currentRoot = new Node;
currentRoot->key = k;
currentRoot->data = s;
currentRoot->left = NULL;
currentRoot->right = NULL;
return true;
}
else if (currentRoot->key == k)
return false;
else if (currentRoot->key > k)
insert(currentRoot->left, k, s);
else
insert (currentRoot->right,k, s);
}
Other than that you have syntax errors all over the place. I also changed the name because as someone pointed out there was a bit of a naming problem. CurrentRoot makes sense because you are passing it the root of the left or right subtree on every recursion.
Couple of points:
You need to change the name of your member variable root to something else– I'd recommend m_root, or my_root, or tree_root, or something of those sorts. Right now you've got a little bit of a namespace clash in any function where you include root as an argument. This will also let you keep track of which root you're referring to.
bool Tree::insert(Node*& root, int k, string s)
{
if(root == NULL){
root = new Node;
root->key = k;
root->data = s;
root->left = NULL;
root->right = NULL;
return true;
} else
if (root == k) //Comparison between pointer and number.
return false;
else
if (root->key > k)
insert(root->left, k, s);
else
insert (root->right,k, s);
}
You need to change root on the commented line to root->key.
Other than that, it looks like it'll work.
EDIT: Also, what the other guy said. You declare an object as
TYPE name ()
if you are calling the default constructor (), so your code in your main function should be
Tree test;
test.insert(...)
Shouldn't you add tree.cpp to your build command?
[trinhc#cs1 Assignment_3]$ g++
movieList.cpp -o a.out
Would become
[trinhc#cs1 Assignment_3]$ g++
tree.cpp movieList.cpp -o a.out