-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathBinary_Tree_to_Doubly_Linked_List.cpp
132 lines (110 loc) · 2.74 KB
/
Binary_Tree_to_Doubly_Linked_List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
This code gets the values of binary tree from the user and then convert the binary tree to doubly linked list.
The formed Doubly linked list will have elements in the same order as that of inorder traversal of binary tree.
*/
#include <bits/stdc++.h>
using namespace std;
struct TreeNode{
int value;
TreeNode* left = NULL;
TreeNode* right = NULL;
};
TreeNode* newNode(int n){
TreeNode* t = new TreeNode;
t->value = n;
t->left = NULL;
t->right = NULL;
}
//This function inserts node into the binary tree
TreeNode* insert(TreeNode* t, queue<TreeNode* > &q, int v){
TreeNode* temp = newNode(v);
if(v==-1){
temp = NULL;
}
if(t==NULL){
t = temp;
}
else if(q.front()->left==NULL){
q.front()->left = temp;
}
else{
q.front()->right = temp;
q.pop();
}
q.push(temp);
return t;
}
//This function converts given vector into Binary Tree
TreeNode* make_tree(vector<int> v){
queue<TreeNode* > q;
TreeNode* t = NULL;
for(int i=0;i<v.size();i++){
t = insert(t,q,v[i]);
}
return t;
}
//This function prints all the elements of doubly linked list
void print_elements(TreeNode* &T){
TreeNode* temp = T;
cout<<"The elements of doubly linked list are: ";
while(temp!=NULL){
cout<<temp->value<<" ";
temp = temp->right;
}
}
//This function converts Binary tree to doubly linked list
void to_dll(TreeNode* T, TreeNode* &temp){
if(T==NULL){
return;
}
to_dll(T->right,temp);
T->right = temp;
if(temp!=NULL){
temp->left = T;
}
temp = T;
to_dll(T->left,temp);
}
void to_doubly_linked_list(TreeNode* T){
TreeNode* h = NULL;
to_dll(T,h);
print_elements(h);
}
int main(){
cout<<"Enter number of nodes of binary tree: "<<endl;
int node; //number of nodes of binary tree
cin>>node;
cout<<"Enter the values of nodes level by level and enter -1 if node is NULL: "<<endl;
vector<int> binary_tree_values;
while(node){ //asking user for elements of Binary tree
int node_value;
cin>>node_value;
binary_tree_values.push_back(node_value);
node--;
}
cout<<"Making binary tree from the vector..."<<endl;
TreeNode* T = make_tree(binary_tree_values);
cout<<"Converting Binary tree to doubly linked list..."<<endl;
to_doubly_linked_list(T);
return 0;
}
/*
Time Complexity : O(N)
Space Complexity : O(N)
Here if we give following binary tree as input:
5
/ \
6 8
/ \ / \
3 4 9 2
i.e,
if input is -
Enter number of nodes of binary tree:
7
Enter the values of nodes level by level and enter -1 if node is NULL:
5 6 8 3 4 9 2
The output will be -
Making binary tree from the vector...
Converting Binary tree to doubly linked list...
The elements of doubly linked list are: 3 6 4 5 9 8 2
*/