Create a Doubly Linked List from a Ternary Tree
Last Updated :
01 Jul, 2022
Given a ternary tree, create a doubly linked list out of it. A ternary tree is just like a binary tree but instead of having two nodes, it has three nodes i.e. left, middle, and right.
The doubly linked list should hold the following properties –
- The left pointer of the ternary tree should act as prev pointer of the doubly linked list.
- The middle pointer of the ternary tree should not point to anything.
- Right pointer of the ternary tree should act as the next pointer of the doubly linked list.
- Each node of the ternary tree is inserted into the doubly linked list before its subtrees and for any node, its left child will be inserted first, followed by the mid and right child (if any).
For the above example, the linked list formed for below tree should be NULL <- 30 <-> 5 <-> 1 <-> 4 <-> 8 <-> 11 <-> 6 <-> 7 <-> 15 <-> 63 <-> 31 <-> 55 <-> 65 -> NULL

We strongly recommend you to minimize your browser and try this yourself first.
The idea is to traverse the tree in a preorder fashion similar to binary tree preorder traversal. Here, when we visit a node, we will insert it into a doubly linked list, in the end, using a tail pointer. That we use to maintain the required insertion order. We then recursively call for left child, middle child and right child in that order.
Below is the implementation of this idea.
C++
// C++ program to create a doubly linked list out
// of given a ternary tree.
#include <bits/stdc++.h>
using namespace std;
/* A ternary tree */
struct Node
{
int data;
struct Node *left, *middle, *right;
};
/* Helper function that allocates a new node with the
given data and assign NULL to left, middle and right
pointers.*/
Node* newNode(int data)
{
Node* node = new Node;
node->data = data;
node->left = node->middle = node->right = NULL;
return node;
}
/* Utility function that constructs doubly linked list
by inserting current node at the end of the doubly
linked list by using a tail pointer */
void push(Node** tail_ref, Node* node)
{
// initialize tail pointer
if (*tail_ref == NULL)
{
*tail_ref = node;
// set left, middle and right child to point
// to NULL
node->left = node->middle = node->right = NULL;
return;
}
// insert node in the end using tail pointer
(*tail_ref)->right = node;
// set prev of node
node->left = (*tail_ref);
// set middle and right child to point to NULL
node->right = node->middle = NULL;
// now tail pointer will point to inserted node
(*tail_ref) = node;
}
/* Create a doubly linked list out of given a ternary tree.
by traversing the tree in preorder fashion. */
void TernaryTreeToList(Node* root, Node** head_ref)
{
// Base case
if (root == NULL)
return;
//create a static tail pointer
static Node* tail = NULL;
// store left, middle and right nodes
// for future calls.
Node* left = root->left;
Node* middle = root->middle;
Node* right = root->right;
// set head of the doubly linked list
// head will be root of the ternary tree
if (*head_ref == NULL)
*head_ref = root;
// push current node in the end of DLL
push(&tail, root);
//recurse for left, middle and right child
TernaryTreeToList(left, head_ref);
TernaryTreeToList(middle, head_ref);
TernaryTreeToList(right, head_ref);
}
// Utility function for printing double linked list.
void printList(Node* head)
{
printf("Created Double Linked list is:\n");
while (head)
{
printf("%d ", head->data);
head = head->right;
}
}
// Driver program to test above functions
int main()
{
// Constructing ternary tree as shown in above figure
Node* root = newNode(30);
root->left = newNode(5);
root->middle = newNode(11);
root->right = newNode(63);
root->left->left = newNode(1);
root->left->middle = newNode(4);
root->left->right = newNode(8);
root->middle->left = newNode(6);
root->middle->middle = newNode(7);
root->middle->right = newNode(15);
root->right->left = newNode(31);
root->right->middle = newNode(55);
root->right->right = newNode(65);
Node* head = NULL;
TernaryTreeToList(root, &head);
printList(head);
return 0;
}
Java
//Java program to create a doubly linked list
// from a given ternary tree.
//Custom node class.
class newNode
{
int data;
newNode left,middle,right;
public newNode(int data)
{
this.data = data;
left = middle = right = null;
}
}
class GFG {
//tail of the linked list.
static newNode tail;
//function to push the node to the tail.
public static void push(newNode node)
{
//to put the node at the end of
// the already existing tail.
tail.right = node;
//to point to the previous node.
node.left = tail;
// middle pointer should point to
// nothing so null. initiate right
// pointer to null.
node.middle = node.right = null;
//update the tail position.
tail = node;
}
/* Create a doubly linked list out of given a ternary tree.
by traversing the tree in preorder fashion. */
public static void ternaryTree(newNode node,newNode head)
{
if(node == null)
return;
newNode left = node.left;
newNode middle = node.middle;
newNode right = node.right;
if(tail != node)
// already root is in the tail so dont push
// the node when it was root.In the first
// case both node and tail have root in them.
push(node);
// First the left child is to be taken.
// Then middle and then right child.
ternaryTree(left,head);
ternaryTree(middle,head);
ternaryTree(right,head);
}
//function to initiate the list process.
public static newNode startTree(newNode root)
{
//Initiate the head and tail with root.
newNode head = root;
tail = root;
ternaryTree(root,head);
//since the head,root are passed
// with reference the changes in
// root will be reflected in head.
return head;
}
// Utility function for printing double linked list.
public static void printList(newNode head)
{
System.out.print("Created Double Linked list is:\n");
while(head != null)
{
System.out.print(head.data + " ");
head = head.right;
}
}
// Driver program to test above functions
public static void main(String args[])
{
// Constructing ternary tree as shown
// in above figure
newNode root = new newNode(30);
root.left = new newNode(5);
root.middle = new newNode(11);
root.right = new newNode(63);
root.left.left = new newNode(1);
root.left.middle = new newNode(4);
root.left.right = new newNode(8);
root.middle.left = new newNode(6);
root.middle.middle = new newNode(7);
root.middle.right = new newNode(15);
root.right.left = new newNode(31);
root.right.middle = new newNode(55);
root.right.right = new newNode(65);
// The function which initiates the list
// process returns the head.
newNode head = startTree(root);
printList(head);
}
}
// This code is contributed by M.V.S.Surya Teja.
Python3
# Python3 program to create a doubly linked
# list out of given a ternary tree.
# Custom node class.
class newNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
self.middle = None
class GFG:
def __init__(self):
# Tail of the linked list.
self.tail = None
# Function to push the node to the tail.
def push(self, node):
# To put the node at the end of
# the already existing tail.
self.tail.right = node
# To point to the previous node.
node.left = self.tail
# Middle pointer should point to
# nothing so null. initiate right
# pointer to null.
node.middle = node.right = None
# Update the tail position.
self.tail = node
# Create a doubly linked list out of given
# a ternary tree By traversing the tree in
# preorder fashion.
def ternaryTree(self, node, head):
if node == None:
return
left = node.left
middle = node.middle
right = node.right
if self.tail != node:
# Already root is in the tail so dont push
# the node when it was root.In the first
# case both node and tail have root in them.
self.push(node)
# First the left child is to be taken.
# Then middle and then right child.
self.ternaryTree(left, head)
self.ternaryTree(middle, head)
self.ternaryTree(right, head)
def startTree(self, root):
# Initiate the head and tail with root.
head = root
self.tail = root
self.ternaryTree(root, head)
# Since the head,root are passed
# with reference the changes in
# root will be reflected in head.
return head
# Utility function for printing double linked list.
def printList(self, head):
print("Created Double Linked list is:")
while head:
print(head.data, end = " ")
head = head.right
# Driver code
if __name__ == '__main__':
# Constructing ternary tree as shown
# in above figure
root = newNode(30)
root.left = newNode(5)
root.middle = newNode(11)
root.right = newNode(63)
root.left.left = newNode(1)
root.left.middle = newNode(4)
root.left.right = newNode(8)
root.middle.left = newNode(6)
root.middle.middle = newNode(7)
root.middle.right = newNode(15)
root.right.left = newNode(31)
root.right.middle = newNode(55)
root.right.right = newNode(65)
# The function which initiates the list
# process returns the head.
head = None
gfg = GFG()
head = gfg.startTree(root)
gfg.printList(head)
# This code is contributed by Winston Sebastian Pais
C#
// C# program to create a doubly linked
// list from a given ternary tree.
using System;
// Custom node class.
public class newNode
{
public int data;
public newNode left, middle, right;
public newNode(int data)
{
this.data = data;
left = middle = right = null;
}
}
class GFG
{
// tail of the linked list.
public static newNode tail;
// function to push the node to the tail.
public static void push(newNode node)
{
// to put the node at the end of
// the already existing tail.
tail.right = node;
// to point to the previous node.
node.left = tail;
// middle pointer should point to
// nothing so null. initiate right
// pointer to null.
node.middle = node.right = null;
// update the tail position.
tail = node;
}
/* Create a doubly linked list out
of given a ternary tree. by traversing
the tree in preorder fashion. */
public static void ternaryTree(newNode node,
newNode head)
{
if (node == null)
{
return;
}
newNode left = node.left;
newNode middle = node.middle;
newNode right = node.right;
if (tail != node)
{
// already root is in the tail so dont push
// the node when it was root.In the first
// case both node and tail have root in them.
push(node);
}
// First the left child is to be taken.
// Then middle and then right child.
ternaryTree(left, head);
ternaryTree(middle, head);
ternaryTree(right, head);
}
// function to initiate the list process.
public static newNode startTree(newNode root)
{
// Initiate the head and tail with root.
newNode head = root;
tail = root;
ternaryTree(root,head);
// since the head,root are passed
// with reference the changes in
// root will be reflected in head.
return head;
}
// Utility function for printing
// double linked list.
public static void printList(newNode head)
{
Console.Write("Created Double Linked list is:\n");
while (head != null)
{
Console.Write(head.data + " ");
head = head.right;
}
}
// Driver Code
public static void Main(string[] args)
{
// Constructing ternary tree as shown
// in above figure
newNode root = new newNode(30);
root.left = new newNode(5);
root.middle = new newNode(11);
root.right = new newNode(63);
root.left.left = new newNode(1);
root.left.middle = new newNode(4);
root.left.right = new newNode(8);
root.middle.left = new newNode(6);
root.middle.middle = new newNode(7);
root.middle.right = new newNode(15);
root.right.left = new newNode(31);
root.right.middle = new newNode(55);
root.right.right = new newNode(65);
// The function which initiates the list
// process returns the head.
newNode head = startTree(root);
printList(head);
}
}
// This code is contributed by Shrikant13
JavaScript
<script>
//javascript program to create a doubly linked list
// from a given ternary tree.
//Custom node class.
class newNode {
constructor(data) {
this.data = data;
this.left = null;
this.middle = null;
this.right = null;
}
}
// tail of the linked list.
var tail;
// function to push the node to the tail.
function push( node) {
// to put the node at the end of
// the already existing tail.
tail.right = node;
// to point to the previous node.
node.left = tail;
// middle pointer should point to
// nothing so null. initiate right
// pointer to null.
node.middle = node.right = null;
// update the tail position.
tail = node;
}
/*
* Create a doubly linked list out of given a ternary tree. by traversing the
* tree in preorder fashion.
*/
function ternaryTree( node, head) {
if (node == null)
return;
var left = node.left;
var middle = node.middle;
var right = node.right;
if (tail != node)
// already root is in the tail so dont push
// the node when it was root.In the first
// case both node and tail have root in them.
push(node);
// First the left child is to be taken.
// Then middle and then right child.
ternaryTree(left, head);
ternaryTree(middle, head);
ternaryTree(right, head);
}
// function to initiate the list process.
function startTree( root) {
// Initiate the head and tail with root.
var head = root;
tail = root;
ternaryTree(root, head);
// since the head,root are passed
// with reference the changes in
// root will be reflected in head.
return head;
}
// Utility function for printing var linked list.
function printList( head) {
document.write("Created Double Linked list is:<br/>");
while (head != null) {
document.write(head.data + " ");
head = head.right;
}
}
// Driver program to test above functions
// Constructing ternary tree as shown
// in above figure
root = new newNode(30);
root.left = new newNode(5);
root.middle = new newNode(11);
root.right = new newNode(63);
root.left.left = new newNode(1);
root.left.middle = new newNode(4);
root.left.right = new newNode(8);
root.middle.left = new newNode(6);
root.middle.middle = new newNode(7);
root.middle.right = new newNode(15);
root.right.left = new newNode(31);
root.right.middle = new newNode(55);
root.right.right = new newNode(65);
// The function which initiates the list
// process returns the head.
head = startTree(root);
printList(head);
// This code contributed by gauravrajput1
</script>
OutputCreated Double Linked list is:
30 5 1 4 8 11 6 7 15 63 31 55 65
Time Complexity: O(n), as we are using recursion to traverse n times. Where n is the number of nodes in the tree.
Auxiliary Space: O(n), as we are using extra space for the linked list.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem