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

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

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

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

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

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

  • Problem #1 : write a Month class that contains methods to determine the number of days...

    Problem #1 : write a Month class that contains methods to determine the number of days in a month given the numeric equivalent of the month and year; your program must take into account leap years. To help facilitate a result, the following are sample runs and a driver program import java util.Scanner This is a test driver class for the Nonth class pablic class namDays ic statie void main (3teingt1 args Scanner in new Scanner (System.in) System.out print ("Please...

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • JAVA Write a class called Pen that contains the following information: 1. Private instance variables for...

    JAVA Write a class called Pen that contains the following information: 1. Private instance variables for the price of the pen (float) and color of the pen (String). 2. A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. 3. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {

  • Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter...

    Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter called dataFile (representing the path to a text file) and uses a Set of Strings to eliminate duplicate words from dataFile. The unique words should be stored in an instance variable called uniqueWords. Create an instance method called write that takes a single parameter called outputFile (representing the path to a text file) and writes the words contained in uniqueWords to the file pointed...

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