Convert a given Binary Tree to Circular Doubly Linked List | Set 2

Given a Binary Tree, convert it to a Circular Doubly Linked List.

  • The left and right pointers in nodes are to be used as previous and next pointers respectively in converted Circular Linked List.
  • The order of nodes in List must be same as In order of the given Binary Tree.
  • The first node of Inorder traversal must be the head node of the Circular List.

Example:

An in-place solution to this problem is discussed in previous post.



In this post, a much simpler solution, but using extra O(n) space, is discussed.

In this approach, firstly we will do an In-Order traversal of the given binary tree, we store this traversal in a vector, which will be passed on to the function along with the tree. Now, generate a circular doubly linked list from the elements of the vector.

To generate a circular doubly linked list from the vector, make the first element of the vector as the head of the linked list and also create a current pointer which is right now pointing to head. Now start traversing the array from the second element and do the following.

  1. Create a temporary pointer which points to the current pointer.
  2. Make a new node with the current element of the vector.
  3. Make current’s right point to this new node.
  4. Make current pointer equals to current’s right pointer.
  5. Now finally make the current’s left point to temporary pointer created in the first step.

Do this for all the elements and after the traversal is completed, make current’s right (current is right now pointing to last element of the vector) point to the head of the list and make the head’s left point to the current pointer. Finally, return the head.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// A C++ program for conversion
// of Binary Tree to CDLL
  
#include <bits/stdc++.h>
using namespace std;
  
// A binary tree node has data,
// and left and right pointers
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
  
    Node(int x)
    {
        data = x;
        left = right = NULL;
    }
};
  
// Function to perform In-Order traversal of the
// tree and store the nodes in a vector
void inorder(Node* root, vector<int>& v)
{
    if (!root)
        return;
  
    /* first recur on left child */
    inorder(root->left, v);
  
    /* append the data of node in vector */
    v.push_back(root->data);
  
    /* now recur on right child */
    inorder(root->right, v);
}
  
// Function to convert Binary Tree to Circular
// Doubly Linked list using the vector which stores
// In-Order traversal of the Binary Tree
Node* bTreeToCList(Node* root)
{
    // Base cases
    if (root == NULL)
        return NULL;
  
    // Vector to be used for storing the nodes
    // of tree in In-order form
    vector<int> v;
  
    // Calling the In-Order traversal function
    inorder(root, v);
  
    // Create the head of the linked list pointing
    // to the root of the tree
    Node* head_ref = new Node(v[0]);
  
    // Create a current pointer to be used in traversal
    Node* curr = head_ref;
  
    // Traversing the nodes of the tree starting
    // from the second elements
    for (int i = 1; i < v.size(); i++) {
  
        // Create a temporary pointer
        // pointing to current
        Node* temp = curr;
  
        // Current's right points to the current
        // node in traversal
        curr->right = new Node(v[i]);
  
        // Current points to its right
        curr = curr->right;
  
        // Current's left points to temp
        curr->left = temp;
    }
  
    // Current's right points to head of the list
    curr->right = head_ref;
  
    // Head's left points to current
    head_ref->left = curr;
  
    // Return head of the list
    return head_ref;
}
  
// Display Circular Link List
void displayCList(Node* head)
{
    cout << "Circular Doubly Linked List is :\n";
  
    Node* itr = head;
    do {
        cout << itr->data << " ";
        itr = itr->right;
    } while (head != itr);
  
    cout << "\n";
}
  
// Driver Code
int main()
{
    Node* root = new Node(10);
    root->left = new Node(12);
    root->right = new Node(15);
    root->left->left = new Node(25);
    root->left->right = new Node(30);
    root->right->left = new Node(36);
  
    Node* head = bTreeToCList(root);
    displayCList(head);
  
    return 0;
}

chevron_right


Output:

Circular Doubly Linked List is :
25 12 30 10 36 15

Time Complexity: O(N), where N is the number of nodes in the Binary Tree.
Auxiliary Space: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.