
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert a Binary Tree to a Circular Doubly Link List in C++
In this tutorial, we will be discussing a program to convert a binary tree to a circular doubly linked list.
For this, we will be provided with a binary tree. Our task will be to convert the left and right nodes to the left and right elements respectively. And take the inorder of the binary tree to be the sequence order of the circular linked list
Example
#include<iostream> using namespace std; //node structure of the binary tree struct Node{ struct Node *left, *right; int data; }; //appending rightlist to the end of leftlist Node *concatenate(Node *leftList, Node *rightList){ //if one list is empty return the other list if (leftList == NULL) return rightList; if (rightList == NULL) return leftList; Node *leftLast = leftList->left; Node *rightLast = rightList->left; //connecting leftlist to rightlist leftLast->right = rightList; rightList->left = leftLast; leftList->left = rightLast; rightLast->right = leftList; return leftList; } //converting to circular linked list and returning the head Node *bTreeToCList(Node *root){ if (root == NULL) return NULL; Node *left = bTreeToCList(root->left); Node *right = bTreeToCList(root->right); root->left = root->right = root; return concatenate(concatenate(left, root), right); } //displaying circular linked list void print_Clist(Node *head){ cout << "Circular Linked List is :\n"; Node *itr = head; do{ cout << itr->data <<" "; itr = itr->right; } while (head!=itr); cout << "\n"; } //creating new node and returning address Node *newNode(int data){ Node *temp = new Node(); temp->data = data; temp->left = temp->right = NULL; return temp; } int main(){ Node *root = newNode(10); root->left = newNode(12); root->right = newNode(15); root->left->left = newNode(25); root->left->right = newNode(30); root->right->left = newNode(36); Node *head = bTreeToCList(root); print_Clist(head); return 0; }
Output
Circular Linked List is : 25 12 30 10 36 15
- Related Articles
- Convert a given Binary Tree to Doubly Linked List (Set 1) in C++
- Convert a given Binary Tree to Doubly Linked List (Set 2) in C++
- Python program to convert a given binary tree to doubly linked list
- Convert an Array to a Circular Doubly Linked List in C++
- Program to convert a linked list into a binary search tree in C++
- Convert Sorted List to Binary Search Tree in C++
- Program to convert binary search tree to a singly linked list in C++?
- C++ Program to Implement Circular Doubly Linked List
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue) in C++
- Convert Ternary Expression to a Binary Tree in C++
- Convert a Binary Tree into its Mirror Tree in C++
- Doubly Linked List as Circular in Javascript
- Searching an Element in Doubly Circular Linked List using C++
- Python program to create a doubly linked list from a ternary tree
- Convert a given Binary tree to a tree that holds Logical AND property on C++

Advertisements