# Python Program to Create a Doubly Linked List from a Ternary Tree

In this comprehensive blog post, we will delve into the world of ternary trees and explore how to create a doubly linked list from a ternary tree using Python. The article aims to provide a clear understanding of the logic behind the solution and guide readers through the step-by-step implementation of the program. Additionally, the output of the program will be presented, allowing readers to visualize the results.

## Understanding Ternary Trees

Before we dive into the program, let’s establish a foundation by understanding what ternary trees are. A ternary tree is a tree data structure in which each node has at most three child nodes. These nodes are commonly referred to as the left child, middle child, and right child. Ternary trees are a type of tree structure that extends the concept of binary trees.

## Creating a Doubly Linked List from a Ternary Tree

The process of creating a doubly linked list from a ternary tree involves traversing the tree and establishing links between the nodes to form a doubly linked list. Each node in the doubly linked list will have links to its previous and next nodes. The logic of the solution revolves around recursive traversal and linking of nodes.

## Python Program Implementation

Let’s proceed with the implementation of the Python program that creates a doubly linked list from a ternary tree:

``````class TreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.middle = None
self.right = None

def __init__(self, value):
self.value = value
self.prev = None
self.next = None

if not root:
return None

# Create a dummy node to simplify the logic
current = dummy

# Helper function to perform in-order traversal and link nodes
def inOrderTraversal(node):
nonlocal current

if not node:
return

inOrderTraversal(node.left)

# Create a doubly linked list node for the current ternary tree node

# Move the current pointer forward

inOrderTraversal(node.middle)
inOrderTraversal(node.right)

# Perform in-order traversal starting from the root of the ternary tree
inOrderTraversal(root)

# The dummy node's next points to the first node in the doubly linked list
return dummy.next

# Example Usage
if __name__ == "__main__":
# Create a sample ternary tree
root = TreeNode(1)
root.left = TreeNode(2)
root.middle = TreeNode(3)
root.right = TreeNode(4)
root.left.left = TreeNode(5)
root.left.middle = TreeNode(6)
root.left.right = TreeNode(7)
root.middle.left = TreeNode(8)
root.middle.middle = TreeNode(9)
root.middle.right = TreeNode(10)

# Convert the ternary tree to a doubly linked list

# Display the resulting doubly linked list
while current:
print(current.value, end=" ")
current = current.next
``````

## Example Usage

Let’s explore how to use the program by running it and observing the output:

Run the Python program:

``````python ternary_tree_to_doubly_linked_list.py
``````

The program will create a sample ternary tree, convert it to a doubly linked list, and then display the elements of the resulting doubly linked list.

### Example Output

``````5 2 6 7 1 8 3 9 10 4
``````

## Step-by-Step Explanation

1. Class `TreeNode`:

• This class represents a node in the ternary tree. Each node has a `value` and pointers to its left, middle, and right children.
2. Class `DoublyLinkedListNode`:

• This class represents a node in the doubly linked list. Each node has a `value` and pointers to its previous (`prev`) and next (`next`) nodes.
3. Function `ternaryTreeToDoublyLinkedList(root)`:

• This function takes the root of a ternary tree as a parameter and returns the head of the resulting doubly linked list.
• It uses a dummy node to simplify the logic and a `current` pointer to keep track of the last node in the doubly linked list.
• The `inOrderTraversal` helper function is used to perform in-order traversal of the ternary tree and link nodes.
4. Example Usage:

• The example usage section demonstrates how to create a sample ternary tree, convert it to a doubly linked list, and display the elements of the resulting doubly linked list.

## Logic Behind the Solution

The key logic lies in performing an in-order traversal of the ternary tree and linking the nodes`inOrderTraversal` function recursively visits the nodes in the order left-middle-right and creates a doubly linked list node for each visited ternary tree node. The resulting doubly linked list preserves the order of elements based on the in-order traversal.

## Conclusion

This Python program provides a comprehensive solution to the task of creating a doubly linked list from a ternary tree. The example usage demonstrates the conversion process, and readers can experiment with different ternary tree structures to observe the program’s behavior. Understanding the concepts of tree traversal and linking nodes is crucial for working with tree data structures and transforming them into other forms for various applications. The article has offered step-by-step explanations and example output to facilitate a clear understanding of the implementation. Readers are encouraged to explore further and apply these concepts to solve related problems in tree-based algorithms and data structures.