Binary Search Tree (C++)
Loading...
Searching...
No Matches
BST.cpp
Go to the documentation of this file.
1
17
18#include "BST.h"
19#include "Stack.h"
20#include "Queue.h"
21#include <iostream>
22
26BST::BST() : root(nullptr) {}
27
32 destroyTree();
33}
34
41void BST::insert(int value) {
42 Node* newNode = new Node(value);
43
44 // Special case: empty tree
45 if (!root) {
46 root = newNode;
47 return;
48 }
49
50 Node* current = root;
51 Node* parent = nullptr;
52
53 // Traverse the tree to find the insertion point
54 while (current) {
55 parent = current;
56
57 if (value < current->getValue())
58 current = current->getLeft();
59 else if (value > current->getValue())
60 current = current->getRight();
61 else {
62 // Duplicate value detected; discard new node
63 delete newNode;
64 return;
65 }
66 }
67
68 // Attach the new node to its parent
69 if (value < parent->getValue())
70 parent->setLeft(newNode);
71 else
72 parent->setRight(newNode);
73}
74
81bool BST::search(int value) const {
82 Node* current = root;
83
84 while (current) {
85 if (value == current->getValue())
86 return true;
87 else if (value < current->getValue())
88 current = current->getLeft();
89 else
90 current = current->getRight();
91 }
92
93 return false;
94}
95
102void BST::inorder() const {
103 if (!root) return;
104
105 Stack s;
106 Node* current = root;
107
108 while (current || !s.isEmpty()) {
109 while (current) {
110 s.push(current);
111 current = current->getLeft();
112 }
113
114 current = s.pop();
115 std::cout << current->getValue() << " ";
116 current = current->getRight();
117 }
118
119 std::cout << std::endl;
120}
121
127void BST::levelOrder() const {
128 if (!root) return;
129
130 Queue q;
131 q.enqueue(root);
132
133 while (!q.isEmpty()) {
134 Node* current = q.dequeue();
135 std::cout << current->getValue() << " ";
136
137 if (current->getLeft())
138 q.enqueue(current->getLeft());
139 if (current->getRight())
140 q.enqueue(current->getRight());
141 }
142
143 std::cout << std::endl;
144}
145
154bool BST::remove(int value) {
155 Node* parent = nullptr;
156 Node* current = root;
157
158 // Locate the node to delete
159 while (current && current->getValue() != value) {
160 parent = current;
161 if (value < current->getValue())
162 current = current->getLeft();
163 else
164 current = current->getRight();
165 }
166
167 if (!current)
168 return false; // Value not found
169
170 // ------------------------------------------------------------
171 // Case 1: Node has no children (leaf)
172 // ------------------------------------------------------------
173 if (!current->getLeft() && !current->getRight()) {
174 if (current == root)
175 root = nullptr;
176 else if (parent->getLeft() == current)
177 parent->setLeft(nullptr);
178 else
179 parent->setRight(nullptr);
180
181 delete current;
182 }
183
184 // ------------------------------------------------------------
185 // Case 2: Node has exactly one child
186 // ------------------------------------------------------------
187 else if (!current->getLeft() || !current->getRight()) {
188 Node* child = current->getLeft()
189 ? current->getLeft()
190 : current->getRight();
191
192 if (current == root)
193 root = child;
194 else if (parent->getLeft() == current)
195 parent->setLeft(child);
196 else
197 parent->setRight(child);
198
199 delete current;
200 }
201
202 // ------------------------------------------------------------
203 // Case 3: Node has two children
204 // ------------------------------------------------------------
205 else {
206 // Find inorder successor (leftmost node in right subtree)
207 Node* succParent = current;
208 Node* successor = current->getRight();
209
210 while (successor->getLeft()) {
211 succParent = successor;
212 successor = successor->getLeft();
213 }
214
215 // Replace current node's value with successor's value
216 current->setValue(successor->getValue());
217
218 // Remove successor node (which has at most one child)
219 Node* child = successor->getRight();
220
221 if (succParent->getLeft() == successor)
222 succParent->setLeft(child);
223 else
224 succParent->setRight(child);
225
226 delete successor;
227 }
228
229 return true;
230}
231
239void BST::destroyTree() {
240 if (!root) return;
241
242 Stack s;
243 s.push(root);
244
245 while (!s.isEmpty()) {
246 Node* current = s.pop();
247
248 if (current->getLeft())
249 s.push(current->getLeft());
250 if (current->getRight())
251 s.push(current->getRight());
252
253 delete current;
254 }
255
256 root = nullptr;
257}
Declaration of the BST (Binary Search Tree) class.
Declaration of the Queue class.
Declaration of the Stack class.
void levelOrder() const
Performs a level-order (breadth-first) traversal of the tree.
Definition BST.cpp:127
void inorder() const
Performs an inorder traversal of the tree.
Definition BST.cpp:102
void insert(int value)
Inserts a value into the BST.
Definition BST.cpp:41
BST()
Constructs an empty BST.
Definition BST.cpp:26
bool remove(int value)
Removes a value from the BST if it exists.
Definition BST.cpp:154
bool search(int value) const
Searches for a value in the BST.
Definition BST.cpp:81
~BST()
Destroys the BST and frees all dynamically allocated nodes.
Definition BST.cpp:31
Represents a node within a binary search tree.
Definition Node.h:35
Node * getRight() const
Returns a pointer to the right child node.
Definition Node.cpp:52
void setLeft(Node *left)
Sets the pointer to the left child node.
Definition Node.cpp:46
void setValue(int value)
Sets the stored integer value.
Definition Node.cpp:32
void setRight(Node *right)
Sets the pointer to the right child node.
Definition Node.cpp:59
Node * getLeft() const
Returns a pointer to the left child node.
Definition Node.cpp:39
int getValue() const
Returns the stored integer value.
Definition Node.cpp:25
Represents an explicit queue used to support non-recursive tree operations.
Definition Queue.h:64
Node * dequeue()
Removes and returns the node at the front of the queue.
Definition Queue.cpp:45
void enqueue(Node *n)
Adds a node to the end of the queue.
Definition Queue.cpp:31
bool isEmpty() const
Checks whether the queue is empty.
Definition Queue.cpp:58
Represents an explicit stack used to support non-recursive tree operations.
Definition Stack.h:65
Node * pop()
Removes and returns the node at the top of the stack.
Definition Stack.cpp:40
bool isEmpty() const
Checks whether the stack is empty.
Definition Stack.cpp:52
void push(Node *n)
Pushes a node onto the stack.
Definition Stack.cpp:31