Inorder successor of Threaded Binary Tree - c

I am writing a c program to create threaded binary tree and then to find INORDER SUCCESSOR of a particular node. For this, i am displaying inorder sequence for the TBT constructed and then asking user to input the node to which successor is to be displayed.. I have written function to do this. But i am not getting successor for the FIRST NODE .. Last node's successor is 0 any ways its working fine.. Can any one help me fix this ?
Here is the whole program :
#include<stdio.h>
#include <stdlib.h>
struct tbtnode {
int data;
struct tbtnode *left,*right;
int lbit,rbit,flag;
int child;
}*root=NULL;
typedef struct tbtnode TBT;
TBT *insuc(TBT *t);
void inorder(TBT *);
void create(TBT *);
void create(TBT *root)
{
int x,op,flag,y;
flag=0;
char ch;
TBT *curr=root;
TBT *q,*p;
do
{
printf("\nCurrent node %d \n\n 1.Left Direction.\n\n2.Right Direction",curr->data);
printf("\nEnter ur choice :");
scanf("%d",&op);
switch(op)
{
case 1: if(curr->lbit==1)
{
printf("Enter left child of %d : ",curr->data);
scanf("%d",&x);
q=(TBT *)malloc(sizeof(TBT));
q->data=x;
q->lbit=q->rbit=1;
q->left=curr->left;
q->right=curr;
curr->left=q;
curr->lbit=0;
q->child=0;
flag=1;
}
else
curr=curr->left;
break;
case 2: if(curr->rbit==1)
{
printf("Enter right child of %d :",curr->data);
scanf("%d",&x);
q=(TBT *)malloc(sizeof(TBT));
q->data=x;
q->lbit=q->rbit=1;
q->left=curr;
q->right=curr->right;
curr->right=q;
curr->rbit=0;
q->child=1;
flag=1;
}
else
curr=curr->right;
break;
}
}while(flag==0);
}
void inorder(TBT *head)
{
TBT *t;
t=head->left;
printf("\n");
while(t->lbit==0)
t=t->left;
while(t!=head)
{
printf(" %d",t->data);
t=insuc(t);
}
}
TBT *insuc(TBT *t)
{
if(t->rbit==0)
{
t=t->right;
while(t->lbit==0)
t=t->left;
return(t);
}
else
return(t->right);
}
void inorder_successor(TBT *head,int x)
{
TBT *t;
t=head->left;
printf("\n");
while(t->lbit==0)
t=t->left;
while(t!=head)
{
t=insuc(t);
if(t->data==x)
{
t=insuc(t);
printf(" %d",t->data);
}
}
}
int main()
{
int op,x,n,i=0,item;
char ch;
TBT *head,*root,*succ; //here head indicates dummy variable
head=(TBT *)malloc(sizeof(TBT));
head->left=head;
head->right=head;
head->lbit=1;
head->rbit=1;
do
{
printf("\n****Threaded binary tree operations****");
printf("\n1)create\n2)inorder\n3)Successor\n4)exit");
printf("\nEnter ur choice: ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\nEnter Number Of Nodes :");
scanf("%d",&n);
printf("\nEnter root data: ");
scanf("%d",&x);
root=(TBT *)malloc(sizeof(TBT));
root->data=x;
root->lbit=root->rbit=1;
root->child=0;
root->left=head->left;
head->left=root;
head->lbit=0;
root->right=head->right;
for(i=0;i<n-1;i++)
create(root);
break;
case 2:
printf("\nInorder Traversal Is:\n");
inorder(head);
break;
case 3: printf("Enter the node to which successor is to be found\n");
scanf("%d",&item);
inorder_successor(head,item);
break;
case 4:
return(0);
break;
}
}while(op<=4);
return 0;
}
please fix - inorder_successor() function for me..
Thank you

You have many problems with your code. To start with you never check for NULL pointers. To continue, you have both a global and a local variable in main called root.
The last thing means that when you allocate memory for root in main, you allocate for the local variable, and the global variable will still be NULL.
There are also other things that look weird, like you assigning the left and right pointer of head to itself.
I think you need to lay it all out on paper first, both how you have it now and how you want it to be. It will help you to better visualize the tree.

Related

Node value changing automatically in C

