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

Click For Summary
SUMMARY

The discussion focuses on modifying a C++ Binary Search Tree (BST) implementation to include a method for checking duplicate values. The existing method, isBST(), correctly verifies the BST properties regarding node values but fails to account for duplicates. The proposed solution involves adjusting the recursive checks to ensure that each node's value is unique, specifically by modifying the right child's comparison to use parent->key + 1 instead of parent->key. This adjustment ensures that duplicate values are not allowed in the tree.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with Binary Search Trees (BST)
  • Knowledge of recursion in programming
  • Basic concepts of template programming in C++
NEXT STEPS
  • Implement a method to track and handle duplicate values in a BST
  • Explore C++ template specialization for more complex data types
  • Learn about tree traversal algorithms and their applications
  • Investigate performance implications of BST operations with duplicates
USEFUL FOR

C++ developers, computer science students, and software engineers working on data structure implementations, particularly those focusing on Binary Search Trees and their properties.

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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 0 ·
Replies
0
Views
2K