1 Write a client method that returns a count of the number o
1) Write a client method that returns a count of the number of nodes in a binary search tree that contains a value less than or equal to the argument value. The signature of the method is
int countLess (BinarySearchTree<Golfer> tree, Golfer maxValue)
2) Write a client method that returns a reference to the information in the node with the “smallest” value in a binary search tree. The signature of the method is
Golfer min(BinarySearchTree<Golfer> tree)
3) Write a client method that returns a reference to the information in the node with the “largest” value in a binary search tree. The signature of the method is
Golfer max(BinarySearchTree<Golfer> tree)
(The question is from the OBJECTED ORIENTATED DATA STRUCTURE using JAVA, 3rd Edition. Can you write the code using JAVA language, please? Thank you)
Solution
1) Write a client method that returns a count of the number of nodes in a binary search tree that contains a value less than or equal
to the argument value. The signature of the method is
int countLess (BinarySearchTree<Golfer> tree, Golfer maxValue)
int countLess (BinarySearchTree<Golfer> tree, Golfer maxValue)
{
int tree_size,i;
int number_of_nodes = 0;
tree_size = tree.reset(BinarySearchTree.INORDER);
for (i = 1; i <= tree_size; i++)
{
if ((tree.getNext(BinarySearchTree.INORDER).compareTo(maxValue))<= 0)
number_of_nodes = number_of_nodes + 1;
}
return number_of_nodes;
}
2) Write a client method that returns a reference to the information in the node with the “smallest” value in a binary search tree.
The signature of the method is
Golfer min(BinarySearchTree<Golfer> tree)
public Golfer min(BinarySearchTree<Golfer> tree)
{
BSTNode<Golfer> i = tree.root;
if (i == NULL)
return -1;
while (i.getLeft() != NULL)
i = i.getLeft();
return i.getInfo();
}
3) Write a client method that returns a reference to the information in the node with the “largest” value in a binary search tree.
The signature of the method is
Golfer max(BinarySearchTree<Golfer> tree)
public Golfer max(BinarySearchTree<Golfer> tree)
{
BSTNode<Golfer> i = tree.root;
if (i == NULL)
return -1;
while (i.getRight()!= NULL)
i = i.getRight();
return i.getInfo();
}

