-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConnect Nodes.cpp
226 lines (175 loc) · 4.28 KB
/
Connect Nodes.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include<iostream>
#include<stack>
#include<queue>
#include<cstdio>
#include<cassert>
#include<signal.h>
using namespace std;
int max_height=0,no_of_elements=0;
struct tree
{
int data;
struct tree *left,*right,*next;
};
struct tree* create_tree(int val)
{
struct tree* node=new tree;
node->left=NULL;
node->right=NULL;
node->data=val;
node->next=NULL;
return node;
}
void inorder(struct tree *root)
{
//struct tree *node=root;
if(root!=NULL)
{
inorder(root->left);
cout<<root->data<<"->";
inorder(root->right);
}
}
int preorder(struct tree*root)
{
if(root!=NULL)
{
cout<<root->data<<"->";
preorder(root->left);
preorder(root->right);
}
}
void special_display(struct tree *root)
{
if(root!=NULL)
{
special_display(root->left);
cout<<root->data<<"->";
if(root->next)
cout<<root->next->data<<"\n";
else
cout<<"NULL\n";
special_display(root->right);
}
}
struct tree* create_temp()
{
struct tree *root=create_tree(7);
root->left=create_tree(3);
root->right=create_tree(11);
root->left->left=create_tree(1);
root->left->right=create_tree(5);
// root->left->left->left=create_tree(0);
// root->left->left->right=create_tree(2);
root->left->right->left=create_tree(4);
root->left->right->right=create_tree(6);
root->right->left=create_tree(9);
root->right->right=create_tree(13);
root->right->left->left=create_tree(8);
root->right->left->right=create_tree(10);
root->right->right->left=create_tree(12);
root->right->right->right=create_tree(14);
/* struct tree* root = create_tree(1);
root->left = create_tree(2);
root->right = create_tree(3);
root->left->left = create_tree(4);
root->left->right = create_tree(5);
root->right->left = create_tree(6);
root->right->right = create_tree(7);
root->left->left->left = create_tree(8);
root->left->right->right = create_tree(9);
root->right->right->left = create_tree(10);
*/
return root;
}
struct tree * connect_nodes_at_same_level_method1(struct tree *root)
{
queue<struct tree *>q;
q.push(root);
int cnt=1;
struct tree *temp=NULL;;
while(!q.empty())
{
while(cnt--)
{
if(temp)
temp->next=q.front();
temp=q.front();
q.pop();
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
}
temp=NULL;
cnt=q.size();
}
return root;
}
void connect_nodes_at_same_level_method2(struct tree *root)
{
if(root==NULL)
return;
connect_nodes_at_same_level_method2(root->next);
if(root->left)
{
if(root->right)
{
root->left->next=root->right;
if(!root->next)
root->right->next=NULL;
else if(root->next->left)
root->right->next=root->next->left;
else
root->right->next=root->next->right;
}
else
{
if(!root->next)
root->left->next=NULL;
else if(root->next->left)
root->left->next=root->next->left;
else
root->left->next=root->next->right;
}
}
else if(root->right)
{
if(!root->next)
root->right->next=NULL;
else if(root->next->left)
root->right->next=root->next->left;
else
root->right->next=root->next->right;
}
connect_nodes_at_same_level_method2(root->left);
}
struct tree * connect_nodes_at_same_level(struct tree *root)
{
if(root==NULL)
return NULL;
// connect_nodes_at_same_level_method1(root);
connect_nodes_at_same_level_method2(root);
}
int main()
{
struct tree *root= create_temp(),*root2=NULL,*temp=NULL;
connect_nodes_at_same_level(root);
special_display(root);
cout<<"\n";
// Let us check the values of nextRight pointers
printf("Following are populated nextRight pointers in the tree "
"(-1 is printed if there is no nextRight) \n");
printf("nextRight of %d is %d \n", root->data,
root->next? root->next->data: -1);
printf("nextRight of %d is %d \n", root->left->data,
root->left->next? root->left->next->data: -1);
printf("nextRight of %d is %d \n", root->right->data,
root->right->next? root->right->next->data: -1);
printf("nextRight of %d is %d \n", root->left->left->data,
root->left->left->next? root->left->left->next->data: -1);
printf("nextRight of %d is %d \n", root->right->right->data,
root->right->right->next? root->right->right->next->data: -1);
getchar();
return 0;
}