An abbreviated version of the classes
BinaryTreeNode and
LinkedBinaryTree
of the text is given below.
public class BinaryTreeNode
{
// package visible data members
Object element;
BinaryTreeNode leftChild; // left subtree
BinaryTreeNode rightChild; // right subtree
}
public class LinkedBinaryTree implements BinaryTree
{
// instance data member
BinaryTreeNode root; // root node
}
You are to write a public method elementAtLevel(int theLevel)
which returns
null if the binary tree has no element at level
theLevel; otherwise, it returns an
element at this level.
- (a)
-
[16]
Write Java code for the public method
elementAtLevel.
You may define and implement additional
methods as needed.
You may not create or delete any nodes or invoke any methods
for which you have not provided code.
(Hint: use recursion.)
- (b)
-
[2] What is the time complexity of your code as a function
of the number of nodes in the binary tree?