Why is the key of the left node missing after insertion? - c

I could not use the Insert_node to insert a node with corresponding key, and I couldn't figure out the problem.
Keys is an array of integers I created earlier.
Here is the code:
Node* Create_node(int x)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->key = x;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node* Insert_node(Node** proot, int x)
{
Node* root = *proot;
if(root == NULL)
{
root = Create_node(x);
}
return root;
}
int main()
{
Node* root = Create_node(keys[0]);
Insert_node(&root->left,keys[1]);
printTree(root);
return 0;
}

Related

Why am I not getting the last print statement, unable to get height of the tree?

After taking values for nodes, program terminates. I don't know what I am doing wrong. I don't even know if the values are getting inserted.
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
struct Node* GetNode(int data){
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data=data;
newnode->left = NULL;
newnode->right = NULL;
}
struct Node* insert(struct Node* root,int data){
if(root=NULL){
root = GetNode(data);
return root;
}
else if(data <= root->data){
root->left = insert(root->left,data);
}
else{
root->right = insert(root->right,data);
}
return root;
}
int height(struct Node* root){
if(root==NULL){
return 0;
}
else{
int lh = height(root->left);
int rh = height(root->right);
if(lh>rh){
return (lh+1);
}
else{
return (rh+1);
}
}
}
int main() {
struct Node* root =NULL;
int n,i;
printf("Enter the total number of nodes in the tree: ");
scanf("%d",&n);
int value[n];
for(i=0;i<n;i++){
printf("Enter the %d node ",i+1);
scanf("%d",&value[i]);
}
for(i=0;i<n;i++){
root = insert(root,value[i]);
}
int high = height(root);
printf("Height of the given tree is : %d ",high);
return 0;
}
No, idea if it is the height function or the insert function.
You have 2 problems in your code.
In the function struct Node* GetNode(int data), you are not returning the newnode.
In the function struct Node* insert(struct Node* root,int data), in the if condition if(root=NULL), you should use ==
Here is the corrected working code:
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
struct Node* GetNode(int data){
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data=data;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
struct Node* insert(struct Node* root,int data){
if(root==NULL){
root = GetNode(data);
return root;
}
else if(data <= root->data){
root->left = insert(root->left,data);
}
else{
root->right = insert(root->right,data);
}
return root;
}
int height(struct Node* root){
if(root==NULL){
return 0;
}
else{
int lh = height(root->left);
int rh = height(root->right);
if(lh>rh){
return (lh+1);
}
else{
return (rh+1);
}
}
}
int main() {
struct Node* root =NULL;
int n,i;
printf("Enter the total number of nodes in the tree: ");
scanf("%d",&n);
int value[n];
for(i=0;i<n;i++){
printf("Enter the %d node ",i+1);
scanf("%d",&value[i]);
}
for(i=0;i<n;i++){
root = insert(root, value[i]);
}
int high = height(root);
printf("Height of the given tree is : %d ",high);
return 0;
}

BST - Deleting a Node

so I'm trying to solve a problem from log2base2.com involving deleting a node from BST. It works except when trying to delete a node with two children. I know I could simply replace the numbers and then delete the duplicate at the end, but what if my struct has more data than just key? So what am I doing wrong please?
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left;
struct node *right;
};
int getRightMin(struct node *root)
{
//Write your code here
struct node *temp = root;
while(temp->left!=NULL)
{
temp=temp->left;
}
return temp;
}
struct node *removeNode(struct node *root, int key)
{
//Write your code here
if(root == NULL){return NULL;}
if(key<root->key){root->left = removeNode(root->left, key);}
else if(key>root->key){root->right = removeNode(root->right,key);}
else
{
if(root->left==NULL && root->right==NULL)
{
free(root);
return NULL;
}
else if(root->left)
{
struct node *temp = root->left;
free(root);
return temp;
}
else if(root->right)
{
struct node *temp = root->right;
free(root);
return temp;
}
else
{
struct node *temp = getRightMin(root->right);
temp->left = root->left;
temp->right = root->right;
root = temp;
root->right = removeNode(root->right, temp->key);
}
}
return root;
}
//Don't change the below code
struct node *getNewNode(int val)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node *insert(struct node *root, int val)
{
if(root == NULL)
return getNewNode(val);
if(root->key < val)
root->right = insert(root->right,val);
else if(root->key > val)
root->left = insert(root->left,val);
return root;
}
void inorder(struct node *root)
{
if(root == NULL)
return;
inorder(root->left);
printf("%d ",root->key);
inorder(root->right);
}
int main()
{
struct node *root = NULL;
root = insert(root,100);
root = insert(root,50);
root = insert(root,200);
root = insert(root,150);
root = insert(root,300);
int key;
scanf("%d",&key);
root = removeNode(root,key);
inorder(root);
return 0;
}
TEST CASE 1:
INPUT: 200
EXPECTED OUTPUT: 50 100 150 300
ACTUAL OUTPUT: 50 100 150
TEST CASE 2:
INPUT: 100
EXPECTED OUTPUT: 50 150 200 300
ACTUAL OUTPUT: 50
You have to reorder conditional in removeNode() to:
if(root->left==NULL && root->right==NULL)
{
free(root);
return NULL;
}
else if (root->left && root->right)
{
struct node *temp = getRightMin(root->right);
temp->left = root->left;
temp->right = root->right;
root = temp;
root->right = removeNode(root->right, temp->key);
}
else if(root->left)
{
struct node *temp = root->left;
free(root);
return temp;
}
else // root->right != NULL
{
struct node *temp = root->right;
free(root);
return temp;
}
It looks that (root->left && root->right) case was caught by (root->left) and
reference to root->right was lost.

