-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBSTstd.cpp
204 lines (185 loc) · 3.71 KB
/
BSTstd.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
/*-- BSTstd.cpp------------------------------------------------------------
This file implements the BSTstd member functions.
-------------------------------------------------------------------------*/
#include "BSTstd.h"
#include <iostream>
using namespace std;
/* Constructor */
BSTstd::BSTstd()
{
root = NULL;
}
/* Destructor */
BSTstd::~BSTstd()
{
if (root != NULL) TreeNode::deleteTree(root);
}
/* Insert an element into the BST */
void BSTstd::insert(ElementType element)
{
TreeNode* newptr = new TreeNode(element);
if (root == NULL)
{
root = newptr;
}
else
{
TreeNode* p = root;
TreeNode* parent = NULL;
while ((p != NULL) && (p->data != element)) //find the right location for the new node
{
if (element < p->data)
{
parent = p;
p = p->left;
}
else
{
parent = p;
p = p->right;
}
}
if (p == NULL) //if the element is not in the BST
{
if (element < parent->data)
parent->left = newptr;
else
parent->right = newptr;
}
else
cout << "node duplicate!" << endl;
}
}
/* Remove an element from the BST */
void BSTstd::remove(ElementType element)
{
TreeNode* p = root;
TreeNode* parent = NULL;
if (root == NULL) // No nodes in the tree
{
cout << "tree is empty, unable to remove element" << endl;
return;
}
while ((p != NULL) && (p->data != element)) //find the correct location for the new node
{
if (element < p->data)
{
parent = p;
p = p->left;
}
else
{
parent = p;
p = p->right;
}
}
if (p == NULL) // Element not found
{
cout << "Unable to find element. It will not be removed" << endl;
}
else // Element was found, so let's remove it
{
if (p->left == NULL && p->right == NULL) //case 1
{
if (p == root) { // node is root node
root = NULL;
}
else
{
if (element < parent->data)
parent->left = NULL;
else
parent->right = NULL;
}
delete p;
}
else if (p->left != NULL && p->right == NULL) //case 2
{
if (p == root) // node is root nodes
{
root = p->left;
}
else
{
if (element < parent->data)
parent->left = p->left;
else
parent->right = p->left;
}
delete p;
}
else if (p->left == NULL && p->right != NULL) //case 2
{
if (p == root) // node is root node
{
root = p->right;
}
else
{
if (element < parent->data)
parent->left = p->right;
else
parent->right = p->right;
}
delete p;
}
else //case 3 - neither child is NULL
{
int min_of_right_child = findmin(p->right);
remove(min_of_right_child);
p->data = min_of_right_child;
}
}
}
/* Find the minimum value recursively starting from a specific node */
int BSTstd::findmin(TreeNode* p)
{
while (p->left != NULL)
p = p->left;
return p->data;
}
/* Find the minimum value for the entire tree */
int BSTstd::findmin()
{
return findmin(root);
}
/* Find the maximum value recursivly starting from a specific node */
int BSTstd::findmax(TreeNode* p)
{
while (p->right != NULL)
p = p->right;
return p->data;
}
/* Find the minimum value for the entire tree */
int BSTstd::findmax()
{
return findmax(root);
}
/* Traverse the tree recursively from a node using inorder */
void BSTstd::inorder(TreeNode* p)
{
if (p != NULL)
{
inorder(p->left);
cout << p->data << " ";
inorder(p->right);
}
}
/* Traverse the tree inorder */
void BSTstd::inorder()
{
inorder(root);
}
/* Display output of various traversal methods */
void BSTstd::traversal()
{
cout << " Min value: " << findmin() << endl;
cout << " Max value: " << findmax() << endl;
cout << " Inorder: ";
inorder();
cout << endl;
cout << endl;
}
/* Traverse the tree preorder */
/* Traverse the tree posorder */
/* Search for an element in the binary search tree */