I'm creating a program for binary search trees and this is a function that I'm using. As seen in the output, the value of
root->rightChild is changing for unknown reasons and the program end without showing any errors.
typedef struct node * BST;
struct node
{
struct node *leftChild;
int data;
struct node *rightChild;
};
BST temp=NULL, ptr=NULL, root=NULL, prev=NULL;
BST newNode()
{
BST X;
X=(BST)malloc(sizeof(BST));
X->leftChild=X->rightChild=NULL;
return X;
}
void createBST(int elem)
{
temp=newNode();
temp->data=elem;
if(!root)
{
root=temp;
printf("\nroot-?rightChild= %p",root->rightChild);
printf("\nroot-?leftChild= %p",root->leftChild);
}
else
{
printf("\nroot-?rightChild= %p",root->rightChild);
printf("\nroot-?leftChild= %p",root->leftChild);
prev=NULL;
ptr=root;
while(ptr!=NULL)
{
prev=ptr;
ptr=(ptr->data<temp->data)?ptr->rightChild:ptr->leftChild;
}
if(prev->data<temp->data)
prev->rightChild=temp;
else
prev->leftChild=temp;
}
}
int main()
{
int choice;
while(1)
{
printf("\nPick a Binary Search Tree operation\n1) Create a Binary Search Tree\n2) Traverse the Binary Search Tree\n3) Seach for a KEY element in the Binary Search Tree\n4) Exit\n>| ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
int num,elem;
printf("\nEnter the number of elements to be inserted into the Binary Search Tree: ");
scanf("%d",&num);
printf("\nEnter the elements to be inserted into the Binary Search Tree: ");
for(int i=1;i<=num;i++)
{
scanf("%d",&elem);
createBST(elem);
}
}
break;
case 2:
traverBST();
break;
case 3:
searchBST();
break;
case 4:
exitProgram();
break;
default:
printf("\nInvalid Choice\n");
}
}
return 0;
}
This is the output of the code:
This statement has a typo
X=(BST)malloc(sizeof(BST));
^^^
That is the call of malloc allocates a memory for a pointer to node instead of for a node itself.
There must be
X=(BST)malloc(sizeof( *X));
or
X=(BST)malloc(sizeof( struct node ));
As a result of the typo the program has undefined behavior.

root node automatically resets to null while insertion

I'm trying to add new nodes to the tree but whenever one insertion is completed, root node automatically resets to null and at the end my tree is empty. I'm trying to construct BST.
My main function:
int main()
{
char c;
int item;
struct BSTNode *root=NULL;
while(1)
{
printf("\n1 Insert an element ");
printf("\n2 Delete an element");
printf("\n3 InOrder Traversal");
printf("\nEnter your choice: ");
scanf("%d", &c);
switch(c)
{
case 1:
printf("\nEnter the item:");
scanf("%d", &item);
if(root){printf("Root data before: %d",root->data); } //Print statement -1
root = insert(root,item);
printf("Root data after: %d",root->data); //Print statement-2
break;
case 2:
printf("\nEnter the info to be deleted:");
scanf("%d", &item);
root = delete(root, item);
break;
case 3:
InOrder(root);
break;
default:
printf("Enter a valid choice: ");
}
}
return 0;
}
and my insert function looks like:
struct BSTNode* insert(struct BSTNode *root, int data)
{
if(root==NULL)
{
root=create(data);
}
if(data<root->data)
root->left=insert(root->left,data);
if(data>root->data)
root->right=insert(root->right,data);
return root;
}
Here in my main function, two print statements are there. In that, statement 2 is printing the data of root node but when again I want to add new node, statement 1 doesn't print the data of root node. And after that new value of root node is shown in statement 2 also.
Why its char c? when your cases are integer, try using int c instead. Also you are taking input using %d so it should be integer

