control insertion to BST according to user inputs - c++

This is my code for binary search tree:
#include<iostream>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
node* createNode(int value)
{
node* newNode = new node;
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
node* insert( node* root, int data)
{
if (root == NULL) return createNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
return root;
}
void inorder(node* root)
{
inorder(root->left);
cout<< root->data<<" ";
inorder(root->right);
}
int main()
{
node *root = NULL;
int n;
cout << "How many values do you want to enter" << endl;
cin >> n;
int no;
for (int i = 0; i < n; i++)
{
cin >> no;
insert(root, no);
}
inorder(root);
}
When I call display function/inorder in int main() it displays no values and the program stops with error
I am using loop to take input so it can take values upto to user specified value/range
but the display/inorder function is not working.
How can I resolve this issue?

In inOrder function you don't have stop condition!
and in the main should be root=insert(root, no);
#include<iostream>
using namespace std;
typedef struct node
{
int data;
struct node* left;
struct node* right;
}node;
node* createNode(int value)
{
node* newNode = new node;
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
node* insert( node* root, int data)
{
if (root == NULL) return createNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
return root;
}
void inorder(node* root)
{
if(root == NULL)return;
inorder(root->left);
cout<< root->data<<" ";
inorder(root->right);
}
int main()
{
node *root = NULL;
int n;
cout << "How many values do you want to enter" << endl;
cin >> n;
int no;
for (int i = 0; i < n; i++)
{
cin >> no;
root=insert(root, no);
}
inorder(root);
}

Related

can u use head->data as an integer variable, if the "data" is integer type in chained lists?

can u use head->data as an integer variable, if the "data" is integer type in chained lists.
for example, I have a code where I have to transform a simply chained list of integer variables, into 2 lists, one with positive numbers, the other one with negative numbers. I cant find the error in my code...
using namespace std;
struct Node{
int data;
Node* next;
};
Node* head=new Node;
Node* pozitiv=NULL;
Node* negativ=NULL;
Node* newNode(int data)
{
Node* nodNou = new Node();
nodNou -> data = data;
nodNou -> next = NULL;
return nodNou;
}
void Push(Node* &top, int data)
{
Node* nodNou = newNode(data);
nodNou -> next = top;
top = nodNou;
}
void read(){
Node* aux=new Node;
int r,i,v[1000];
cout<<"insert the number of elements in list"<<endl;
cin>>r;
cin>>v[0];
head->data=v[0];
head->next=NULL;
for(i=1;i<r;i++){
cin>>v[i];
aux->data=v[i];
aux->next=head;
head=aux;
aux=new Node;
}
}
write(){
if(head==NULL) return 0;
else{
while(head!=NULL){
cout<<head->data<<"->";
head=head->next;
}
}
cout<<endl<<endl;
}
void stivezlutat(){
Node* aux;
pozitiv=new Node;
pozitiv->next=NULL;
negativ=new Node;
negativ->next=NULL;
while(head!=NULL){
if(head->data >= 0){
Push(pozitiv, head->data);
}
else{
Push(negativ, head->data);
}
head=head->next;
}
cout<<"pozitive list:"<<endl;
while(pozitiv != NULL){
cout<<pozitiv->data<<"->";
pozitiv=pozitiv->next;
}
cout<<endl;
cout<<"negative list:"<<endl;
while(negativ != NULL){
cout<<negativ->data<<"->";
negativ=negativ->next;
}
}
int main()
{
read();
write();
stivezlutat();
}```
Errors in your code:
Required #include directives don't present.
extra "```" exists at the end of code.
No return type is specified for write.
In the function write, return with value is used while no return statement presents at the end of the function, so no return type is valid for that.
The function write breaks the value of head, so the function stivezlutat will see empty list.
Extra nodes are assigned to pozitiv and negativ and they are printed in the funciton stivezlutat.
Fixed code (also indentation is fixed):
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
Node* head=new Node;
Node* pozitiv=NULL;
Node* negativ=NULL;
Node* newNode(int data)
{
Node* nodNou = new Node();
nodNou -> data = data;
nodNou -> next = NULL;
return nodNou;
}
void Push(Node* &top, int data)
{
Node* nodNou = newNode(data);
nodNou -> next = top;
top = nodNou;
}
void read(){
Node* aux=new Node;
int r,i,v[1000];
cout<<"insert the number of elements in list"<<endl;
cin>>r;
cin>>v[0];
head->data=v[0];
head->next=NULL;
for(i=1;i<r;i++){
cin>>v[i];
aux->data=v[i];
aux->next=head;
head=aux;
aux=new Node;
}
}
void write(){
if(head==NULL) return;
else{
Node* cursor = head;
while(cursor!=NULL){
cout<<cursor->data<<"->";
cursor=cursor->next;
}
}
cout<<endl<<endl;
}
void stivezlutat(){
Node* aux;
pozitiv=NULL;
negativ=NULL;
while(head!=NULL){
if(head->data >= 0){
Push(pozitiv, head->data);
}
else{
Push(negativ, head->data);
}
head=head->next;
}
cout<<"pozitive list:"<<endl;
while(pozitiv != NULL){
cout<<pozitiv->data<<"->";
pozitiv=pozitiv->next;
}
cout<<endl;
cout<<"negative list:"<<endl;
while(negativ != NULL){
cout<<negativ->data<<"->";
negativ=negativ->next;
}
}
int main()
{
read();
write();
stivezlutat();
}

C++ program implementing tree stops working when a node is inserted and I can't understand why

here is my code. after reading T the program reads the first input data and stops working. message "Unhandled exception at 0x00844D30 in tree.exe: 0xC0000005: Access violation reading location 0x00000000." is shown. I included iostream and stddef.h libraries.
class Node{
public:
int data;
Node *left, *right;
Node(int d){
data = d;
left = right = NULL;
}
};
class Solution{
public:
Node* insert(Node* root, int data){
if (root = NULL){
return new Node(data);
}
else{
Node* cur;
if (data <= root->data){
cur = insert(root->left, data);
root->left = cur;
}
else{
cur = insert(root->right, data);
root->right = cur;
}
return root;
}
}
};
int main(){
Solution myTree;
Node* root = NULL;
int T, data;
cin >> T;
while (T-->0){
cin >> data;
root = myTree.insert(root, data);
}
return 0;
}
This if will always go into the else part. Because you assign NULL to the variable root and then check the value. It should be root == NULL
if (root = NULL){
return new Node(data);
}
else{
Node* cur;
if (data <= root->data){
cur = insert(root->left, data);
root->left = cur;
}
else{
cur = insert(root->right, data);
root->right = cur;
}
return root;
}
The condition should be
if (root==NULL)
#include <iostream>
#include <stddef.h>
using namespace std;
class Node{
public:
int data;
Node *left, *right;
Node(int d){
data = d;
left = right = NULL;
}
};
class Solution{
public:
Node* insert(Node* root, int data){
if(root == NULL){
return new Node(data);
}
else{
Node* cur;
if (data <= root->data){
cur = insert(root->left, data);
root->left = cur;
}
else{
cur = insert(root->right, data);
root->right = cur;
}
cur->left=cur->right=NULL;
return root;
}
}
};
int main(){
Solution myTree;
Node* root = NULL;
int T, data;
cin>>T;
while (T-->0){
cin>>data;
root = myTree.insert(root, data);
}
return 0;
}

Delete node from Linked List C++

I am working on iterative delete function that deletes node from a linked list, I think that the code is supposed to work fine. But when I can't use "delete" to delete the first Node of the List.
The code is:
#include
using namespace std;
struct Node
{
int data;
Node* next;
};
Node* GetNewNode(int data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
Node* Insert(Node *root, int data)
{
if (root == NULL)
{
root = GetNewNode(data);
}
else
root->next = Insert(root->next, data);
return root;
}
void Delete_k(Node *root, int k)
{
int i = 0;
Node* P = new Node;
if (k == 1)
{
P = root;
root = root->next;
delete P;
}
else
{
for (int i = 1; i <= k - 2; i++)
{
root = root->next;
}
root->next = root->next->next;
}
}
void Output(Node* root)
{
if (root == NULL)
{
root = root->next;
}
while (root != NULL)
{
cout << root->data << " ";
root = root->next;
}
}
int main()
{
int n, a, pos;
Node* root = NULL;
cout << "Input your list hear: ";
cin >> n;
while (n > 0)
{
root = Insert(root, n);
cin >> n;
}
Output(root);
cout << "\nDelete Pos?: ";
cin >> pos;
Delete_k(root, pos);
Output(root);
}
I have problem in this
void Delete_k(Node *root, int k)
{
int i = 0;
Node* P = new Node;
if (k == 1)
{
P = root;
root = root->next;
delete P;
}
else
{
for (int i = 1; i <= k - 2; i++)
{
root = root->next;
}
root->next = root->next->next;
}
}
The problem:
void Delete_k(Node *root, int k)
The value at root is pass by reference, but the pointer to it is not.
Result: Delete_k's root is a copy of main's root. Delete_K's root gets repointed and deleted. Main's root now points at garbage memory. End game program.
Solution:
Provide a reference to the root pointer so that it doesn't get copied.
void Delete_k(Node *& root, int k)
Or return root from Delete_k.
Node * Delete_k(Node * root, int k)
{
//existing code
return root;
}

I'm having trouble with cpp code to insert elements in a binary tree

I've tried this way to insert elements in a binary tree in code blocks. The program got compiled but got a run time error and the program stopped running. Could some one please help me out in this issue.
Thanks in advance.
#include <iostream>
#include <cstdlib>
using namespace std;
class bst;
class node
{
public:
int data;
node *lc;
node *rc;
};
class bst
{
public:
node *root;
bst()
{
root = NULL;
}
void search(int, node **, node **);
void insert(int);
void display(node *, int);
};
void bst::search(int item, node **par, node **loc)
{
node *current;
node *ptr;
if(root == NULL)
{
*par = NULL;
*loc = NULL;
return;
}
if(item == root->data)
{
*par = NULL;
*loc = root;
return;
}
if(item < root->data)
current = root->lc;
else
current = root->rc;
ptr = root;
while(current != NULL)
{
if(item == current->data)
{
*par = ptr;
*loc = current;
return;
}
ptr = current;
if(item < current->data)
current = current->lc;
else
current = current->rc;
}
*par = current;
*loc = NULL;
}
void bst::insert(int item)
{
node *parent;
node *location;
node *temp;
search(item, &parent, &location);
temp = new node;
temp->data = item;
temp->lc = NULL;
temp->rc = NULL;
if(item < parent->data)
parent->lc = temp;
else
parent->rc = temp;
}
void bst::display(node *ptr, int level)
{
if(ptr != NULL)
{
display(ptr->rc, level+1);
cout<<"\n";
for(int i=0;i<level;i++)
cout<<" ";
cout<<ptr->data;
display(ptr->lc, level+1);
}
}
int main()
{
int ch, num;
bst b;
while(1)
{
cout<<"1. INSERT ; 2. DISPLAY ; 3. EXIT "<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the number to insert"<<endl;
cin>>num;
b.insert(num);
break;
case 2: b.display(b.root, 1);
break;
case 3: exit(0);
}
}
return 0;
}
In this line search(item, &parent, &location); you are calling the function search with arguments of types int, node** and node**. It in fact expects int, node* and node*. You should change the function declaration to search(int, node**, node**).

why is the root value not being updated

This is the code to find the ceil and floor in BST. When I am trying to insert the data. every time the insert call goes to the first if condition. i.e. though I pass the pointer. The value is not being updated in the main function. can some one tell me why is it so?
using namespace std;
struct Node
{
int key;
struct Node* right;
struct Node* left;
};
struct Node* newNode(int key)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->right = NULL;
newNode->left = NULL;
newNode->key = key;
return newNode;
}
void insert(struct Node** root,int key) {
if((*root) == NULL){
(*root)= newNode(key);
cout<<"entered first if condition"<<endl;
}
else if( (*root)->key <= key)
insert(&((*root)->left),key);
else
insert (&((*root)->right),key);
}
int ceil( struct Node* root , int input)
{
if (root == NULL)
return -1;
if(root->key == input)
return root->key;
if(root->key < input)
return ceil( root->right , input);
else{
int ceilnum = ceil(root->left, input);
return (ceilnum >= input) ? ceilnum : root->key;
}
}
int main()
{
int size, temp, ceilfor;
struct Node* root = NULL;
cout<< "size" << endl;
cin >> size;
for( int i = 0; i< size; i++)
{
cin >> temp;
insert(&root,temp);
}
cout<< root->key;
cout<< root->left->key;
cout << root->right->key;
cout << "ceil for" << endl;
cin >> ceilfor;
cout<< ceil(root, ceilfor) <<endl;
}
It has to come to the first condition (either directly or indirectly through recursive calls).
The actual insertion happens only in the first if block and other blocks will recursively reach the first if block.