Question

How do I delete a node in a binary search tree with a java implementation? Specifically...

How do I delete a node in a binary search tree with a java implementation?

Specifically with

public BTNode<Pair<Integer,String>>
delete
(BTNode<Pair<Integer,String>> aNode)
{

}

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

Below is the answer of your problem

You can use this function in your Code. I hope this fuction can solve your problem.

public BTNode(BTNode root, Integer data) {

if(root == null) return root;

if(data < root.getData()) {
root.Left(deleteNode(root.Left(), data));
} else if(data > root.Data()) {
root.Right(deleteNode(root.Right(), data));
} else {

if(root.Left() == null && root.Right() == null) {
System.out.println("deleting "+data);
return null;
} else if(root.Left() == null) {

System.out.println("deleting "+data);
return root.Right();
} else if(root.Right() == null) {

System.out.println("deleting "+data);
return root.Left();
} else {

Integer minValue = minValue(root.Right());
root.Data(minValue);
root.Right(deleteNode(root.Right(), minValue));
System.out.println("deleting "+data);
}
}

return root;
}

If you have any query please comment below

Please give a thumbs up.

Add a comment
Know the answer?
Add Answer to:
How do I delete a node in a binary search tree with a java implementation? Specifically...
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...

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

  • Need them in c++ 1.       Give the code for the definition  of a node for the  linked  implementation  of a  tree  that  contains...

    Need them in c++ 1.       Give the code for the definition  of a node for the  linked  implementation  of a  tree  that  contains integer data. 2.       In the array implementation of a tree in C, if the data for anode in in  index i,  what  are the indices  of  its  two  children? 3.        Draw the binary search treethat resultsfor insertingthe following numbers, in the order given, into aninitially empty tree:   64,  14,  23,  84,  67,  2,  8,  73,  89,  47. 4.       Describe, in detail, the algorithm for deleting a node from a binary search tree.   (make sure you have differentcases fornodes with 0,  1,  or  2  children.

  • This is binary search tree problem. The program reads the text file, and creates a binary...

    This is binary search tree problem. The program reads the text file, and creates a binary search tree based on the words in the file. I can create the tree but I also have to store 'the order of insertion' in each node.   For example, if text includes "the" 3 times and it is the 1st, 5th, and 9th word in the file, in binary search tree, one node should have string value "the" and array list{1, 5, 9}. How...

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

  • The linked implementation is used in the coding of a Binary Search Tree structure. Calculate the...

    The linked implementation is used in the coding of a Binary Search Tree structure. Calculate the structure's density assuming that it contains 200 nodes and: a.  Each node contains 10 bytes of information. b.  Each node contains 300 bytes of information. Then, repeat the above exercise for the array implementation of the Binary Search Tree, assuming it is: a.  Left balanced. b.  Skewed to the right.

  • Given a binary search tree and a value k, implement a function to find the node...

    Given a binary search tree and a value k, implement a function to find the node in the binary search tree whose value is closest to k. Write the program in Java Syntax: int lookup(Node node)

  • Consider the __init__ constructor method for the Python implementation of a node within a binary tree....

    Consider the __init__ constructor method for the Python implementation of a node within a binary tree. The binary tree will aim to require minimal space by having only one tree node per search key. How would this constructor method differ between a Set and a Map? How might this constructor method differ between a Map and a Multi-map? How might this constructor method differ between a Set and a Multi-Set?

  • a. How can I show that any node of a binary search tree of n nodes...

    a. How can I show that any node of a binary search tree of n nodes can be made the root in at most n − 1 rotations? b. using a, how can I show that any binary search tree can be balanced with at most O(n log n) rotations (“balanced” here means that the lengths of any two paths from root to leaf differ by at most 1)?

  • Recall that in a binary search tree, at every node, all elements to the left of...

    Recall that in a binary search tree, at every node, all elements to the left of the node have a smaller key, and all elements to the right of a node have a larger key. Write a program called that takes two parameters: a pointer to a binary search tree node, and an int parameter called min which will print all the elements bigger than the specified value, min. Your program should allow the user to enter data (integer) from...

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