1
+ """
2
+ Binary trees are a type of data tree data structure in which a node can only have 0,1 or 2 children only.
3
+ Linked list is a type of linear data structure in which one object/node along with data, also contains the address of next object/node.
4
+ Doubly linked list is a type of linked list in which a node points to both the element before it, and after it.
5
+
6
+ A binary tree can be converted to a doubly linked list based on in order traversal, where left and right pointers point to prev and next element Eg :-
7
+
8
+ 10
9
+ / \
10
+ 12 15
11
+ /\ /
12
+ 25 30 36
13
+
14
+ can be converted to the doubly linked list
15
+
16
+ 25 <-> 12 <-> 30 <-> 10 <-> 36 <-> 15
17
+
18
+ We have used an iterative approach here, keeping the track of head and tail of the doubly linked list,
19
+ recursively solving left subtree, then redirecting the pointers to connect the last element and the current element,
20
+ and finally recursively solving the right subtree
21
+
22
+ """
23
+
24
+
25
+
26
+
27
+ class Node :
28
+ """Class, to define node of the binary tree and dll, has data, left, and right as attributes"""
29
+ def __init__ (self , value ):
30
+ self .data = value
31
+ self .left = None
32
+ self .right = None
33
+
34
+ def binary_tree_to_dll (node , head , tail ):
35
+ """This function converts a binary tree to a doubly linked list with recursive approach
36
+ input: tree root or node, head and tail pointer of DLL
37
+ returns : the head and tail of the the linked lists
38
+ """
39
+
40
+ if node == None :
41
+ return head , tail
42
+
43
+ head , tail = binary_tree_to_dll (node .left , head , tail ) # converting the left subtree
44
+
45
+ # updating the tail of the list to point towards current node
46
+ if head == None :
47
+ head = node
48
+ else :
49
+ tail .right = node
50
+ node .left = tail
51
+ tail = node # shifting the tail to the latest node
52
+
53
+ head , tail = binary_tree_to_dll (node .right , head , tail ) # converting the right subtree
54
+
55
+ return head , tail
56
+
57
+ def print_list (head ):
58
+ """ iterates over the linked list prints the data elements
59
+ input : head of the linked list
60
+ prints the elements, does not return anything
61
+ """
62
+ while head != None :
63
+ print (head .data , end = " " )
64
+ head = head .right
65
+
66
+
67
+ # DRIVER CODE
68
+
69
+ if __name__ == "__main__" :
70
+
71
+ root = Node (10 )
72
+ root .left = Node (12 )
73
+ root .right = Node (15 )
74
+ root .left .left = Node (25 )
75
+ root .left .right = Node (30 )
76
+ root .right .left = Node (36 )
77
+
78
+
79
+ head , tail = None , None
80
+ head , tail = binary_tree_to_dll (root , head , tail )
81
+ print ("\n Equivaltent doubly linked list : " , end = "" )
82
+ print_list (head )
83
+ print ("\n " )
84
+
85
+
86
+
87
+ # Extra examples for testing
88
+ """
89
+ root = Node(-5)
90
+ root.left = Node(10)
91
+ root.right = Node(2)
92
+
93
+ # ans : 10 -5 2
94
+ """
95
+
96
+ """
97
+ root = Node(3)
98
+ root.left = Node(5)
99
+ root.right = Node (7)
100
+ root.left.left = Node(9)
101
+ root.left.right = Node(11)
102
+ root.right.left = Node(13)
103
+ root.right.right = Node(15)
104
+ root.left.left.left = Node(17)
105
+
106
+ # ans : 17 9 5 11 3 13 7 15
107
+ """
0 commit comments