Re: [algogeeks] BST to DLL code: whats wrong with this code........

2012-09-03 Thread manish untwal
if u r getting desired output ? then what is the problem?

On Sun, Sep 2, 2012 at 7:24 PM, shubham jain coolshubham...@gmail.comwrote:

 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.

 #includestdio.h
 #includestdlib.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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
With regards,
Manish kumar untwal
Indian Institute of Information Technology
Allahabad (2009-2013 batch)

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] BST to DLL code: whats wrong with this code........

2012-09-03 Thread nikki
Sorry by mistake i write that

Actually I am not getting desired output..

On Mon, Sep 3, 2012 at 11:02 AM, manish untwal manishuntw...@gmail.comwrote:

 if u r getting desired output ? then what is the problem?


 On Sun, Sep 2, 2012 at 7:24 PM, shubham jain coolshubham...@gmail.comwrote:

 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.

 #includestdio.h
 #includestdlib.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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 With regards,
 Manish kumar untwal
 Indian Institute of Information Technology
 Allahabad (2009-2013 batch)


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] BST to DLL code: whats wrong with this code........

2012-09-02 Thread shubham jain
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.

#includestdio.h
#includestdlib.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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.