Question: An error in a BST implementation may result in a variety of problems. Ex: Key - related problems include: - A node ( X

An error in a BST implementation may result in a variety of problems. Ex: Key-related problems include:
- A node \( X \) in in the left subtree of ancestor \( Y \) with \( X \)'s key > Y's key
- A node \( X \) in in the right subtree of ancestor \( Y \) with \( X \)'s key \( Y \)'s key
Child-related problems include:
- A node that is a child of two or more distinct nodes
- A child pointer that points to an ancestor
Examples of key-related problems:
Key \(\mathbf{50}\)(and 60) in 40's left subtree
Examples of child-related problems:
Key 66 in 77's right subtree
55's left child is an ancestor
BST validity checker overview
This lab requires implementation of a BST validity checker that identifies each type of problem mentioned above. The validity checker can be used to assist with BST implementations.
Determining how to identify key-related problems is part of this lab's requirements. As for identifying child-related problems, consider a preorder traversal of the two example trees above. A preorder traversal of either encounters the same node more than once. So the two child-related problems can be described with a single criterion: A distinct node is visited more than once during preorder traversal.
- Ex: In the left tree, preorder traversal visits node \(50,25,37,75,88,75\) again, and 88 again.
- Ex: In the right tree, preorder traversal visits node \(44,22,11,33,55\), and then repeats in an infinite cycle.
So an algorithm to check the validity of BST should stop as soon as any node is visited more than once. Ex: The second visit of 75 stops traversal in the left tree and the second visit of 44 stops traversal in the right tree.
Step 1: Inspect the BSTNode.h file
Inspect the class declaration for a BST node in BSTNode.h. Each node has a key, a left child pointer, and a right child pointer. BSTNode.h is read-only, since no changes are required.
Step 2: Implement the BSTChecker::CheckBSTValidity() function
Implement the CheckBSTValidity() function in the BSTChecker class in the BSTChecker.h file. The function takes the tree's root node as an argument. If the tree is a valid BST, nullptr must be returned. Otherwise the first-encountered problematic node must be returned.
Example figures above show the problematic node in red. Implement a preorder traversal that tracks necessary information, like the valid key range for a node's key and a set of visited nodes. As soon as a violation is encountered, like a node's key being out of range or a node being visited a second time, return that node.
Code in main() creates a variety of trees and runs test cases. Ensure that all tests in main() pass before submitting code for grading.
An error in a BST implementation may result in a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!