-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
boundary_traversal_bst.cpp
156 lines (131 loc) · 3.36 KB
/
boundary_traversal_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
/*
Program to print the boundary traversal of a Binary Search Tree.
The given approach uses iterative method to print the boundary nodes of
the given Binary Search Tree.
*/
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node * left;
struct Node * right;
};
Node* root = NULL;
// Utility function to create a new Tree Node
Node* newNode(int val)
{
Node *temp = new Node;
temp->data = val;
temp->left = NULL;
temp->right = NULL;
return temp;
}
// Function to insert item in a BST
void insertItem(int item)
{
Node *newnode = newNode(item);
if(!root) {
root = newnode;
return;
}
Node *cur, *prev;
cur = root;
while(cur!= NULL) {
prev = cur;
if(cur->data > item)
cur = cur->left;
else
cur = cur->right;
}
if(prev->data > item)
prev->left = newnode;
else
prev->right = newnode;
}
// Function to add leaf nodes of the Binary Search Tree
void addLeafNodes(Node* root, vector<int>& boundary) {
if (root->left == NULL && root->right == NULL) {
boundary.push_back(root->data);
return;
}
if(root->left) addLeafNodes(root->left, boundary);
if(root->right) addLeafNodes(root->right, boundary);
}
// Function to print to boundary of a BST
vector<int> boundaryTraversal() {
if(root == NULL)
return {};
vector<int> boundary;
if(root->left != NULL && root->right != NULL)
boundary.push_back(root->data);
Node *curr = root->left;
while(curr != NULL) {
if(curr->left != NULL && curr->right != NULL)
boundary.push_back(curr->data);
if(curr->left)
curr = curr->left;
else
curr = curr->right;
}
addLeafNodes(root, boundary);
curr = root->right;
vector<int> rightBoundary;
while(curr != NULL) {
if(curr->left != NULL && curr->right != NULL)
rightBoundary.push_back(curr->data);
if(curr->right)
curr = curr->right;
else
curr = curr->left;
}
for(int i = rightBoundary.size() - 1; i >= 0; i--)
boundary.push_back(rightBoundary[i]);
return boundary;
}
// Printing Boundary Traversal
void printBoundary() {
vector<int> boundary = boundaryTraversal();
for (int i = 0; i < boundary.size(); ++i)
{
cout << boundary[i] << " ";
}
}
int main() {
cout << "Enter the number of nodes you want to enter in the tree: ";
int n, item;
cin >> n;
cout << "Start entering the nodes separated by space: ";
for (int i = 0; i < n; ++i)
{
cin >> item;
insertItem(item);
}
cout << "\nBoundary Traversal is: ";
printBoundary();
}
/*
Sample Input/Output:
Input:
The first line contains a single integer n - number of nodes in the tree
The next line contains n spaced integers
Output:
Contains spaced integers representing the boundary of the tree
Sample Input:
Enter the number of nodes you want to enter in the tree: 8
Start entering the nodes separated by space:
20 8 4 12 10 14 25
Sample Output:
Boundary Traversal is: 20 8 4 10 14 25
Explanation: Tree is
20
/ \
8 25
/ \
4 10
\
14
Complexity Analysis:
Time Complexity: O(n);
Space Complexity: O(n);
*/