https://ide.geeksforgeeks.org/EREtkq9sSe--- the output is not correct for the given sample input. What’s the error in my code
Delete nodes from bst problem
Hey @AbhishekAhlawat1102
you are doing wrong
Arrays.sort(arr); // here root Node is change
Add data manually in tree
yes i m understanding what you are saying but i not able to come up with a method to build a bst using unsortted array without sorting the array before hand , can u suggest one?
import java.util.*;
public class DeletenodesfromBST {
private class Node {
int data;
Node left;
Node right;
}
private Node root;
public DeletenodesfromBST(int arr[]) {
// TODO Auto-generated constructor stub
// this.root.data = arr[0];
for (int i = 0; i < arr.length; i++) {
this.root = Create_Node(root, arr[i]);
}
}
private Node Create_Node(Node node, int item) {
if (node == null) {
Node nn = new Node();
nn.data = item;
return nn;
}
if (node.data >= item) {
node.left = Create_Node(node.left, item);
} else {
node.right = Create_Node(node.right, item);
}
return node;
}
public static void main(String[] args) {
int ans[] = new int[q];
DeletenodesfromBST db = new DeletenodesfromBST(arr);
}
}
Build Method of Bst
Take input element of array from Scenner
https://ide.geeksforgeeks.org/wmGKk5LDAP--- i have used the method to take input but still the output is not correct, can u tell what’s the error now in the code–https://ide.geeksforgeeks.org/wmGKk5LDAP
@AbhishekAhlawat1102
You are doing wrong in Bulid Tree
if (node.data >= item) {
node.left = Create_Node(node.left, item);
}
debug your remove fun fro this input
1
7
5 3 2 4 7 6 8
1
5
https://ide.geeksforgeeks.org/detlcQklsY---output is correct for sample input and one test case is passing but the order is not correct what m i doing wrong now? Also for the test case i provided output is: 4 3 2 7 6 8
In case of node with 2 children, consider its inorder successor as its replacement.
Consider this case