linked list c programming error by inserting new element

I am trying to insert an element but it get the error "Process finished with exit code 11"
struct node {
int key;
struct node *next;
};
struct node* init(){
struct node *head =NULL;
return head;
}
void create(struct node * head,int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while(tmp!= NULL && tmp->key < num){
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp== NULL)
head=tmp;
}
int main() {
int num;
struct node* head;
head=init()
printf("Enter data:");
scanf("%d",&num);
create(head,num);
}
i am trying to insert an element into a linked list and the element should be sorted and entered at the same time.can someone tell me that the error is ? i cannot seem to find out the error.
It's not clear what your function create()
void create(struct node * head, int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while (tmp != NULL && tmp->key < num) {
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp == NULL)
head = tmp;
}
is supposed to do. You effectively pass it a NULL pointer and return void, so everything it does is meaningless to the outside world.
Thetm starting point for every no bs linked list implementation:
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct node_tag {
int value;
struct node_tag *next;
} node_t;
// write functions to encapsulate the data and provide a stable interface:
node_t* node_create_value(int value)
{
node_t *new_node = calloc(1, sizeof *new_node);
if(new_node) new_node->value = value;
return new_node;
}
node_t* node_advance(node_t const *node) { return node->next; }
typedef struct list_tag { // a list usually consists of
node_t *head; // a pointer to the first and
node_t *tail; // a pointer to the last element
// size_t size; // one might want to add that.
} list_t;
list_t list_create(void)
{
list_t list = { NULL, NULL };
return list;
}
// make code based on these functions "speak" for itself:
node_t* list_begin(list_t const *list) { return list->head; }
node_t* list_end (list_t const *list) { return list->tail; }
bool list_is_empty(list_t const *list) { return !list_begin(list); }
// common operations for lists:
node_t* list_push_front(list_t *list, int value)
{
node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;
new_node->next = list->head;
return list->head = new_node;
}
node_t* list_push_back(list_t *list, int value)
{
// push_back on an empty list is push_front:
if (list_is_empty(list))
return list->tail = list_push_front(list, value);
node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;
list->tail->next = new_node;
return list->tail = new_node;
}
node_t* list_insert_after(list_t *list, node_t *node, int value)
{
if (list_end(list) == node)
return list_push_back(list, value);
node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;
new_node->next = node->next;
return node->next = new_node;
}
node_t* list_insert_sorted(list_t *list, int value)
{
// first handle the special cases that don't require iterating the whole list:
if (list_is_empty(list) || value < list_begin(list)->value)
return list_push_front(list, value);
if (value > list_end(list)->value)
return list_push_back(list, value);
// the general (worst) case:
for (node_t *current_node = list_begin(list); node_advance(current_node); current_node = node_advance(current_node))
if (value < node_advance(current_node)->value)
return list_insert_after(list, current_node, value);
return NULL; // should never happen
}
void list_print(list_t const *list)
{
for (node_t *current_node = list_begin(list); current_node; current_node = node_advance(current_node))
printf("%d\n", current_node->value);
}
void list_free(list_t *list)
{
for(node_t *current_node = list_begin(list), *next_node; current_node; current_node = next_node) {
next_node = current_node->next;
free(current_node);
}
}
// user code should not be required to know anything about the inner workings
// of our list:
int main(void)
{
list_t list = list_create();
for (int i = 1; i < 10; i += 2) {
if (!list_push_back(&list, i)) {
list_free(&list);
fputs("Not enough memory :(\n\n", stderr);
return EXIT_FAILURE;
}
}
list_print(&list);
putchar('\n');
for (int i = 0; i < 11; i += 2) {
if (!list_insert_sorted(&list, i)) {
list_free(&list);
fputs("Not enough memory :(\n\n", stderr);
return EXIT_FAILURE;
}
}
list_print(&list);
list_free(&list);
}
Output:
1
3
5
7
9
0
1
2
3
4
5
6
7
8
9
10

