51 Node* parent =
nullptr;
57 if (value < current->getValue())
59 else if (value > current->
getValue())
69 if (value < parent->getValue())
87 else if (value < current->getValue())
106 Node* current = root;
108 while (current || !s.
isEmpty()) {
115 std::cout << current->
getValue() <<
" ";
119 std::cout << std::endl;
135 std::cout << current->
getValue() <<
" ";
143 std::cout << std::endl;
155 Node* parent =
nullptr;
156 Node* current = root;
159 while (current && current->
getValue() != value) {
161 if (value < current->getValue())
176 else if (parent->
getLeft() == current)
194 else if (parent->
getLeft() == current)
207 Node* succParent = current;
211 succParent = successor;
212 successor = successor->
getLeft();
221 if (succParent->
getLeft() == successor)
239void BST::destroyTree() {
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.
void inorder() const
Performs an inorder traversal of the tree.
void insert(int value)
Inserts a value into the BST.
BST()
Constructs an empty BST.
bool remove(int value)
Removes a value from the BST if it exists.
bool search(int value) const
Searches for a value in the BST.
~BST()
Destroys the BST and frees all dynamically allocated nodes.
Represents a node within a binary search tree.
Node * getRight() const
Returns a pointer to the right child node.
void setLeft(Node *left)
Sets the pointer to the left child node.
void setValue(int value)
Sets the stored integer value.
void setRight(Node *right)
Sets the pointer to the right child node.
Node * getLeft() const
Returns a pointer to the left child node.
int getValue() const
Returns the stored integer value.
Represents an explicit queue used to support non-recursive tree operations.
Node * dequeue()
Removes and returns the node at the front of the queue.
void enqueue(Node *n)
Adds a node to the end of the queue.
bool isEmpty() const
Checks whether the queue is empty.
Represents an explicit stack used to support non-recursive tree operations.
Node * pop()
Removes and returns the node at the top of the stack.
bool isEmpty() const
Checks whether the stack is empty.
void push(Node *n)
Pushes a node onto the stack.