Comp Sci How Can I Modify a C++ BST Implementation to Check for Duplicate Values?

AI Thread Summary
The discussion revolves around modifying a C++ Binary Search Tree (BST) implementation to include a method that checks for duplicate values, in addition to verifying the BST properties. The user has successfully implemented checks for the left child being smaller and the right child being larger than the parent, but struggles with ensuring no duplicate values exist. The current method checks the tree's minimum and maximum values but fails when duplicates are present. The user mentions they might have found an alternative solution to check for duplicates. Overall, the focus is on enhancing the BST validation logic to include duplicate detection.
FMAfan
Messages
2
Reaction score
0

Homework Statement


I am working on an assignment in which I have to add a method to my BST class which checks if a binary tree object is in fact a Binary Search tree. So
(1) A left child must be smaller than the parent
(2) A right child must be larger than the parent
and:
(3) There cannot be any repeat data values

I think my code works for the first two conditions. What it does is check the root first against its maximum (right-most leaf) and minimum (left-most leaf) values. If that's true, then the method will call itself, checking that each subtree's parent is less than the right max value and and greater than the left min value.

However, this does not work correctly if a data value of the binary tree has been repeated.
I am completely stumped on how to also check for this case.

Homework Equations



bool isBST() --> sets initial min and max values to start check and calls second isBST method
bool isBST(Node<t>* r, int max, int min) --> checks to see if the < > rules have been followed

The Attempt at a Solution


Code:
//******************************************************************
// Function name: isBST
//*******************************************************************
template <class T>
bool BST<T> :: isBST() {
	Node<T>* seekerL = root,
		   * seekerR = root;
	T minimum,
	  maximum;

	if(!seekerL) { // if the tree is empty return true
		return true;
	} else { // find the left and right min and max values of the tree
		while(seekerL) {
			minimum = seekerL->key;
			seekerL = seekerL->left;
		}

		while(seekerR) {
			maximum = seekerR->key;
			seekerR = seekerR->right;
		}
	}

	// check that the tree and its subtrees follow the BST rules
	return isBST(root, minimum , maximum);
} // end isBST

//******************************************************************
// Function name: isBST
//*******************************************************************
template <class T>
bool BST<T> :: isBST(Node<T>* r, T min, T max) {
	Node<T> *parent = r;

	// if the parent has no children a leaf node has been reached
	if(!parent) {
		return true;
	}

	// if parent does not fall inbetween max or min return false
	if (parent->key < min || parent->key > max) {
		return false;
	}

	// else keep checking each subtree until rule is broken or tree is BST
	return (isBST(parent->left, min, parent->key) && isBST(parent->right, parent->key+1, max)); 
} // end isBST

Any help would be greatly appreciated. :)

Edit: Oh and the test data/key I am using is a Binary Tree of integers.
 
Last edited:
Physics news on Phys.org
Never mind. I think I may have figured out another way to check my tree.
 
Back
Top