-
Notifications
You must be signed in to change notification settings - Fork 139
/
Buring Tree.cpp
179 lines (142 loc) · 3.25 KB
/
Buring Tree.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
// C++ implementation to print the sequence
// of burning of nodes of a binary tree
#include <bits/stdc++.h>
using namespace std;
// A Tree node
struct Node {
int key;
struct Node *left, *right;
};
// Utility function to create a new node
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
// Utility function to print the sequence of burning nodes
int burnTreeUtil(Node* root, int target, queue<Node*>& q)
{
// Base condition
if (root == NULL) {
return 0;
}
// Condition to check whether target
// node is found or not in a tree
if (root->key == target) {
cout << root->key << endl;
if (root->left != NULL) {
q.push(root->left);
}
if (root->right != NULL) {
q.push(root->right);
}
// Return statements to prevent
// further function calls
return 1;
}
int a = burnTreeUtil(root->left, target, q);
if (a == 1) {
int qsize = q.size();
// Run while loop until size of queue
// becomes zero
while (qsize--) {
Node* temp = q.front();
// Printing of burning nodes
cout << temp->key << " , ";
q.pop();
// Check if condition for left subtree
if (temp->left != NULL)
q.push(temp->left);
// Check if condition for right subtree
if (temp->right != NULL)
q.push(temp->right);
}
if (root->right != NULL)
q.push(root->right);
cout << root->key << endl;
// Return statement it prevents further
// further function call
return 1;
}
int b = burnTreeUtil(root->right, target, q);
if (b == 1) {
int qsize = q.size();
// Run while loop until size of queue
// becomes zero
while (qsize--) {
Node* temp = q.front();
// Printing of burning nodes
cout << temp->key << " , ";
q.pop();
// Check if condition for left subtree
if (temp->left != NULL)
q.push(temp->left);
// Check if condition for left subtree
if (temp->right != NULL)
q.push(temp->right);
}
if (root->left != NULL)
q.push(root->left);
cout << root->key << endl;
// Return statement it prevents further
// further function call
return 1;
}
}
// Function will print the sequence of burning nodes
void burnTree(Node* root, int target)
{
queue<Node*> q;
// Function call
burnTreeUtil(root, target, q);
// While loop runs unless queue becomes empty
while (!q.empty()) {
int qSize = q.size();
while (qSize > 0) {
Node* temp = q.front();
// Printing of burning nodes
cout << temp->key;
// Insert left child in a queue, if exist
if (temp->left != NULL) {
q.push(temp->left);
}
// Insert right child in a queue, if exist
if (temp->right != NULL) {
q.push(temp->right);
}
if (q.size() != 1)
cout << " , ";
q.pop();
qSize--;
}
cout << endl;
}
}
// Driver Code
int main()
{
/* 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree as shown
above */
Node* root = newNode(10);
root->left = newNode(12);
root->right = newNode(13);
root->right->left = newNode(14);
root->right->right = newNode(15);
root->right->left->left = newNode(21);
root->right->left->right = newNode(22);
root->right->right->left = newNode(23);
root->right->right->right = newNode(24);
int targetNode = 14;
// Function call
burnTree(root, targetNode);
return 0;
}