This is the part of code where I want the createNode function to return the node that it creates.
struct Node *ptr = createNode(key);
if(key<prev->data)
{
prev->left = ptr;
}
else
{
prev->right = ptr;
}
createNode Function :-
Node * tree :: createNode(int key). //Unknown type name 'Node'
{
Node * n = new Node;
n->data = key;
n->left = NULL;
n->right = NULL;
return n;
}
declaration of function in class:-
Node * createNode(int key);
structure of Node :-
struct Node
{
int data;
Node *left;
Node *right;
};
when I define the function createNode i'am getting the error that -> Unknown type name 'Node'
how can I define the function whose return type is pointer.
#include <iostream>
#include<stdlib.h>
using namespace std;
class tree
{
struct Node
{
int data;
Node *left;
Node *right;
};
public:
Node * toDelete(Node *root, int key);
void insert(Node *root,int key);
Node * createNode(int key);
int isBST(Node* root);
void preorder(Node *root);
void postorder(Node *root);
void inorder(Node *root);
Node * search(Node *root,int key);
Node * searchIter(Node *root,int key);
Node * findmin(Node *root);
};
void tree :: preorder(Node *root)
{
if(root != NULL)
{
cout<<root->data<<"\t";
preorder(root->left);
preorder(root->right);
}
}
void tree :: postorder(Node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
cout<<root->data<<"t";
}
}
void tree :: inorder(Node *root)
{
if(root!=NULL)
{
inorder(root->left);
cout<<root->data<<"\t";
inorder(root->right);
}
}
void tree :: insert(Node *root,int key)
{
struct Node *prev = NULL;
while(root!=NULL)
{
prev = root;
if(key<root->data)
{
root = root->left;
}
else
{
root = root->right;
}
}
struct Node *ptr = createNode(key);
if(key<prev->data)
{
prev->left = ptr;
}
else
{
prev->right = ptr;
}
}
Node * tree :: createNode(int key) //Unknown type name 'Node'
{
Node * n = new Node;
n->data = key;
n->left = NULL;
n->right = NULL;
return n; //Cannot initialize return object of type 'int *' with an lvalue of type 'tree::Node *'
}
int main()
{
}
Keep Node outside tree class.
struct Node
{
int data;
Node* left;
Node* right;
};
class tree
{
....
Node is private to your tree class. Hence, it is not directly accessible outside the class the way you are trying in your code.
Related
struct node {
int data{};
node *right{nullptr};
node *left{nullptr};
};
class BTree {
private:
node *root;
void insert(node *sr, int num);
public:
BTree();
void buildTree(int num);
};
void BTree::insert(node *sr, int num) {
if (sr == nullptr) {
sr = new node;
sr->data = num;
} else {
if (num < sr->data)
insert(sr->left, num);
else
insert(sr->right, num);
}
}
int main() {
BTree tree;
tree.buildTree(3);
return 0;
}
I am using the above insert method to add a node to Binary Search Tree. But this method is unable to add the node , if i add a number as its root or first node the root remains nullptr.
How do i resolve this issue.
At the first root is nullptr , and I am sending root i.e the nullptr as the argument. As nullptr does refer to any node therefor the root is not getting updated by the operation in the method.
possible solution:
use pointer to pointer, so that address of the node can be passed and and changed.
directly acces the root to do changes.
PROTOTYPE
void insert(node **sr, int num);
BUILD TREE METHOD
void BTree::buildTree(int num) {
insert(&root, num);
}
INSERT METHOD
void BTree::insert(node **sr, int num) {
if (*sr == nullptr) {
*sr = new node;
(*sr)->data = num;
} else {
if (num < (*sr)->data)
insert(&((*sr)->left), num);
else
insert(&((*sr)->right), num);
}
}
I have the following c++ structure
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
I have a function which sets TreeNode's children to null if their subtrees do not contain the value 1.
bool modify(TreeNode *root)
{
if(root==NULL)
return false;
if(root->val!=1)
{
bool l = modify(root->left);
bool r = modify(root->right);
if(l||r)
{
return true;
}
else
{
root = NULL;
return false;
}
}
else
return true;
}
How do I pass TreeNode *root by reference so that modifications made inside modify() are persisted?
i'm trying to implement binary tree, pre order traversal using stack.
here its popping the last left node and after that root=root->right doesnt seem to work. Pls help. here 7 is being popped out and is being displayed and after the the program is ending.
all the functions are working yet not the desired output
#include<stdio.h>
#include<stdlib.h>
#define maxsize 100
int a[maxsize];
int top=-1;
struct node{
int data;
struct node *left, *right;
};
struct node *newNode(int data)
{
struct node *nn;
nn=(struct node *)malloc(sizeof(struct node));
if(!nn)
return;
nn->data=data;
nn->left=nn->right=NULL;
return nn;
};
void push(struct node *root)
{
printf("pushcalled\n");
if(top!=maxsize-1)
a[++top]=root;
}
int isempty()
{
return(top==-1);
}
struct node *pop()
{
printf("popcalled\n");
if(top!=-1)
{
return a[top];
top--;
}
}
void deleteStack()
{
free(a[top--]);
}
void preorder(struct node *root)
{
while(1)
{
while(root)
{
printf("%d\t",root->data);
push(root);
root=root->left;
}
printf("hello\n");
if(isempty())
break;
printf("hello\n");
root=pop();
printf("Popped data is:%d\n",root->data);
root=root->right;
printf("right data is:%d\n",root->data);
}
deleteStack();
}
int main()
{
int data;
struct node *root=newNode(10);
root->left = newNode(11);
root->left->left = newNode(7);
root->right = newNode(9);
root->right->left = newNode(15);
root->right->right = newNode(8);
preorder(root);
return 0;
}
Your logic is correct but there are some errors in your code.
root=root->right;
printf("right data is:%d\n",root->data);
you should check whether the root is null or not before you try to access root->data. That is why you are getting a segmentation fault. So put a condition if(root!=NULL) above printf() statement.
Another mistake is in the implementation of stack's pop.
struct node *pop()
{
printf("popcalled\n");
if(top!=-1)
{
return a[top];
top--;
}
}
it should be like this,
struct node *pop()
{
printf("popcalled\n");
if(top!=-1)
{
struct node* temp = a[top];
top--;
return temp;
}
}
In your code, when you return a[top]; the line below it i.e. top--; never executes and the value of top remains same.
I am trying to create a function that will insert a node to the back of the list using linked list. I am new to using linked list and I have tried many different ways of doing an insertion at the end of the list but nothing seems to be working. The main passes the values one at a time to be inserted 2 4 5 8 9, and the output is 2 0 0 0 0. I do not whats causing this problem.
.h
class Node
{
public:
Node() : data(0), ptrToNext(NULL) {}
Node(int theData, Node *newPtrToNext) : data(theData), ptrToNext(newPtrToNext){}
Node* getPtrToNext() const { return ptrToNext; }
int getData() const { return data; }
void setData(int theData) { data = theData; }
void setPtrToNext(Node *newPtrToNext) { ptrToNext = newPtrToNext; }
~Node(){}
private:
int data;
Node *ptrToNext; //pointer that points to next node
};
class AnyList
{
public:
AnyList();
//default constructor
void print() const;
//Prints all values in the list.
void destroyList();
//Destroys all nodes in the list.
~AnyList();
//destructor
int getNumOfItems();
void insertBack(int b);
void deleteFirstNode();
private:
Node *ptrToFirst; //pointer to point to the first node in the list
int count; //keeps track of number of nodes in the list
};
.cpp
void AnyList::insertBack(int b)
{
Node *temp = new Node;
if (ptrToFirst == NULL)
{
temp->setData(b);
ptrToFirst = temp;
}
else
{
Node *first = ptrToFirst;
while (first->getPtrToNext() != NULL)
{
first = first->getPtrToNext();
}
first->setPtrToNext(temp);
}
}
First, you really should be using the std::list or std::forward_list class instead of implementing the node handling manually:
#include <list>
class AnyList
{
public:
void print() const;
//Prints all values in the list.
void destroyList();
//Destroys all nodes in the list.
int getNumOfItems() const;
void insertBack(int b);
void deleteFirstNode();
private:
std::list<int> nodes; //nodes in the list
};
void AnyList::print() const
{
for (std::list<int>::const_iterator iter = nodes.begin(); iter != nodes.end(); ++iter)
{
int value = *iter;
// print value as needed...
}
}
void AnyList::destroyList()
{
nodes.clear();
}
void AnyList::getNumOfItems() const
{
return nodes.size();
}
void AnyList::insertBack(int b)
{
nodes.push_back(b);
}
void AnyList::deleteFirstNode()
{
if (!nodes.empty())
nodes.pop_front();
}
That being said, your manual implementation fails because you are likely not managing the nodes correctly (but you did not show everything you are doing). It should look something like this:
class Node
{
public:
Node() : data(0), ptrToNext(NULL) {}
Node(int theData, Node *newPtrToNext) : data(theData), ptrToNext(newPtrToNext) {}
~Node() {}
Node* getPtrToNext() const { return ptrToNext; }
int getData() const { return data; }
void setData(int theData) { data = theData; }
void setPtrToNext(Node *newPtrToNext) { ptrToNext = newPtrToNext; }
private:
int data;
Node *ptrToNext; //pointer that points to next node
};
class AnyList
{
public:
AnyList();
//default constructor
~AnyList();
//destructor
void print() const;
//Prints all values in the list.
void destroyList();
//Destroys all nodes in the list.
int getNumOfItems() const;
void insertBack(int b);
void deleteFirstNode();
private:
Node *ptrToFirst; //pointer to point to the first node in the list
Node *ptrToLast; //pointer to point to the last node in the list
int count; //keeps track of number of nodes in the list
};
AnyList:AnyList()
: ptrToFirst(NULL), ptrToLast(NULL), count(0)
{
}
void AnyList::print() const
{
for (Node *temp = ptrToFirst; temp != NULL; temp = temp->getPtrToNext())
{
int value = temp->getData();
// print value as needed...
}
}
AnyList::~AnyList()
{
destroyList();
}
void AnyList::destroyList()
{
Node *temp = ptrToFirst;
ptrToFirst = ptrToLast = NULL;
count = 0;
while (temp != NULL)
{
Node *next = temp->getPtrToNext();
delete temp;
temp = next;
}
}
int AnyList::getNumOfItems() const
{
return count;
}
void AnyList::insertBack(int b)
{
Node *temp = new Node(b, NULL);
if (ptrToFirst == NULL)
ptrToFirst = temp;
if (ptrToLast != NULL)
ptrToLast->setPtrToNext(temp);
ptrToLast = temp;
++count;
}
void AnyList::deleteFirstNode()
{
if (ptrToFirst == NULL)
return;
Node *temp = ptrToFirst;
ptrToFirst = temp->getPtrToNext();
if (ptrToLast == temp)
ptrToLast = NULL;
delete temp;
--count;
}
i am deleting a (3 or 4 th) node using double linked list...every time only first element is deleting ?what is the problem with the following code?
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *pre;
struct node *link;
};
//void addafter(struct node*,int,int);
main()
{
struct node *p=NULL;
add(&p,2);
add(&p,3);
add(&p,4);
add(&p,5);
add(&p,6);
add(&p,27);
add(&p,8);
add(&p,9);
add(&p,10);
//addafter(&p,7,20);
delete(&p ,6);
display(&p);
}
add(struct node **q,int n)
{
struct node *temp,*r;
temp=*q;
if(temp==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->pre=NULL;
temp->data=n;
temp->link=NULL;
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *pre;
struct node *link;
};
//void addafter(struct node*,int,int);
main()
{
struct node *p=NULL;
add(&p,2);
add(&p,3);
add(&p,4);
add(&p,5);
add(&p,6);
add(&p,27);
add(&p,8);
add(&p,9);
add(&p,10);
//addafter(&p,7,20);
delete(&p ,6);
display(&p);
}
add(struct node **q,int n)
{
struct node *temp,*r;
temp=*q;
if(temp==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->pre=NULL;
temp->data=n;
temp->link=NULL;
*q=temp;
}
else
{
while(temp->link!=NULL)
temp=temp->link;
r=(struct node*)malloc(sizeof(struct node));
r->pre=temp;
r->data=n;
r->link=NULL;
temp->link=r;
}
}
/*
addafter(struct node **q,int m,int n)
{
struct node *temp,*r,*s;
int i;
temp=*q;
for(i=1;i<m;i++)
temp=temp->link;
s=temp->link;
r=(struct node*)malloc(sizeof(struct node));
r->data=n;
r->link=temp->link;
r->pre=temp;
temp->link=r;
s->pre=r;
}
*/
/*display(struct node **q)
{
struct node *temp;
temp=*q;
while(temp!=NULL)
{
printf("\n %d\n",temp->data);
temp=temp->link;
}
}
*/
delete(struct node **q,int n)
{
struct node *temp,*old,*s;
temp=*q;
while(temp!=NULL)
{
if(temp->data=n)
{
if(temp == *q)
{
*q=temp->link;
free(temp);
return;
}
else
{
// s=temp->link;
old->link=temp->link;
// s->pre=old;
free(temp);
return;
}
}
else
{
old=temp;
temp=temp->link;
}
}
printf("data not found");
}
display(struct node **q)
{
struct node *temp;
temp=*q;
while(temp!=NULL)
{
printf("\n %d\n",temp->data);
temp=temp->link;
}
}
At first glance I see this mistake:
old->link=temp->link;
temp->link->pre=temp->pre; // or old.
free(temp);
You should set the pointers for next and previous nodes too. You can make this change in your add method which is also wrong.