Question

java: i am having a hard time understanding this concept and if so please follow the...

java: i am having a hard time understanding this concept and if so please follow the below guidelines and explain what you do for me thanks

draw a binary search tree (BST) using these values : 4, 6, 11, 22, 53, 94, 21, 53

then select one of the leaves of the bst you chose and show how the below contains algorithm goes through its execution on the bst to find the node you have selected.

the contains algorithm:

private boolean contains (AnyType x, binaryNode<AnyType> t){

if (t == null) return false;

in compareResult = x.compareTo (t.element);

if (compareResult < 0){

return contains (x, t.left); }

else if (compareResult > 0){

return contains (x, t.right); }

else {

return true; //math }

}

and then alongside on your bst drawing, trace the path contains method.

thanks!

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

Let us understand the question:

  • we are given a set of values 4, 6, 11, 22, 53, 94, 21, 53
  • we need to construct a binary search tree from above values
  • after constructing the binary search tree, we select one of the leaf nodes of the tree
  • we are given an algorithm contain, that takes input a reference to node and root of the BST
  • we invoke algorithm giving selected leaf node and root of the tree as input
  • we have to explain how the algorithm proceeds for these values on given tree
  • after that, we need to trace the path of the contain algorithm

Now, let us do everything step by step.

here is the binary search tree:

Now, let us select 21.

Pass the root of tree and reference to node 21 to the contain algorithm, t= root of tree, x=reference to node 21.

Algorithm runs on tree as follows:

  • check if t==null, evaluates to false
  • compare value at x to 4, compareResult wil be more than zero
  • else if part executes and algorithm is invoked on (21, right subtree of 4)
  • steps 1 to 3 repeats and algorithm is invoked on (21, right subtree of 6)
  • steps 1 to 3 repeats and algorithm is invoked on (21, right subtree of 11)
  • steps 1 to 3 repeats, but this time value of compareResult is greater than zero, so, algorithm is invoked on (21, left subtree of 22)
  • this time, value of compareResult is zero, so, else pat executes and it returns zero

Path of the algorithm is traces in red color:

Add a comment
Know the answer?
Add Answer to:
java: i am having a hard time understanding this concept and if so please follow the...
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 C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write...

    In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...

  • 3. [5 marks] Suppose T is a binary tree that stores an integer key value in...

    3. [5 marks] Suppose T is a binary tree that stores an integer key value in each node. Assume the following notation/operations on a binary tree. » the key T.key is the root node's integer key value . the left child T.left is T's left subtree, which is possibly an empty tree (or null) the right child T.right is T's right subtree, which is possibly an empty tree (or null) (a) Write an efficient algorithm FINDMAxPrODuCT(T) in pseudocode that returns...

  • Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import...

    Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...

  • (Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL)...

    (Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL) binary search tree. Note that you should not be implementing the map-based API described in the plain (unbalanced) BSTree notebook — i.e., nodes in the AVLTree will only contain a single value. class AVLTree: class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right def rotate_right(self): n = self.left self.val, n.val = n.val, self.val self.left, n.left, self.right, n.right...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Exercise 1: BSTree operations For this exercise you'll implement three additional methods in the binary search...

    Exercise 1: BSTree operations For this exercise you'll implement three additional methods in the binary search tree data structure completed in class, so that you have an opportunity to practice both using the recursive pattern covered in class and navigating the binary tree structure. The methods you'll implement are: count_less_than: takes an argument x, and returns the number of elements in the tree with values less than x successor: takes an argument x, and returns the smallest value from the...

  • 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...

  • CAN SOMEONE PLEASE SOLVE THIS? THANK YOU. FINALTREE.JAVA: import java.util.LinkedList; public class FinalTree<E extends Comparable<? super...

    CAN SOMEONE PLEASE SOLVE THIS? THANK YOU. FINALTREE.JAVA: import java.util.LinkedList; public class FinalTree<E extends Comparable<? super E>> { private Node<E> root; private int size; public FinalTree() { root = null; size = 0; } public boolean isEmpty() { return size == 0; } public void add(E newItem) { if (isEmpty()) { root = new Node<E>(newItem); } else { Node<E> parent = null; Node<E> temp = root; int result = 0; while (temp != null) { parent = temp; result =...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

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