Stuck with branches of Tree

i was trying to do tree traversal.(per-order,in-order,and post-order)
here is my code.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* left;
struct node* right;
};
void Insert(struct node* root,int item)
{
struct node* parent;
struct node* NewNode = (struct node*)malloc(sizeof(struct node));
NewNode->left = NULL;
NewNode->data = item;
NewNode->right = NULL;
if (root == NULL)
root = NewNode;
else
{
parent = root;
while (1)
{
if (parent->data>item)
{
if(parent->left == NULL)
{
parent->left = NewNode;
return;
}
parent = parent->left;
}
if (parent->data<item)
{
if(parent->right == NULL)
{
parent->right = NewNode;
return;
}
parent = parent->right;
}
}
}
}
void pre(struct node *newNode)
{
if(newNode!=NULL)
{
printf("%d ",newNode->data);
pre(newNode->left);
pre(newNode->right);
}
}
void in(struct node *newNode)
{
if(newNode!=NULL)
{
in(newNode->left);
printf("%d ",newNode->data);
in(newNode->right);
}
}
void post(struct node *newNode)
{
if(newNode!=NULL)
{
post(newNode->left);
post(newNode->right);
printf("%d ",newNode->data);
}
}
int main(void)
{
int num,i;
printf("\nHow many Numbers you wanna Enter in tree:\t");
scanf("%d",&num);
int numArr[num];
printf("\nEnter the numbers: \n");
struct node* root = NULL;
for (i=0;i<num;i++)
{
scanf("%d",&numArr[i]);
Insert(root,numArr[i]);
}
printf("\nPre order traversal is:\n");
pre(root);
printf("\nIn order traversal is:\n");
in(root);
printf("\nPost order traversal is:\n");
post(root);
}
i think i have problem with inserting the value because when i run the code the output is just empty.
could anyone explain me where m i going wrong?
at insert function m taking the root node and the item to be inserted as argument.
then i'm creating a new node using malloc.
inserting data to the new node and left, right is null as left and right is currently not pointing to any nodes.
then checking if root is null or not. if it is null m assigning the new node to root node.
if root is not null.
(i should not loose the root so i am copying root to parent and using that.)
m checking if the data is less than root or not. if it is less m going to the left of root if left of root is null m inserting the address of new node there if it is not null m just going left and left until it becomes null.
m doing the same thing(just going right) if data is greater than the value in root.
my explanation tells me m going on the right path.
but my code tell me a different story.
this is the correct answer thanks to everyone for help.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* left;
struct node* right;
};
void Insert(struct node** root,int item)
{
struct node* parent;
struct node* NewNode = (struct node*)malloc(sizeof(struct node));
NewNode->left = NULL;
NewNode->data = item;
NewNode->right = NULL;
if (*root == NULL)
*root = NewNode;
else
{
parent = *root;
while (1)
{
if (parent->data>item)
{
if(parent->left == NULL)
{
parent->left = NewNode;
return;
}
parent = parent->left;
}
if (parent->data<item)
{
if(parent->right == NULL)
{
parent->right = NewNode;
return;
}
parent = parent->right;
}
}
}
}
void pre(struct node *newNode)
{
if(newNode!=NULL)
{
printf("%d ",newNode->data);
pre(newNode->left);
pre(newNode->right);
}
}
void in(struct node *newNode)
{
if(newNode!=NULL)
{
in(newNode->left);
printf("%d ",newNode->data);
in(newNode->right);
}
}
void post(struct node *newNode)
{
if(newNode!=NULL)
{
post(newNode->left);
post(newNode->right);
printf("%d ",newNode->data);
}
}
int main(void)
{
int num,i;
printf("\nHow many Numbers you wanna Enter in tree:\t");
scanf("%d",&num);
int numArr[num];
printf("\nEnter the numbers: \n");
struct node* root = NULL;
for (i=0;i<num;i++)
{
scanf("%d",&numArr[i]);
Insert(&root,numArr[i]);
}
printf("\nPre order traversal is:\n");
pre(root);
printf("\nIn order traversal is:\n");
in(root);
printf("\nPost order traversal is:\n");
post(root);
}

