Identifier not found - c++

So for my assignment, I am supposed to implement a Node class that just contains data and pointers to its two siblings and a BinaryTree that reads in these Nodes and creates a binary tree out of them. My problem is pointing to the root of the Tree does not seem to work. Any help you can provide would be appreciated!
Note: The error is found a few lines into the addNode method in the BinaryTree.cpp file which can be found at the end of the question. Also, I am not able to access the value of size either, so I believe this is some sort of weird scope issues I cannot resolve. I also cannot use the "this" keyword in the addNode function.
I am also not allowed to use structs, per my homeworks' instruction.
Node.H
#include <iomanip>
using namespace std;
class Node
{
public:
int data;
Node* leftChild;
Node* rightChild;
Node(int data, Node* leftChild, Node* rightChild);
};
Node.cpp
#include <iomanip>
#include <iostream>
#include "Node.h"
using namespace std;
Node::Node(int data, Node* leftChild, Node* rightChild)
{
this->data = data;
this->leftChild = leftChild;
this->rightChild = rightChild;
}
BinaryTree.H
#include <iomanip>
#include "Node.h"
using namespace std;
class Tree
{
public:
Tree(int data);
void addNode(int data);
void inOrder(Node* N);
protected:
Node* root;
int size;
int data;
private:
int printNode(Node* N);
};
BinaryTree.cpp
#include <iostream>
#include <iomanip>
#include "BinaryTree.h"
using namespace std;
//Tree constructor. Sets the values of data, size, and root.
Tree::Tree(int data)
{
this->data = data;
this->size = 0;
this->root = new Node(data, NULL, NULL);
}
//Adds a node to the current Tree.
void addNode(int data)
{
Node* tempNode = new Node(data, NULL, NULL);
Node* current = root; //THIS IS THE ERROR LINE.
while(current!=NULL)
{
//If the data we are trying to add is already in the Tree
if(current->data == tempNode->data)
{
cout << "Data already in the Tree.";
}
//If the data for the new node is larger than the old
else if(current->data < tempNode->data)
{
//See if the right child is null. If so, add the tree node there.
if(current->rightChild == NULL)
{
current->rightChild = tempNode;
return;
}
//Otherwise, traverse down the right tree.
else
{
current = current->rightChild;
}
}
//The data is smaller than the current node
else
{
//See if the left child is null. If so, add the tree node there.
if(current->leftChild == NULL)
{
current->leftChild = tempNode;
return;
}
//Otherwise, traverse down the left tree
else
{
current = current->leftChild;
}
}//End of leftChild Else
}//End of while
}//End of addNode

void addNode(int data)
should be:
void Tree::addNode(int data)
as it is a member function of class Tree

//Adds a node to the current Tree.
void addNode(int data)
Should be:
//Adds a node to the this Tree
void Tree::addNode(int data)

Related

Function for inserting a value in binary tree?

I have a function insert that is used to insert values into the Binary tree.
But when I log out the value nothing is shown.
I'm aware of the insertion using member function.
The root node's value is not being updated?
Could someone tell me where I'm going wrong?
#include <iostream>
using namespace std;
class Node{
public:
int value;
Node* left;
Node* right;
Node();
Node(int data){
value = data;
left = NULL;
right = NULL;
}
};
void insert(Node* root , int val){
if(root == NULL){
root = new Node(val);
return;
}
if(root->value > val)
insert(root->left,val);
else
insert(root->right,val);
}
int main()
class Node* root = NULL;
insert(root,5);
cout<<root->value;
}
you are inserting position on the right place but the problem is you are not creating the link of your newly inserted node to it's parent.
you can check this as reference!

Creating a binary Tree and search function

