Question

create your own implementation of a Binary Search Tree using the following data structure:  

public class BinarySearchTreeVertex<E> {   
public E e;
public BinarySearchTreeVertex<K> parent;
public BinarySearchTreeVertex<K> left_child;
public BinarySearchTreeVertex<K> right_child;
}

Create a generic class called BinarySearchTree<E> that maintains a BST. This class contains a reference to the root of the BST and provides the functions add() and find():

public class BinarySearchTree<E>
{
public BinarySearchTreeVertex<E> root = null;
public boolean add(E e) {}
public boolean find(E e) {}
}

Create a BST that can hold Integers. Make a driver program that provides the following menu options:

1. Add Integer(s) to the tree
2. Display tree
3. Find an element
4. Exit

TESTING:

(Output should match all pics)

(Option#1 Test: 1. Add Integer(s) to the tree 2. Display tree 3. Find an element 4. Exit >>1 Enter Integer to add to tree (ty

[] Option #2 Test 1. Add Integer(s) to the tree 2. Display tree 3. Find an element 4. Exit >>2 NODE.E PARENT null NODE. LEFT

[] Option #3 Test 1. Add Integer(s) to the tree 2. Display tree 3. Find an element 4. Exit >>3 What INTEGER are you looking f

Solution Need in JAVA Code. Please do a Test and recheck before comment . Thanks

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

HI,

please find code below.

import java.util.*;

public class Main
{
static BinarySearchTree<Integer> mybst = new BinarySearchTree<Integer>();
// writing this globally to maintain data intact
public static void main(String[] args)
{
int userChoice;
String valueToInsert;
userChoice = menuDetails();
Scanner in = new Scanner(System.in);


if(userChoice == 1) /* to insert values to bst*/
{
while(true)
{
System.out.print("Enter integer to add to tree(type 'stop' to exit");
valueToInsert = in.nextLine();
System.out.print(valueToInsert);
if(valueToInsert.equals("stop"))
{
break;
}
else
{
mybst.add(Integer.parseInt(valueToInsert));
mybst.preOrderTraversal();
}
}
main(null);
}
else if(userChoice == 2)/* to display tree*/
{
System.out.println("displaying tree");
mybst.preOrderTraversal();
System.out.println();
  
main(null);
}
else if(userChoice == 3)/* to search element in tree*/
{
System.out.println("finding element in tree");
Scanner input = new Scanner(System.in);
System.out.println("What integer are you looking for?");
int findElem = input.nextInt();
boolean isFound = mybst.find(findElem);
System.out.println("Is"+ findElem + "In Tree?" + isFound);
}
}
static int menuDetails()
{

int userSeletion;
Scanner input = new Scanner(System.in);

/***************************************************/

System.out.println("Choose from these menu choices");
System.out.println("-------------------------\n");
System.out.println("1 - Add Integer(s) to the tree");
System.out.println("2 - Display tree");
System.out.println("3 - Find An element");
System.out.println("4 - Exit");

userSeletion = input.nextInt();
return userSeletion;
}

}
/*Create a generic class called BinarySearchTree<E> that maintains a BST.
This class contains a reference to the root of the BST and provides the functions add() and find():*/
class BinarySearchTree <E extends Comparable<E>>
{
  

private BinarySearchTreeVertex<E> root = null;
private Comparator<E> indexElement = null;

public BinarySearchTree()
{
root = null;
}
private int matchWith(E x, E y)
{
if(indexElement == null) return x.compareTo(y);
else
return indexElement.compare(x,y);
}
public boolean add(E e)
{
boolean ret = false;
root = add(root, e);
if(root!=null)
ret = true;
  
return ret;
}
private BinarySearchTreeVertex<E> add(BinarySearchTreeVertex<E> pnode, E elemtoInsert)
{
if (pnode == null)
return new BinarySearchTreeVertex<E>(elemtoInsert);

if (matchWith(elemtoInsert, pnode.e) == 0)
   return pnode;

if (matchWith(elemtoInsert, pnode.e) < 0)
pnode.left_child = add(pnode.left_child, elemtoInsert);
else
pnode.right_child = add(pnode.right_child, elemtoInsert);

System.out.println(pnode.e);
System.out.println(pnode.left_child);
System.out.println(pnode.right_child);
return pnode;
}
public boolean find(E e)
{
return find(root, e);
}
private boolean find(BinarySearchTreeVertex<E> p, E e)
{
if (p == null)
return false;
else
if (matchWith(e, p.e) == 0)
   return true;
else
if (matchWith(e, p.e) < 0)
return find(p.left_child, e);
else
return find(p.right_child, e);
}
public void preOrderTraversal()
{
//System.out.print("preOrderTraversal");
preOrderexecuter(root);
}
private void preOrderexecuter(BinarySearchTreeVertex r)
{
//System.out.print("preOrderexecuter");
if (r != null)
{
System.out.print(r+" "); // node
preOrderexecuter(r.left_child); // leftchild
preOrderexecuter(r.right_child);// right child
}
}
/*our own implementation of a Binary Search Tree using the given data structure: */
public class BinarySearchTreeVertex<E>
{
public E e;
public BinarySearchTreeVertex<E> left_child, right_child;
public BinarySearchTreeVertex<E> parent;

public BinarySearchTreeVertex(E e,BinarySearchTreeVertex<E> par, BinarySearchTreeVertex<E> l, BinarySearchTreeVertex<E> r)
{
parent = par;
left_child = l;
right_child = r;
this.e = e;
}

public BinarySearchTreeVertex(E data)
{
this(data, null, null,null);
}

public String toString()
{
return e.toString();
}
} //end of BinarySearchTreeVertex
}//end of BinarySearchTree

output attached:

Enter integer to add to tree(type stop to exit5 55 Enter integer to add to tree (type stop to exit7 75 null 5 7 Enter int5 7 9 Enter integer to add to tree (type stop to exitstop stopChoose from these menu choices -- - --- --- --- --- --- - --thanks,

kindly upvote.

Add a comment
Know the answer?
Add Answer to:
create your own implementation of a Binary Search Tree using the following data structure:   public class...
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
  • 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...

  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

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

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation...

    Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...

  • Preliminaries Download the template class and the driver file. Objective Learn how to traverse a binary...

    Preliminaries Download the template class and the driver file. Objective Learn how to traverse a binary search tree in order. Description For the template class BinarySearchTree, fill in the following methods: insert - Inserts values greater than the parent to the right, and values lesser than the parent to the left.parameters elem - The new element to be inserted into the tree. printInOrder - Prints the values stored in the tree in ascending order. Hint: Use a recursive method to...

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

  • BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E>...

    BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E> overallRoot;    public BST() {        overallRoot = null;    }    // ************ ADD ************ //    public void add(E addThis) {        if (overallRoot == null) {            overallRoot = new TreeNode<>(addThis);        } else {            add(overallRoot, addThis);        }    }    private TreeNode<E> add(TreeNode<E> node, E addThis) {        if...

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

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