Display function in Circular Linked List in C

I am having a problem with my Circular Linked list. I believe the problem is with my display function. Please let me know what is going wrong. The problem I have is that the first n-1 elements are displayed and then I get a segmentation fault(The last element doesn't get displayed and I get a segmentation fault).Thank you :-)
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* link;
};
struct Node* last = NULL;
void Insert_begin(int a)
{
struct Node* temp;
temp = malloc(sizeof(struct Node));
temp->data = a;
if (last == NULL)
last = temp;
else
{
temp->link = last->link;
last->link = temp;
}
}
void Display()
{
struct Node* temp;
if (last == NULL)
{
printf("list is empty");
}
temp = last->link;
while(temp!=last)
{
printf("%d\n",temp->data);
temp = temp->link;
}
printf("%d\n",temp->data);
}
int main()
{
Insert_begin(0);
Insert_begin(1);
Insert_begin(2);
Insert_begin(3);
Insert_begin(4);
Display();
return 0;
}
When you insert the first element into the list, you must its link point to itself:
if (last == NULL) {
last = temp;
last->link = last;
} else ...
In your code, the link from the last element was uninitialised.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* link;
};
struct Node* last = NULL;
void Insert_begin(int a)
{
struct Node* temp;
temp = malloc(sizeof(struct Node));
temp->data = a;
if (last == NULL)
{
last = temp;
temp->link=last;//you forget this
}
else
{
temp->link = last->link;
last->link = temp;
last=temp;
}
}
void Display()
{
struct Node* temp;
if (last == NULL)
{
printf("list is empty");
return;
}
temp = last->link;
while(temp!=last)
{
printf("%d\n",temp->data);
temp = temp->link;
}
printf("%d\n",temp->data);
}
int main()
{
Insert_begin(0);
Insert_begin(1);
Insert_begin(2);
Insert_begin(3);
Insert_begin(4);
Display();
return 0;
}
if (last == NULL)
{
last = temp;
**// adding this line
last->link = last;**
}
That solve the problem
Below is corrected code:
one mistake you have done that is at inserting first value you are not pointing the link to first node itself.In circular singly linked list if there is one node then link(next in general) field should be pointed to that node itself.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* link;
};
struct Node* last = NULL;
void Insert_begin(int a)
{
struct Node* temp;
temp = malloc(sizeof(struct Node));
temp->data = a;
if (last == NULL)
{
last = temp;
/*link field is pointing to that node
*it self(you have forgotten this)*/
last->link = temp;
}
else
{
temp->link = last->link;
last->link = temp;
}
}
void Display()
{
struct Node* temp;
if (last == NULL)
{
printf("list is empty");
}
temp = last->link;
while(temp!=last)
{
printf("%d\n",temp->data);
temp = temp->link;
}
printf("%d\n",temp->data);
}
int main()
{
Insert_begin(0);
Insert_begin(1);
Insert_begin(2);
Insert_begin(3);
Insert_begin(4);
Display();
return 0;
}
*
Problem is with Insert_begin(int a) function,
When you are inserting first node, you are not linking his next to itself, so next time while inserting second/third/.. node, you trying to access first node as last->link but it gives you garbage value and that is reason
void Insert_begin(int a)
{
struct Node* temp;
temp = malloc(sizeof(struct Node));
temp->data = a;
if (last == NULL)
{
last = temp;
last->link = last;
}
else
{
temp->link = last->link;
last->link = temp;
last = temp;
}
}
void Display()
{
struct Node* temp;
if (last == NULL)
{
printf("list is empty");
}
else
{
temp = last->link;
while(temp!=last);
{
printf("%d\n",temp->data);
temp = temp->link;
}
printf("%d\n",temp->data);
}
}
void main()
{
Insert_begin(10);
Insert_begin(20);
Insert_begin(30);
Display();
}

Resources