Hi
I am trying to convert the BST to doubly linked list but I am getting
desired output with this code ....Plz correct this code.....
#include<stdio.h>
#include<stdlib.h>
typedef struct tree mytree;
struct tree
{
int data;
mytree* left;
mytree* right;
};
void insert(mytree** root,int data)
{
if(*root==NULL)
{
mytree* node=(mytree*)malloc(sizeof(mytree));
if(node==NULL)
return;
else
{
node->data=data;
node->left=NULL;
node->right=NULL;
}
*root=node;
return;
}
else
{
if((*root)->data >=data)
insert(&(*root)->left,data);
else
insert(&(*root)->right,data);
}
}
void traverse(mytree* ptr)
{
if(ptr==NULL) return;
traverse(ptr->left);
printf("%d\t",ptr->data);
traverse(ptr->right);
}
void traversell(mytree* ptr)
{
if(ptr==NULL) return;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->right;
}
}
void bst22ll(mytree** tree,mytree** head)
{
static mytree* prev=NULL;
if(*tree==NULL) return;
mytree* ptr=*tree;
if((*tree)->left!=NULL)
bst22ll(&(*tree)->left,head);
*tree=ptr;
if(*head==NULL)
{
*head=*tree;
(*head)->left==NULL;
}
else
{
prev->right=*tree;
(*tree)->left=prev;
}
prev=*tree;
if((*tree)->right!=NULL)
{
bst22ll(&(*tree)->right,head);
}
}
void bst2ll(mytree** tree)
{
if(tree==NULL) return;
mytree* head=NULL;
mytree* prev=NULL;
bst22ll(tree,&head);
*tree=head;
}
int main()
{
mytree* tree=NULL;
insert(&tree,50);
insert(&tree,40);
insert(&tree,60);
insert(&tree,30);
insert(&tree,70);
insert(&tree,65);
insert(&tree,45);
insert(&tree,34);
traverse(tree);
bst2ll(&tree);
printf("\n");
traversell(tree);
printf("\n");
}
should print : 30 34 40 45 50 60 65 70
but printing: 30 34 40 45 50 60 70
Thank you
Shubham
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/algogeeks/-/XXMvBSg0t08J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.