Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to create a doubly linked list from a ternary tree
When it is required to create a doubly linked list from a ternary tree, a ‘Node’ class needs to be created. In this class, there are two attributes, the data that is present in the node, and the access to the next node of the linked list.
Another ‘linked_list’ class needs to be created that would have an initialization function, and the head of the node would be initialized to ‘None’.
In a doubly linked list, the nodes have pointers. The current node would have a pointer to the next node as well as the previous node. The last value in the list will have ‘NULL’ value in the next pointer. It can be traversed in both the directions.
Multiple methods are defined by the user to convert the given doubly linked list to a ternary tree, and to print the node values.
Below is a demonstration for the same −
Example
class Node:
def __init__(self, my_data):
self.right = None
self.data = my_data
self.left = None
self.mid = None
class ternary_tree_to_list:
def __init__(self):
self.root = None
self.head = None
self.tail = None
def convert_ternary_tree_to_list(self, node_val):
if node_val is None:
return
left = node_val.left;
mid = node_val.mid;
right = node_val.right;
if (self.head == None) :
self.head = self.tail = node_val
node_val.middle = None
self.head.left = None
self.head.right = None
else:
self.tail.right = node_val
node_val.left = self.tail
node_val.mid = None
self.tail = node_val
self.tail.right = None
self.convert_ternary_tree_to_list(left)
self.convert_ternary_tree_to_list(mid)
self.convert_ternary_tree_to_list(right)
def print_it(self):
curr = self.head
if (self.head == None):
print("The list is empty")
return
print("The nodes are :")
while curr != None:
print(curr.data)
curr = curr.right
my_instance = ternary_tree_to_list()
print("Elements are being added to the list")
my_instance.root = Node(10)
my_instance.root.left = Node(14)
my_instance.root.mid = Node(24)
my_instance.root.right = Node(17)
my_instance.root.left.left = Node(22)
my_instance.root.left.mid = Node(23)
my_instance.root.mid.left = Node(24)
my_instance.root.mid.mid = Node(28)
my_instance.root.mid.right = Node(30)
my_instance.root.right.left = Node(45)
my_instance.root.right.mid = Node(50)
my_instance.root.right.right = Node(80)
my_instance.convert_ternary_tree_to_list(my_instance.root)
my_instance.print_it()
Output
Elements are being added to the list The nodes are : 10 14 22 23 24 24 28 30 17 45 50 80
Explanation
- The ‘Node’ class is created.
- Another class with required attributes is created.
- Another method named ‘convert_ternary_tree_to_list’ is defined, that is used to convert the given doubly linked list into a ternary tree.
- Another method named ‘print_it’ is defined, that displays the nodes of the circular linked list.
- An object of the ‘ternary_tree_to_list’ class is created, and the methods are called on it to convert the doubly linked list to a ternary tree.
- An ‘init’ method is defined, that the root, head, and tail nodes of the doubly linked list to None.
- The ‘convert_ternary_tree_to_list’ method is called.
- It iterates through the doubly linked list, and converts it into a ternary tree.
- This is displayed on the console using the ‘print_it’ method.