I am creating a binary tree and want to just Search function but I want to know how many nodes are visited to find a value. in the search function.
Here is the hearder file
#ifndef INTBINARYTREE_H
#define INTBINARYTREE_H
class IntBinaryTree
{
private:
struct TreeNode
{
int value; // The value in node .
TreeNode *left; //pointer to left node
TreeNode *right; // Pointer to right child node
};
TreeNode *root;
//private member functions
void insert(TreeNode *&,TreeNode *&);
void displayInOrder(TreeNode *) const;
void displayPreOrder(TreeNode *) const;
void displayPostOrder(TreeNode *) const;
public:
IntBinaryTree()
{
root = nullptr;
}
// Binary search tree
int searchNode(int);
void insertNode(int);
void displayInOrder() const
{
displayInOrder(root);
}
#endif // INTBINARYTREE_H
And here is the .cpp File I want to know how to for the search function if a value is not found zero and if value is found how many nodes are visited ?
#include "IntBinaryTree.h"
void IntBinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode)
{
if (nodePtr == nullptr)
nodePtr=newNode; // insert the node
else if (newNode->value < nodePtr->value) `//search the left branch`
insert(nodePtr->left, newNode);
else
insert(nodePtr->right, newNode); //search the right branch
}
void IntBinaryTree::insertNode(int num)
{
TreeNode *newNode= nullptr; // pointer to a new node
//create a new node and store num in it
newNode = new TreeNode;
newNode->value= num;
newNode->left = newNode->right = nullptr;
//insert the node
insert(root, newNode);
}
int IntBinaryTree::searchNode(int num)
{
TreeNode *nodePtr = root;
while(nodePtr)
{
if (nodePtr->value==num)
{
cout<<"Node found"<<num<<endl;
}
else if (num < nodePtr->value)
nodePtr = nodePtr->left; // look to the left side of the branch if less than the value of the node
else
nodePtr = nodePtr->right; // look to the right side of the if not less than the value .
}
return 0;
}
Here is the Mainfile
#include <iostream>
#include "IntBinaryTree.h"
using namespace std;
int main()
{
IntBinaryTree tree;
cout << "inserting nodes" << endl;
tree.insertNode(5);
tree.insertNode(8);
tree.insertNode(3);
tree.insertNode(12);
tree.insertNode(9);
cout << "Done.\n";
tree.searchNode(5);
return 0;}
Can you please code it for me and edit and explain it briefly how does it work ?
Your includes are wrong.
There is a #include "IntBinaryTree.cpp" in your main.cpp. Because of this the insert member (and many others) exist twice. General rule: Never include cpp files.
Just remove the line, and you should be fine.

Root node should have an assigned node, but remains NULL. Why can't I assign a node to my root?

I'm writing a binary tree in object-oriented format. I've had experience with binary trees before, but it's been a while since I've touched on this. My problem is that I'm unable to assign a node to my root. Every time I check in debugging mode, the root remains NULL. While this is happening, the cur node contains all the information it's assigned.
I've tried making my root private and changing this->root = NULL; to root-> = NULL;. I've also tried making all of my functions public, but it didn't make a difference. I tried declaring root's children to NULL values and name to an empty string as well.
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "Friends.h"
using namespace std;
int main() {
string line;
ifstream file;
file.open("friends.txt");
Friends f;
while (getline(file, line)) {
f.insert(f.root, line);
}
f.print(f.root);
system("pause");
return 0;
}
Friends.cpp
#include "Friends.h"
#include <iostream>
#include <string>
using namespace std;
Friends::Friends() {
this->root = NULL;
}
Friends::node* Friends::createNode(string& val) {
node* newNode = new node();
newNode->left = NULL;
newNode->right = NULL;
newNode->name = val;
return newNode;
}
Friends::node* Friends::insert(node* cur, string& val) {
if (!cur) {
cur = createNode(val);
}
else if (val < cur->name) {
insert(cur->left, val);
return cur;
}
else if (val > cur->name) {
insert(cur->right, val);
return cur;
}
return NULL;
}
void Friends::print(node* cur) {
if (!cur) {
return;
}
print(cur->left);
cout << cur->name << endl;
print(cur->right);
}
Friends.h
#ifndef FRIENDS_H
#define FRIENDS_H
#include <string>
using namespace std;
class Friends {
private:
struct node {
string name;
node* left;
node* right;
};
public:
node* root;
node* insert(node* cur, string&);
void print(node* cur);
Friends();
node* createNode(string&);
};
#endif
The root node should have a node, but has keeps showing up as a NULL value. It doesn't run with any errors either. It just remains as NULL.
change from:
node* insert(node* cur, string&);
to :
node* insert(node* &cur, string&);
should fix
Of course the implementation header should also be changed

Simple Binary Search Tree Non recursive Add function

