Question
1. Consider the following code segment from the implementation of the binary search tree: import ch05.queues.*; import ch03.stacks.*; importsupport.BSTNode; public class BinarySearchTree implementsBSTInterface { protectedBSTNode
1. Consider the following code segment from the implementation of the binary search tree:
import ch05.queues.*;
import ch03.stacks.*;
importsupport.BSTNode;
public class BinarySearchTree
implementsBSTInterface
{
protectedBSTNode
boolean found; // used by remove
// for traversals
protectedLinkedUnbndQueue
protectedLinkedUnbndQueue
protectedLinkedUnbndQueue
publicBinarySearchTree()
// Creates an empty BST object.
{
root = null;
}
publicbooleanisEmpty()
// Returns true if this BST is empty; otherwise, returns false.
{
return (root == null);
}
privateintrecSize(BSTNode
// Returns the number of elements in tree.
{
if (tree == null)
return 0;
else
returnrecSize(tree.getLeft()) + recSize(tree.getRight()) + 1;
}
publicint size()
// Returns the number of elements in this BST.
{
returnrecSize(root);
}}//End of code segment
Add a method to the class to calculate number of leafs in a tree.
2. To implement a simple directed graph, write two Java classes: nodes and edges. Nodes must have an ID number (integer) a name (String) and a simple constructor. Edges must have an ID number (integer), label (String), a source node (reference to a node) and a target node (reference to node).
3. Consider the Array representation of a binary search tree. Write a java method that receives the index of a node and returns the index of the parent.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started