Question

in java, Write methods contains and remove for the BinarySearchTree class. Use methods find and delete...

in java, Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do the work.

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

1 public class BinarySearchTreel { /* Class containing left and right child of current node and key value*/ class Node { int

if (key < root.key) root.left deleteRec(root.left, key); else if (key > root.key) root.right = deleteRec(root.right, key); 1

- int minValue (Node root) { int minv = root.key; while (root.left != null) { miny = root.left.key; root = root. left; } retu

void inorder) { inorderRec(root); } // A utility function to do inorder traversal of BST void inorderRec(Node root) { if (roo

tree.insert(80); System.out.println(Inorder traversal of the given tree); tree. inorder(); 151 152 153 154 155 156 157 158

source code:

public class BinarySearchTree1 {
   /* Class containing left and right child of current node and key value*/
class Node
{
int key;
Node left, right;
  
public Node(int item)
{
key = item;
left = right = null;
}
}
  
// Root of BST
Node root;
  
// Constructor
BinarySearchTree1()
{
root = null;
}
  
// This method mainly calls deleteRec()
void deleteKey(int key)
{
root = deleteRec(root, key);
}
  
/* A recursive function to insert a new key in BST */
Node deleteRec(Node root, int key)
{
/* Base Case: If the tree is empty */
if (root == null) return root;
  
/* Otherwise, recur down the tree */
if (key < root.key)
root.left = deleteRec(root.left, key);
else if (key > root.key)
root.right = deleteRec(root.right, key);
  
// if key is same as root's key, then This is the node
// to be deleted
else
{
// node with only one child or no child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
  
// node with two children: Get the inorder successor (smallest
// in the right subtree)
root.key = minValue(root.right);
  
// Delete the inorder successor
root.right = deleteRec(root.right, root.key);
}
  
return root;
}
  
public Node search(Node root, int key)
{
// Base Cases: root is null or key is present at root
if (root==null || root.key==key)
return root;

// val is greater than root's key
if (root.key > key)
return search(root.left, key);

// val is less than root's key
return search(root.right, key);
}
  
int minValue(Node root)
{
int minv = root.key;
while (root.left != null)
{
minv = root.left.key;
root = root.left;
}
return minv;
}
  
// This method mainly calls insertRec()
void insert(int key)
{
root = insertRec(root, key);
}
  
/* A recursive function to insert a new key in BST */
Node insertRec(Node root, int key)
{
  
/* If the tree is empty, return a new node */
if (root == null)
{
root = new Node(key);
return root;
}
  
/* Otherwise, recur down the tree */
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
  
/* return the (unchanged) node pointer */
return root;
}
  
// This method mainly calls InorderRec()
void inorder()
{
inorderRec(root);
}
  
// A utility function to do inorder traversal of BST
void inorderRec(Node root)
{
if (root != null)
{
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}
  
// Driver Program to test above functions
public static void main(String[] args)
{
BinarySearchTree1 tree = new BinarySearchTree1();
  
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
  
System.out.println("Inorder traversal of the given tree");
tree.inorder();
  
System.out.println("\nDelete 20");
tree.deleteKey(20);
System.out.println("Inorder traversal of the modified tree");
tree.inorder();
  
System.out.println("\nDelete 30");
tree.deleteKey(30);
System.out.println("Inorder traversal of the modified tree");
tree.inorder();
  
System.out.println("\nDelete 50");
tree.deleteKey(50);
System.out.println("Inorder traversal of the modified tree");
tree.inorder();
}

}

Add a comment
Know the answer?
Add Answer to:
in java, Write methods contains and remove for the BinarySearchTree class. Use methods find and delete...
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
  • Please use C++,thank you! Write an AddressBook class that manages a collection of Person objects. Use the Person class developed in question 2. An AddressBook will allow a person to add, delete, o...

    Please use C++,thank you! Write an AddressBook class that manages a collection of Person objects. Use the Person class developed in question 2. An AddressBook will allow a person to add, delete, or search for a Perso n object in the address book The add method should add a person object to the address book. The delete method should remove the specified person object from the address book 0 .The search method that searches the address book for a specified...

  • What need to be done What’s given Problem Binary Search Tree Implement the BinarySearchTree class. The...

    What need to be done What’s given Problem Binary Search Tree Implement the BinarySearchTree class. The BinarySearchTree class extends the BinaryTree class. Both can be seen here. Your assignment is to implement all of the abstract methods of the BinaryTree class recursively. They are: . insert iterator (non-recursive) * remove . search You must also implement an Iterator inner class for the BinarySearchTree class. You must submit a modified BinarySearchTree.java file with your source code. Do not submit and do...

  • Please write in Java Recall that the ADT list class methods are;  void List() ...

    Please write in Java Recall that the ADT list class methods are;  void List()  bool isEmpty()  int size()  void add(int item, int pos)//inserts item at specified position (first postion is 1)  void remove(int index)//removes item from specified position  void removeAll()  int indexOf(int item)//returns the index of item  int itemAt(int index)//returns the item in position specified by index Implementation of LIST ADT operations and details are hidden. Only the methods listed above are...

  • * Create a Single Java Class that contains two static methods that solve the specified problems...

    * Create a Single Java Class that contains two static methods that solve the specified problems on slides 3 and 4. Call the static methods with the parameters specified in the Task. ifu Submit as single java file Submit to Blackboard by 8AM on March sth. Assignment is worth 2 pts Cannot be late. . . Remember you should create/use 'helper' methods to solve your problem rather than creating a single lengthy method. However, all'helper' methods must be called from...

  • In Java The following Java implementation of a class Node is given: private class Node<Object> {...

    In Java The following Java implementation of a class Node is given: private class Node<Object> { Node() { this(null, null); } Node(Object d) { this(d, null); } Node(Object d, Node n) { data = d; next = n; } Object data; Node next; } Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a reference to the header node. Using the class Node described above, write a MySingleLinkedList...

  • In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a...

    In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C...

  • Write a JAVA code to do the following: create a class and add methods to count...

    Write a JAVA code to do the following: create a class and add methods to count the number of primitive fields, number of methods with primitive return types, a method that will also return the number of primitive in a given parameter. they are: public int getNumberOfPrimitiveMethods(Class host) public int getNumberOfPrimitiveFields(Class host) public int getNumberOfPrimitiveParameters(Method host) also public int getNumberOfPrivateMethods (Class host) public int getNumberOfPublicMethods (Class host)

  • please write the code in C++ Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as...

    please write the code in C++ Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C & findMax( ) const...

  • write the code in java please Write code for an [add remove contains |rehash getLoad] method...

    write the code in java please Write code for an [add remove contains |rehash getLoad] method in a hash table which uses [separate chaining | open addressing with linear probing open addressing with quadratic probing | open addressing with double hashing where h2(key)-7-(key % 7)].

  • In Java, write a class for a Triangle Object that contains, Private instance variables: - three...

    In Java, write a class for a Triangle Object that contains, Private instance variables: - three doubles: a,b,c, representing the three sides of the Triangle - constructor with three double as args, which are put into three instances variables Public Instance Methods: - getPerimeter: computes the perimeter of the triangle(the sum of the three sides), returns a double - getArea: computes the area of the triangle using Heron's formula: Area = (s*(s-a)*(s-b)*(s-c))^0.5, where s = 0.5*(the triangle's perimeter), returns a...

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