Hi Guys...I think I am right I have written the code for this..please
go through it...& let me know if any issue....check the ouyout....i
have write the solution for the BT of height 2 ..
1
/ \
2 3
/ \ / \
4 5 6 7
its printing 1237654. now it become DLL so head is pointed by Doubly
Linked start Pointer ..
Prev & Next Pointers of DLL..which was left & right pointers for the
Tree.
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *left;
struct node *right;
int data;
};
struct node *rslt;//Global structure pointer
int height(struct node* head)
{
if(head==NULL)
return 0;
if(head->left==NULL && head->right==NULL)
return 0;
int lh=height(head->left);
int rh=height(head->right);
return lh>rh?(lh+1):(rh+1);
}
struct node* appnd(struct node *a,struct node *b)
{
if(a==NULL)return b;
if(b==NULL)return a;
struct node* result=a;
while(result->right!=NULL)
result=result->right;
result->right=b;
b->left=a;
result=a;
return result;
}
void printGivenLevel(struct node* head,int level,int ltr)
{
struct node *current;
struct node *temp;
if(head==NULL)
return;
if(level==0)
{
printf(" %d ---> ",head->data);
temp=current;
current=head;
rslt=appnd(temp,current);
}
if(level>0)
{
if(ltr)
{
printGivenLevel(head->left,level-1,ltr);
printGivenLevel(head->right,level-1,ltr);
scanf("%d");
}
else
{
printGivenLevel(head->right,level-1,ltr);
printGivenLevel(head->left,level-1,ltr);
}
}
}
void printGivenOrder(struct node* head)
{
int i=0;
int ltr=0;
for(i=0;i<=height(head);i++)
{
printGivenLevel(head,i,ltr);
ltr=~ltr;
}
}
struct node* NewNode(int data)
{
struct node* tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=data;
tmp->left=NULL;
tmp->right=NULL;
return tmp;
}
void printList(struct node* node)
{
struct node* current=node;
while(current!=NULL)
{
printf("%d \n",current->data);
current=current->right;
}
}
int main()
{
struct node* start=NULL;
start=NewNode(1);
start->left=NewNode(2);
start->right=NewNode(3);
start->left->left=NewNode(4);
start->left->right=NewNode(5);
start->right->left=NewNode(6);
start->right->right=NewNode(7);
printGivenOrder(start);
printList(rslt);
return 0;
}
in output extra 4 is printing in the last.....after printing 1237654
(4).....if sum1 able to figure out it..please let know
Thanks & Regards
Shashank ""The best way to escape from a problem is to solve it."
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
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.