GeeksforGeeks » Trees specific questions

Convert Zig Zag level order Traversal of Tree to Doubly Linked List

(14 posts)
  • Started 1 year ago by Shashank Mani Narayan
  • Latest reply from
  1. chaitanya
    guest
    Posted 1 year ago #

    Consider a Binary Tree. Binary tree must be converted to a doubly linked list in Zig Zag level order Traversal in constant space i.e in place without creating extra nodes .
    Conditions :
    1)The right pointer of the node must be the next pointer in the doubly linked list.
    2)The left pointer of the node must be the previous pointer in the doubly linked list.
    For Example consider the below Binary Tree

                                                1
                                              /    \
                                            2       3
                                          /   \       \
                                        4      5       6
    

    so when converted to doubly linked list shud like 1 3 2 4 5 6 which is zig zag level order traversal of the binary tree.
    NOTE : Must be done in constant space without creating extra nodes and in O(n) time.

  2. Rimu
    Member
    Posted 1 year ago #

    The following algo does it using no extra space but i think time complexity is not too good, it can be improved using some better techniques:

    void PrintGivenLevel(struct node *head,int level,int ltr,int cnt)
    {
    	static struct node *first=NULL,*f=NULL,*last=NULL;
    
    	if(cnt==0)
    	{
    		f=first;
    		first=NULL;
    		last=NULL;
    	}
    	if(head==NULL)
    		return;
    	if(level==1)
    	{
    		if(!first)
    		{
    			first=head;
    			last=head;
    			first->left=NULL;
    		}
    		else
    		{
    			last->right=head;
    			head->left=last;
    			head->right=NULL;
    			last=head;
    		}
    
    	}
    	if(level>0)
    	{
    		if(ltr)
    		{
    			PrintGivenLevel(head->right,level-1,ltr,1);
    			PrintGivenLevel(head->left,level-1,ltr,1);
    		}
    		else
    		{
    			PrintGivenLevel(head->left,level-1,ltr,1);
    			PrintGivenLevel(head->right,level-1,ltr,1);
    		}
    	}
    	if(f&&(cnt==0))
    	{
    		last->right=f;
    		f->left=last;
    	}
    
    }
    
    int height(struct node *head)
    {
    	int lh,rh;
    	if(head==NULL)
    		return 0;
    
    	lh=height(head->left);
    	rh=height(head->right);
    
    	return lh>=rh?(lh+1):(rh+1);
    }
    
    void Print(struct node *head)
    {
    	int i=0;
    	int ltr;
    	int h=height(head);
    
    	if(h%2==0)
    		ltr=1;
    	else
    		ltr=-1;
    
    	for(i=h;i>=0;i--)
    	{
    		Print(head,i,ltr,0);
    		ltr=~ltr;
    	}
    
    }

    Call the function Print(root) to start the conversion.

  3. codeseeker
    guest
    Posted 1 year ago #

    I was thinking of implementing the above thing by the following algorithm.

    Make use of two stacks (CurrentStack and NextOrderStack) and a variable that tell us whether to traverse from left-to-right or right-to-left.

    1. Start with the root node and place it in the CurrentStack.
    2. While the current stack is empty:
    3. Pop the value from the current stack, print the value and check the order from the variable.
    (i) if order is left to right, insert the left child of the popped node into the NextOrderStack first and then the right child of the popped node.
    (ii) if order is right to left, insert the right child of the popped node into the NextOrderStack first and then the left child of the popped node.
    4. Change the stack from CurrentStack and NextOrderStack and repeat step 2 and 3.
    5. Continue these till both the stacks become empty.

  4. Rimu
    Member
    Posted 1 year ago #


  5. TK
    guest
    Posted 1 year ago #

    Algorithm :
    The problem can be solved recursively.
    1. Convert the left subtree into a doubly linked list
    2. Convert the right subtree into doubly linked list
    3. root.left= the rightmost node of the doubly linked list of the left subtree.
    4. root.right=the leftmost node of the doubly linked list of the right subtree.
    5 return root.
    At the end of recursion, from the main routine, traverse to the leftmost node
    which gives the headnode of the doubly linked list.
    You can find a perfectly working Java implementation in the below link.
    http://cslabprograms.blogspot.com/2011/02/convert-binary-tree-to-doubly-linked.html

  6. wgpshashank

    Posted 1 year ago #

    HI guys Plz Check it & Let me know id there any flaw int this program

    remember i have used globale pointer to point the head & node in doublyu linked list

    as "struct node *rslt"

    http://codepad.org/dvnkbpaF

    have a look at my prog & correct me if i am wrong

  7. Adam
    guest
    Posted 1 year ago #

    Looks like this code has been copied from somewhere. ;)

  8. Kasoota_Sirjee
    guest
    Posted 1 year ago #

    @wgpshashank
    It is mention in the question that complexity should be O(n).
    And your code seems to traversing each of 'i' level node ( d-i) times , where 'd' is depth of the tree.
    So i thik using two stack is fine.

  9. SAMBHU_SHIKARI
    guest
    Posted 1 year ago #

    SIRJI ..HAS MADE SIMPLE CODE COMPLEX ....JUST TO SOLVE A PROBLEM WITH COMPLEXITY n THE NUMBER OF ITERATIONS USED IN ...EXCEEDING THE LIMITS OF THE POSSIBLE NUMBER OF ITERATIONS IN THE GIVEN CODE OF JUST 2 LINES....

  10. Tebis=23
    guest
    Posted 1 year ago #

    @wgpshashank
    i am not able to complie the code. should i use #include<sirji.h> ??

  11. shashank __JAVA GEEK
    guest
    Posted 1 year ago #

    AM I PAGAL????????..............PLZZ GIVE UR COMMENTS........URGENTLY REQUIRED...

  12. sirjee ka bhokaal
    guest
    Posted 1 year ago #

    @Tebis=23
    Since sirjee.h file is in every directory of universe. you just write #include"sirjee.h"

  13. Vijay Nag
    guest
    Posted 9 months ago #

    This program segfaulted.

  14. Vijay Nag
    guest
    Posted 8 months ago #

    Couple of corrections in the code.

    struct node
    {
      int data;
      node *left;
      node *right;
    };
    
    void PrintGivenLevel(struct node *head,int level,int ltr,int cnt)
    {
      static struct node *first=NULL,*f=NULL,*last=NULL;
    
      if(cnt==0)
      {
        f=first;
        first=NULL;
        last=NULL;
      }
      if(head==NULL)
        return;
      if(level==1)
      {
        if(!first)
        {
          first=head;
          last=head;
          first->left=NULL;
        }
        else
        {
          last->right=head;
          head->left=last;
          head->right=NULL;
          last=head;
        }
    
      }
      if(level>0)
      {
        if(ltr == 1)
        {
          PrintGivenLevel(head->right,level-1,ltr,1);
          PrintGivenLevel(head->left,level-1,ltr,1);
        }
        else
        {
          PrintGivenLevel(head->left,level-1,ltr,1);
          PrintGivenLevel(head->right,level-1,ltr,1);
        }
      }
      if(f&&(cnt==0))
      {
        last->right=f;
        f->left=last;
      }
    
    }
    
    int height(struct node *head)
    {
      int lh,rh;
      if(head==NULL)
        return 0;
    
      lh=height(head->left);
      rh=height(head->right);
    
      return lh>=rh?(lh+1):(rh+1);
    }
    
    void Print(struct node *head)
    {
      int i=0;
      int ltr;
      int h=height(head);
    
      if(h%2==0)
        ltr= -1;
      else
        ltr= 1;
    
      for(i=h;i>0; --i)
      {
        PrintGivenLevel(head,i,ltr,0);
        ltr= -ltr;
      }
    
    }
    
    void PrintLinkedList(node *head)
    {
      while(head != NULL) {
        printf("%d ", head->data);
        head = head->right;
      }
    
      printf("\n");
    }
    
    int main()
    {
      node a,b,c,d,e,f;
    
      a.data = 1;
      b.data = 2;
      c.data = 3;
      d.data = 4;
      e.data = 5;
      f.data = 6;
    
      a.left = &b;
      a.right = &c;
      b.left = &d;
      b.right = &e;
      c.right = &f;
    
      c.left = d.left = d.right = e.left = e.right = f.left = f.right = NULL;
    
      Print(&a);
      PrintLinkedList(&a);
    }
    

Reply

You must log in to post.

RSS feed for this topic