not woking c program for SLL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Q please help me this program is not working properly.
not displaying the value.this program is an example of singly linked list which I am trying to run on c.
`
#include<stdio.h>
#include<stdlib.h> //malloc defined
struct node
{
int data;
struct node *next;
};
add() //add function
{
int value;
struct node *n;
n=(struct node*)malloc(sizeof(struct node)); //mem allocation
printf("enter the value to add\n");
scanf("%d",&value);
n->data=value;
n->next=NULL;
// n=n->next;
// n->next=NULL;
}
delete() //delete function
{
// n=n->next;
struct node *n; //declaration
printf("the node deleted is %d",n->data);
free(n);
}
display() //display function
{
struct node *n;
while(n!=NULL)
{
printf("%d",n->data);
n=n->next;
}
}
int main()
{
int ch;
while(1)
{
printf("do you want to add node press 1\n");
printf("do you want to delete node press 2\n");
printf("do you want to display node press 3\n");
printf("do you want to exit press 4\n");
scanf("%d",&ch);
switch(ch)
{
case 1:add();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
default: printf("wrong choice!!!\n");
}
}
return 0;
getch();
}
please help me this program is not working properly.
not displaying the value.this program is an example of singly linked list which I am trying to run on c.
printf("%d",n->data); is not printed because:
struct node *n; // n is not determined.
while(n != NULL) // undefined behaviour
n is different from the other n that is in the add() function. You never passed the struct to the other functions so it will not do what you wanted it to do.
in the function display(), the local variable 'n' is only declared but not defined, so here 'n' is only a wild pointer. It's happened in the function delete() as well.
it's not a correct way to implement the single linked list. In the function add() what you wrote is only create a node, but not add a node to linked list.
#include<stdio.h>
#include<stdlib.h> //malloc defined
struct node
{
int data;
struct node *next;
}*n,*p;
create() //add function
{
int value;
n=(struct node*)malloc(sizeof(struct node)); //mem allocation
printf("enter the value to add\n");
scanf("%d",&value);
n->data=value;
n->next=NULL;
}
add()
{
int value;
// struct node *p;
p=(struct node*)malloc(sizeof(struct node));
printf("enter the value to add next\n");
scanf("%d",&value);
n->next=p;
p->data=value;
p->next=NULL;
}
delete() //delete function
{
printf("the node deleted is %d",p->data);
n->next=NULL;
free(p);
}
display() //display function
{
while(n!=NULL)
{
printf("%d\n",n->data);
n=n->next;
}
}
int main()
{
int ch;
while(1)
{
printf("do you want to create node press 1\n");
printf("do you want to add node press 2\n");
printf("do you want to delete node press 3\n");
printf("do you want to display node press 4\n");
printf("do you want to exit press 5\n");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:add();
break;
case 3:delete();
break;
case 4:display();
break;
case 5:exit(0);
default: printf("wrong choice!!!\n");
}
}
return 0;
getch();
}

C Program to copy one binary search tree to another

So, here i have come up with Binary search tree prgram, where i am creating 2 binary trees tmp and tmp2, where i am trying to copy whole tmp2 to tmp, the node which is taken as input from user. But i am getting some segmentation fault, also i am not so sure if the logic is right.
Here is the whole program, please lemme know where is it going wrong in t_cpy() or please just fix it for me..
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *rlink;
struct node *llink;
}*tmp=NULL,*tmp2=NULL,*tmp3=NULL;
typedef struct node NODE;
NODE *create();
void inorder(NODE *);
void insert(NODE *);
void t_cpy(NODE *,NODE *);
int main()
{
int n,m;
do
{
printf("\n1.create tree 1\n2.Insert element to tree1\n3.create tree 2\n4.Insert element to tree2\n5.Inorder tree1\n6.Inorder tree2\n7.Copy tree2 to tree1\n8.exit\n\n");
printf("\nEnter ur choice: ");
scanf("%d",&m);
switch(m)
{
case 1: tmp=create();
break;
case 2: insert(tmp);
break;
case 3: tmp2=create();
break;
case 4:
insert(tmp2);
break;
case 5: printf("\n\nInorder Tree1: ");
inorder(tmp);
break;
case 6: printf("\n\nInorder Tree 2: ");
inorder(tmp2);
break;
case 7: t_cpy(tmp,tmp2);
break;
case 8: return(0);
}
}while(n!=8);
return(0);
}
void insert(NODE *root)
{
NODE *newnode;
if(root==NULL)
{
newnode=create();
root=newnode;
}
else
{
newnode=create();
while(1)
{
if(newnode->data<root->data)
{
if(root->llink==NULL)
{
root->llink=newnode;
break;
}
root=root->llink;
}
if(newnode->data>root->data)
{
if(root->rlink==NULL)
{
root->rlink=newnode;
break;
}
root=root->rlink;
}
}
}
}
NODE *create()
{
NODE *newnode;
int n;
newnode=(NODE *)malloc(sizeof(NODE));
printf("\n\nEnter the Data ");
scanf("%d",&n);
newnode->data=n;
newnode->llink=NULL;
newnode->rlink=NULL;
return(newnode);
}
void t_cpy(NODE *t1,NODE *t2)
{
int val,opt=0;
NODE *temp;
if(t1==NULL || t2==NULL)
{
printf("Can not copy !\n");
}
inorder(t1);
printf("\nEnter the node value where tree 2 should be copied\n");
scanf("%d",&val);
temp=t1;
while(temp!=NULL)
{
if(val<temp->data)
temp=temp->llink;
else
temp=temp->rlink;
}
if(temp->llink!=NULL || temp->rlink!=NULL)
printf("Not possible to copy tree to this node\n");
else
{
printf("Copy tree to \n 1.Left Node \n 2.Right Node\n Enter your choice : ");
scanf("%d",&opt);
if(opt==1)
{
temp->llink=t2;
}
else if(opt==2)
{
temp->rlink=t2;
}
else
printf("Invalid choice\n");
}
printf("Tree1 after copying is\n");
inorder(temp);
}
void inorder(NODE *tmp)
{
if(tmp!=NULL)
{
inorder(tmp->llink);
printf("%d",tmp->data);
inorder(tmp->rlink);
}
}
EDIT : Thanks to #xaxxon , who helped me with this.
Just update the while to make it work :
while(temp!=NULL&&temp->data!=val)
{
if(val<temp->data)
temp=temp->llink;
else
temp=temp->rlink;
if(temp->llink==NULL && temp->rlink==NULL && temp->data!=val)
{
printf("Invalid Node value entered !\n");
//break;
return 0;
}
and, Now it works fine for if entered value is present in the tree.
Thanks :)
Among other possible problems, you traverse temp until it is null, and on the next line you dereference it.
while(temp!=NULL)
{
if(val<temp->data)
temp=temp->llink;
else
temp=temp->rlink;
}
if(temp->llink!=NULL || temp->rlink!=NULL)
printf("Not possible to copy tree to this node\n");
You most likely mean to break out of this loop if val == temp->data, but you don't. Also, you still need to check to see if temp is null after the loop in case you didn't find val in your tree. Most likely you just meant to say:
if(temp==NULL)
printf("Not possible to copy tree to this node\n");
Also, you can't ask which side of the found node the user wants to copy a tree to. If you have a binary search tree, it has to be the side where the value should go. If you say to copy it to the right side, but all the values are less than the node, it's no longer a BST. In fact, you can't even ask where the value should go and still have a binary search tree. Each node has to be traversed from the root of the tree you want to put the other tree into to maintain the BST mechanics.
When you first use insert(tmp) the value of tmp does not change after you call insert(). Pass the address of tmp to insert(), using a *root within it instead of root.

Why my doubly linked list's C implementation creating duplicate values?

I coded for doubly linked list implementation in C. In that, after making insertion of values, i am getting duplication of values. i.e. the last value given by me duplicated in all list items.
My code is as follows
header.h
#include<stdio.h>
#include<stdlib.h>
typedef struct doubly_list
{
int id;
char *name;
struct doubly_list *next;
struct doubly_list *prev;
}node;
void insertfirst(node **,int ,char *);
void insertlast(node **,int ,char *);
doubly_list_insert.c
#include"header.h"
void insertfirst(node **head,int id,char *name)
{
node *tmp=(node *)malloc(sizeof(node));
if(NULL == tmp)
{
printf("\nMemory allocation failed\n");
exit(1);
}
tmp->id=id;
tmp->name=name;
tmp->prev=NULL;
if(*head== NULL)
{
tmp->next=NULL;
*head=tmp;
}
else
{
tmp->next=*head;
(*head)->prev=tmp;
*head=tmp;
}
}
void insertlast(node **head,int id,char *name)
{
if(*head==NULL)
{
insertfirst(head,id,name);
return;
}
node *last=*head;
node *tmp=(node *)malloc(sizeof(node));
if(NULL == tmp)
{
printf("\nMemory allocation failed\n");
exit(1);
}
tmp->id=id;
tmp->name=name;
tmp->next=NULL;
while(last->next!=NULL)
{
last=last->next;
}
last->next=tmp;
tmp->prev=last;
}
doubly_list_traverse.c
#include"header.h"
void traverse(node *head)
{
node *tmp=head;
if(head==NULL)
{
printf("\nList is empty\n");
exit(1);
}
while(tmp!=NULL)
{
printf("%d --> %s\n",tmp->id,tmp->name);
tmp=tmp->next;
}
}
And, here comes the main file,
main.c
#include"header.h"
int main()
{
int choice;
int id;
char name[15];
node *root=NULL;
system("clear");
while(1)
{
printf("\n1.Insert First\n");
printf("\n2.Insert Last\n");
printf("\n3.Traverse\n");
printf("\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the employee id : ");
scanf("%d",&id);
printf("\nEnter the employee name : ");
scanf("%s",name);
insertfirst(&root,id,name);
break;
case 2:
printf("\nEnter the employee id : ");
scanf("%d",&id);
printf("\nEnter the employee name : ");
scanf("%s",name);
insertlast(&root,id,name);
break;
case 3:
traverse(root);
break;
case 4:
return 0;
break;
default:
printf("\nPlease enter valid choices\n");
}
}
}
During execution its getting input from me properly,if i insert only one data either first or last.
But if i insert a second one, there comes the problem.
In my case, the id value remains the same. But the 2nd input's name value is duplicated in 1st value.
Why this is happening? Is it anything wrong in passing arguments?
When you create a new node, you set the node name by just copying the pointer to the name. You have to copy the string not the pointer. The strdup function is perfect for this:
tmp->name=strdup(name);
Remember to free the name when you free the nodes.
Edit
What happens when you call insertfirst the first time, is that the name field of the first node points to the name array in main. When you fetch the name for the second node, the contents of the array in main is updated with the new name, and since the pointer in the first node points to that array it seems like the name is duplicated.

Resources