I am trying to write a BST() that would take in strings as nodes without using recursion. here is the add function of my code, can you please review and see if it is easy to understand/follow and point out mistakes. I am new to programming and would appreciate any feedback.
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main() {
class BST {
private:
struct Node {
string value;
Node* left;
Node* right;
};
Node* root;
public:
BST()
{
root = NULL;
}
~BST()
{
stack<Node*> nodes;
nodes.push( root);
while( ! nodes.empty())
{
Node* topNode = nodes.top();
nodes.pop();
if ( topNode->left )
nodes.push(topNode->left);
if ( topNode->right )
nodes.push(topNode->right);
delete topNode;
}
}
Node* GetNewNode(string data){
Node* newNode = new Node();
newNode->value=data;
newNode->left = newNode->right = NULL;
return root;
}
Node* Insert(Node* rootPtr,string data){
if(rootPtr == NULL){
rootPtr = GetNewNode(data);
return rootPtr;
}
else if(data<= rootPtr->value){
rootPtr->left = Insert(rootPtr->left,data);
}
else {
rootPtr->right = Insert(rootPtr->right,data);
}
return rootPtr;
}
};
}
1 - In the insert function:
while (root != NULL) { // this shouldn't be root, the root isn't the node that traverse your tree, this has to be current
.....
}
2- you never add your new node, you just keep looping till you reach a null.
You should traverse your tree till u find the right position to insert your node then add the new node, something like:
current = root;
prev = current;
while (current!= NULL) {
prev = current;
if (current->value.compare(word) < 0)
current = current->left;
else
current = current->right;
}
//here your new node should be on the left or the right of the prev node
3 - in "GetNewNode" return the new node not the root
4 - your delete function is a mess, u shall think about it again and re-implement it.
Finally I strongly recommend you to check and understand a ready made implementation from the web then try to write your BST again.

Linked List Printing 0 value at beginning c++

I am refreshing my c++ by creating a simple linked list class. What I am having problems is when I try to print the list, there is a zero printing at the beginning of the list. How can I get rid of this? Also, I am having trouble with my second constructor. How would I go about this?`
Here is the code
List.h
#ifndef NODE_H
#define NODE_H
class List{
private:
typedef struct Node{
int data;
struct Node* next;
}* node;
node head;
int listLength;
public:
List();
List(int data, node nextLink);
void printList();
void push(int data);
void Delete(int d);
int listSize(void);
};
my List.cpp
#endif
#include "node.h"
#include <iostream>
using namespace std;
List::List(){
head->data=0;
head->next= NULL;
listLength=0;
}
List::List(int data, node nextLink){
head=NULL;
listLength++;
}
void List::push(int data){
if(head==NULL){
head->data=data;
head->next= NULL;
}
else{
node cursor = head;
while(cursor->next != NULL)
cursor = cursor -> next;
node newNode= new Node;
newNode->data=data;
newNode->next=NULL;
cursor->next= newNode;
}
listLength++;
}
void List::printList(){
node cursor=head;
while(cursor!=NULL){
//if(cursor->data==0){cursor=cursor->next;}
if(cursor->next==NULL){
cout<<cursor->data<<endl;
return;
}
else{
cout<<cursor->data<<" -> ";
cursor=cursor->next;
}
}
cout<<endl;
}
int main(){
List li;
li.push(2);
li.push(3);
li.push(0);
li.push(4);
li.printList();
return 0;
}
You never initialize your head node, so you're writing to unallocated memory in the code below.
if(head==NULL){
head->data=data;
head->next= NULL;
}
It should be:
if(head==NULL){
head = new Node; // added this line
head->data=data;
head->next= NULL;
}
You also probably want the first constructor
List::List(){
head->data=0;
head->next= NULL;
listLength=0;
}
to instead be
List::List(){
head = NULL;
listLength=0;
}
As for the second constructor, I assume you want something like this?
List::List(int data, node nextLink){
head = new Node;
head->data = data;
head->next = nextLink;
listLength = 1;
}
If not, could you better explain what you want?
I would also note that it would be generally considered good programming practice to create a constructor for the Node struct that initializes next to NULL, and then you wouldn't have to set it explicitly every time you create a new Node throughout your code.