-
Notifications
You must be signed in to change notification settings - Fork 6
/
Reverse a path in BST using queue.cpp
152 lines (124 loc) · 2.64 KB
/
Reverse a path in BST using queue.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
// C++ code to demonstrate insert
// operation in binary search tree
#include <bits/stdc++.h>
using namespace std;
struct node {
int key;
struct node *left, *right;
};
// A utility function to
// create a new BST node
struct node* newNode(int item)
{
struct node* temp = new node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// A utility function to
// do inorder traversal of BST
void inorder(struct node* root)
{
if (root != NULL) {
inorder(root->left);
cout << root->key << " ";
inorder(root->right);
}
}
// reverse tree path using queue
void reversePath(struct node** node,
int& key, queue<int>& q1)
{
/* If the tree is empty,
return a new node */
if (node == NULL)
return;
// If the node key equal
// to key then
if ((*node)->key == key)
{
// push current node key
q1.push((*node)->key);
// replace first node
// with last element
(*node)->key = q1.front();
// remove first element
q1.pop();
// return
return;
}
// if key smaller than node key then
else if (key < (*node)->key)
{
// push node key into queue
q1.push((*node)->key);
// recursive call itself
reversePath(&(*node)->left, key, q1);
// replace queue front to node key
(*node)->key = q1.front();
// perform pop in queue
q1.pop();
}
// if key greater than node key then
else if (key > (*node)->key)
{
// push node key into queue
q1.push((*node)->key);
// recursive call itself
reversePath(&(*node)->right, key, q1);
// replace queue front to node key
(*node)->key = q1.front();
// perform pop in queue
q1.pop();
}
// return
return;
}
/* A utility function to insert
a new node with given key in BST */
struct node* insert(struct node* node,
int key)
{
/* If the tree is empty,
return a new node */
if (node == NULL)
return newNode(key);
/* Otherwise, recur down the tree */
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
/* return the (unchanged) node pointer */
return node;
}
// Driver Program to test above functions
int main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node* root = NULL;
queue<int> q1;
// reverse path till k
int k = 80;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
cout << "Before Reverse :" << endl;
// print inorder traversal of the BST
inorder(root);
cout << "\n";
// reverse path till k
reversePath(&root, k, q1);
cout << "After Reverse :" << endl;
// print inorder of reverse path tree
inorder(root);
return 0;
}