-
Notifications
You must be signed in to change notification settings - Fork 2
/
flatten_BST.cpp
187 lines (155 loc) · 4.55 KB
/
flatten_BST.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
flatten a BST into a sorted linked list (linear data structure)
reuse the same nodes with creating a new linked list.
ie. change the tree pointers
every node will return the head and the tail pointers of the linked list constructed through that subtree(node included)
there are 4 cases:
root node could become the part of linked list
root node could become the head if left subtree doesn't exist
root node could become the tail if right subtree doesn't exist
root node could become both head and tail if it is the leaf node.
we do a postorder traversal, LRN
*/
#include <iostream>
#include <queue>
using namespace std;
class node {
public:
int data;
node *left;
node *right;
node(int data) {
this->data = data;
left = right = NULL;
}
};
// print tree, level by level
void BFS(node *root) {
if(root == NULL)
return;
queue<node*> q;
q.push(root);
// for newline
q.push(NULL);
while(!q.empty()) {
node *front = q.front();
q.pop();
if(front == NULL) {
cout << endl;
if(!q.empty())
q.push(NULL);
}
else {
cout << front->data << " ";
// push the children of front if they exist
if(front->left)
q.push(front->left);
if(front->right)
q.push(front->right);
}
}
}
// accepts the old root node and returns the new root node
node* insert(node *root, int data) {
// base case
if(root == NULL)
return new node(data);
// recursive calls
if(data <= root->data)
root->left = insert(root->left, data);
else
root->right = insert(root->right, data);
return root;
}
node* build() {
// read a list of numbers till -1 and insert them into BST
int data;
cin >> data;
node *root = NULL;
while(data != -1) {
root = insert(root, data);
cin >> data;
}
return root;
}
class LinkedList {
public:
node *head;
node *tail;
};
LinkedList flatten(node *root) {
LinkedList ll;
// base case
if(root == NULL) {
ll.head = ll.tail = NULL;
return ll;
}
// case-1: leaf node
if(root->left == NULL and root->right == NULL) {
ll.head = ll.tail = root;
return ll;
}
// case-2: left is not null and right is null
if(root->left != NULL and root->right == NULL) {
// convert left subtree into linked list
LinkedList left = flatten(root->left);
// connect tail of the subtree with the root node
left.tail->right = root;
// update the tail to be the root node
ll.tail = root;
// update the head which will be the head of the left subtree
ll.head = left.head;
return ll;
}
// case-3: right is not null and left is not null
else if(root->left == NULL and root->right != NULL) {
// convert the right subtree into linked list
LinkedList right = flatten(root->right);
// make root point to the head of the right linked list
root->right = right.head;
// update the head to be the root node
ll.head = root;
// update the tail to be the tail node of the right subtree
ll.tail = right.tail;
return ll;
}
// case-4: both sides are not null
else {
// do a postorder traversal: LRN
// convert left subtree into linked list
LinkedList left = flatten(root->left);
// convert right subtree into linked list
LinkedList right = flatten(root->right);
// make tail of left linked list point to root
left.tail->right = root;
// make root point to the head of the right linked list
root->right = right.head;
// update the head to be the head of the left linked list
ll.head = left.head;
// update the tail to be the tail of the right linked list
ll.tail = right.tail;
return ll;
}
}
istream& operator>>(istream& cin, node *&root) {
root = build();
return cin;
}
ostream& operator<<(ostream& cout, node *root) {
BFS(root);
return cout;
}
int main() {
node *root = NULL;
cin >> root;
cout << "original tree: " << endl;
cout << root << endl;
LinkedList ll = flatten(root);
node *temp = ll.head;
cout << "sorted linked list: ";
while(temp != NULL) {
cout << temp->data << " ";
temp = temp->right;
}
return 0;
}