Question

25. We need a public method for our Binary Search Tree ADT that returns a refer- ence to the information in the node with the smallest value in the tree. The signature of the method is public T min a. Design an iterative version of the method b. Design a recursive version of the method. c. Which approach is better? Explain

This is for java programming.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public T min() {

    return recMin(root).getInfo();

}

public BSTnode<T> recMin(BSTnode<T> tree) {// Recursive method

    if (tree == null) {

        throw new NoSuchElementException();

    }

    if (tree.left == null) {

        return tree;

    }

    return recMin(tree.left);

}

public T IterationMin(BSTnode<T> tree) {// iterative method

    if (tree == null) {

        throw new NoSuchElementException();

    }

    while (tree.left != null) {

        tree = tree.left;

    }

    return tree.getInfo();

}

It really depends upon the problem which one is better between recursive and iteration, In general recursive solutions tend to be easier to understand, but slower since they consume more and more stack space proportional to the depth of recursion. So it is generally better to use iteration.

Add a comment
Know the answer?
Add Answer to:
This is for java programming. We need a public method for our Binary Search Tree ADT...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • answer in java Given a Binary Search Tree containing integer values, write a method to print...

    answer in java Given a Binary Search Tree containing integer values, write a method to print the values in descending order. public class Treenode { int val; Treenode left; Treenode right; Treenode (int x) { val = x; } The method signature is: public void reverseSorted(Treenode root){ // your code

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • write the method that return the kth smallest item in the binary search tree. for example...

    write the method that return the kth smallest item in the binary search tree. for example if the tree has 18 node and the user enter 13 then the method should return the 13 smallest element in the tree. I has a method that done it recursively but I want to Implement findKth nonrecursively public String findKth( String k ) { return findKth( k, root).data; } public Node findKth(int k, Node t ) {             if( t...

  • SHOW HOW WE VISUALIZE THE BINARY SEARCH TREE WITH ROOT REFERENCED BY ROOT A. AFTER EACH...

    SHOW HOW WE VISUALIZE THE BINARY SEARCH TREE WITH ROOT REFERENCED BY ROOT A. AFTER EACH OF THE FOLLOWING CHANGES, ALSO LIST THE SEQUENCE OF BINARY SEARCH TREE METHOD CALLS, BOTH PUBLIC AND PRIVATE, THAT WOULD BE MADE WHEN EXECUTING THE CHANGE. USE THE ORIGINAL TREE TO ANSWER EACH PART OF THIS QUESTION. A) ADD NODE Q. B) REMOVE NODE R. DO THE QUESTION ABOVE WITH REFERENCE TO ROOT A GIVEN ABOVE. AND ALSO LIST THE SEQUENCE OF BINARY SEARCHTREE...

  • *****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search...

    *****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search trees. We have provided with you a standard implementation of a generic BST in BinarySearchTree.java. Note that this class is an abstract class, which means that some of its methods are not implemented. In previous assignments, you have implemented interfaces which specified methods that you needed to write. Very similarly, an abstract class is a class with some unimplemented methods (it can be thought...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • Public IntBTNode find(int item) This function will attempt to find the item in the binary search ...

    write java function: public IntBTNode find(int item) This function will attempt to find the item in the binary search tree. It will return a reference to the node that contains Item if the item is stored in the tree. If the item is not in the tree, the function will return nu public IntBTNode find(int item) This function will attempt to find the item in the binary search tree. It will return a reference to the node that contains Item...

  • A binary tree is constructed of nodes that are instances of the following class: public class...

    A binary tree is constructed of nodes that are instances of the following class: public class Node public int val public Node left public Node right) Consider the following method public static Node mystery Node root) rootghtanul return root else return mystery root ) You consult Professor Kennedy and hegves an opinion about what the method does when passed a reference to the root node of a binary tree. Assuming he is correct what does the mystery function do? it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT