# Python program to convert a given binary tree to doubly linked list.

python tutorials and learn python

## Python program to convert a given binary tree to doubly linked list.

In this program, we need to convert the given binary tree to corresponding doubly liked list.

The binary tree is a tree data structure in which each node has at most two children node.

This can be achieved by traversing the tree in the in-order manner that is, left the child -> root ->right node. Traverse left sub-tree and convert it into the doubly linked list by adding nodes to the end of the list. In this way, the leftmost node will become head of the list. Then, convert the right sub-tree into the doubly linked list.  ### ALGORITHM:

1. Define a Node class which represents a node in the binary tree. It will have three properties: data left, and right where the left and right represent two children of a node.
2. Root will represent the root of the binary tree. Head and tail node represent the head and tail of the doubly linked list.
3. BinaryTreeToDLL() will convert the given binary tree to the corresponding doubly linked list.
• Perform inorder traversal of the binary tree by converting the left subtree first.
• If the list is empty, both head and tail will point to a node.
• If the list is not empty, the node will be inserted at the end of the list. Here, pointer left, and right will represent the previous and next pointer of the doubly linked list.
• Now, recursively call BinaryTreeToDLL() to convert the right subtree.

a. display() will show all the nodes present in the list.

• Define a new node ‘current’ that will point to the head.
• Print current.data till current points to null.
• Current will point to the next node in the list in each iteration.

### PROGRAM:

1. #Represent a node of binary tree
2. class Node:
3.     def __init__(self,data):
4.         self.data = data;
5.         self.left = None;
6.         self.right = None;
7. class BinaryTreeToDLL:
8.     def __init__(self):
9.         #Represent the root of binary tree
10.         self.root = None;
13.         self.tail = None;
14.     #convertbtToDLL() will convert the given binary tree to corresponding doubly linked list
15.     def convertbtToDLL(self, node):
16.         #Checks whether node is None
17.         if(node == None):
18.             return;
19.         #Convert left subtree to doubly linked list
20.         self.convertbtToDLL(node.left);
21.         #If list is empty, add node as head of the list
23.             #Both head and tail will point to node
24.             self.head = self.tail = node;
25.         #Otherwise, add node to the end of the list
26.         else:
27.             #node will be added after tail such that tail’s right will point to node
28.             self.tail.right = node;
29.             #node’s left will point to tail
30.             node.left = self.tail;
31.             #node will become new tail
32.             self.tail = node;
33.         #Convert right subtree to doubly linked list
34.         self.convertbtToDLL(node.right);
35.     #display() will print out the nodes of the list
36.     def display(self):
37.         #Node current will point to head
40.             print(“List is empty”);
41.             return;
42.         print(“Nodes of generated doubly linked list: “);
43.         while(current != None):
44.             #Prints each node by incrementing pointer.
45.             print(current.data),
46.             current = current.right;
47. bt = BinaryTreeToDLL();
48. #Add nodes to the binary tree
49. bt.root = Node(1);
50. bt.root.left = Node(2);
51. bt.root.right = Node(3);
52. bt.root.left.left = Node(4);
53. bt.root.left.right = Node(5);
54. bt.root.right.left = Node(6);
55. bt.root.right.right = Node(7);
56. #Converts the given binary tree to doubly linked list
57. bt.convertbtToDLL(bt.root);
58. #Displays the nodes present in the list
59. bt.display();

Output:

```Nodes of generated doubly linked list:
4 2 5 1 6 3 7```