In this problem, you will write some Java code for simple operations on binary s
ID: 3844863 • Letter: I
Question
In this problem, you will write some Java code for simple operations on binary search trees where keys are integers. Assume you already have the following code and assume that the method bodies, even though not shown, are correct and implement the operations as we defined them.
public class BinarySearchTreeNode{
public int key;
public BinarySearchTreeNode left;
public BinarySearchTreeNode right;
}
public class BinarySearchTree{
private BinarySearchTreeNode root;
public void insert(int key) {... }
public void delete(int key) { ... }
public SomeType find(int key) { ... }
}
c) Add a method public void printTree() to the BinarySearchTree class that iterates over the nodes to print then in increasing order. So the tree...
4
/
2 5
/
1 3
Produces the output "1 2 3 4 5".
Note: You will need an assistant/helper method.
d) Add method public void print Postorder() to the BinarySearchTree class that prints out the nodes of the tree according to a "postorder" traversal. So the tree...
4
/
2 5
/
1 3
Produces the output "1 3 2 5 4".
Note: You will need an assistant/helper method.
e) You have a binary search tree. Consider a leave /. B is the set of keys in the path p of / including the leave / and the root of the tree. A is the set of keys to the left of the path p. C is the set of keys to the right of the path p. Is the following statement true or false? Given any element a in A; b in B; c in C; a ? b ? c.
Explanation / Answer
As per chegg policy solving first 2 parts. Please post 3rd part saperatly. Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
c)
public void printTree() {
printTree(root);
}
public void printTree(BinarySearchTreeNode node) {
if (node != null) {
printTree(node.left);
System.out.println(node.key + " ");
printTree(node.right);
}
}
d)
// Prints a post-order traversal of this tree.
public void printPostorder() {
printPostorder(overallRoot);
}
// Prints a post-order traversal of the tree starting at the specified root
private void printPostorder(BinarySearchTreeNode node) {
if(node != null) {
printPostorder(node.left);
printPostorder(node.right);
System.out.print(node.key + " ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.