Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to convert a given binary tree to doubly linked list
When it is required to convert a given binary tree to a doubly linked list, a Node class needs to be created. In this class, there are two attributes: the data that is present in the node, and pointers to the left and right nodes.
A binary tree is a non-linear data structure which contains one root node, and every node can have at most two children. In a doubly linked list, nodes have pointers to both the next and previous nodes, allowing traversal in both directions.
The conversion follows an in-order traversal approach, where we visit left subtree, current node, then right subtree. This maintains the sorted order in the resulting doubly linked list.
Node Class Structure
First, we define a Node class that will serve both as a binary tree node and doubly linked list node ?
class Node:
def __init__(self, data):
self.data = data
self.left = None # Left child in tree, previous node in DLL
self.right = None # Right child in tree, next node in DLL
Binary Tree to Doubly Linked List Conversion
The conversion algorithm uses in-order traversal to maintain the sorted order ?
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class BinaryTreeToDLL:
def __init__(self):
self.head = None
self.tail = None
def convert_tree_to_list(self, node):
if node is None:
return
# Process left subtree
self.convert_tree_to_list(node.left)
# Process current node
if self.head is None:
# First node becomes head
self.head = self.tail = node
else:
# Link current node to the end of DLL
self.tail.right = node
node.left = self.tail
self.tail = node
# Process right subtree
self.convert_tree_to_list(node.right)
def print_dll(self):
if self.head is None:
print("The list is empty")
return
print("Doubly Linked List (Forward):")
current = self.head
while current is not None:
print(current.data, end=" ")
current = current.right
print()
def print_dll_reverse(self):
if self.tail is None:
print("The list is empty")
return
print("Doubly Linked List (Backward):")
current = self.tail
while current is not None:
print(current.data, end=" ")
current = current.left
print()
# Create binary tree
converter = BinaryTreeToDLL()
root = Node(10)
root.left = Node(14)
root.right = Node(17)
root.left.left = Node(22)
root.left.right = Node(29)
root.right.left = Node(45)
root.right.right = Node(80)
print("Converting binary tree to doubly linked list...")
converter.convert_tree_to_list(root)
converter.print_dll()
converter.print_dll_reverse()
Converting binary tree to doubly linked list... Doubly Linked List (Forward): 22 14 29 10 45 17 80 Doubly Linked List (Backward): 80 17 45 10 29 14 22
How the Algorithm Works
The conversion follows these steps:
- In-order Traversal: Visit left subtree ? current node ? right subtree
- Link Nodes: Connect each visited node to form a doubly linked list
- Maintain Pointers: Update head and tail pointers as nodes are processed
- Reuse Structure: The same Node class serves both tree and list purposes
Time and Space Complexity
| Complexity | Value | Explanation |
|---|---|---|
| Time | O(n) | Visit each node exactly once |
| Space | O(h) | Recursion stack depth equals tree height |
Conclusion
Converting a binary tree to a doubly linked list uses in-order traversal to maintain sorted order. The algorithm runs in O(n) time and reuses the existing node structure by repurposing left/right pointers as previous/next pointers.
