Binary Search tree - insertion - java

You are given a pointer to the root of a binary search tree and a value to be inserted into the tree. Insert this value into its appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function.
I have given my code but one test case is not working. Here is my code:
static Node Insert(Node root,int value){
Node d =root;
Node q = new Node();
q.left = null;
q.right=null;
q.data = value;
while(true){
if(value<d.data){
if(d.left==null){d.left = q;
return root; }
else{
d= d.left ;
}
}
else{
if(value>d.data){
if(d.right==null){d.right=q;
return root;}
else d = d.right;
}
}
}
}

You're missing two cases: value == d.data and the empty tree.
value == d.data: In that case the tree won't be altered in any way, but your code won't break-off and end up in an infinite loop.
Simplest solution would be to insert these lines in the while-loop:
if(value == d.data)
return root;
The case where the tree is empty is rather implementation-specific, so it's difficult to suggest any solution for this case.

Related

Mirror Binary Search Tree using Recursion

Looking for some help while practicing Binary Search Trees. I am unable to figure out where I am going wrong in my code, as it seems to follow the generic recursion format. Possibly something related to either the temp Node or the return statement? Any help would be appreciated thanks.
public static Node < Integer > mirror(Node < Integer > root) {
if (root == null)
return null;
else {
mirror(root.left);
mirror(root.right);
Node temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}
First, it seems you do not allocate memory for the nodes that will belong to the mirrored tree. Note that the instruction
Node temp;
does not allocate in the heap, but in the stack. When the function returns, this memory is freed and whatever you have stored there won't make sense.
Second, it seems you do not have very clear the traversal pattern and I think that because of the line
root.right = temp;
poses an kind of asymmetry.
This is a way (in C++ pseudo-code), among others, for getting a mirrored binary tree that uses a preorder pattern:
Node * mirror(Node * root)
{
if root == NULL
return NULL;
Node * mirror_root = new Node;
mirror_root->left = mirror(root->right); // here is where the mirroring happens
mirror_root->right = mirror(root->left);
return mirror_root;
}
You could try to do the same thing allocating the nodes in postorder or inorder.

binary tree insertion using recursion (clarification)

I have the following code to insert into the binary tree:
public void insert(T item) {
root = insert(item, root);
}
private Node insert(T item, Node node) {
if(node == null){
return new Node(item, null, null);
} else {
if(item.compareTo(node.item) > 0) {
node.rightChild = insert(item, node.rightChild);
} else {
node.leftChild = insert(item, node.leftChild);
}
}
return node;
}
the code works fine, I have tested it
my question is, how come the root is never changed since in the public function I assigned the returned node from the private function to the root
Thank you!
The public insert is just an interface into the recursive method which rebuilds the tree as the stack unwinds ending where you started, at the root (root = ...). Except for the first insert, you go left or right until you insert at the leaf level. Without any balancing, you will have the same root (assuming no removals) for the lifetime of the tree. Therefore, the only time the root changes on insert is when it's empty.
Note: There's also a matter of what happens when a node is inserted with an existing value; do you discard it, allow duplicates, or swap the objects? That's an implementation detail.
I read your code again and your code is correct. The return value is always the root element because your frist function call is insert(item, root) and the return value is what you give.

Delete from Binary Search Tree in Java

I can't find what is wrong with my deletion algorithm. When I run my delete method on the root of the BST, it will replace the root with the value of the minimum of the right subtree, but it is not deleting the node thereafter.
public void delete(BinaryTreeNode node, int x){
if (node==null)
return;
else if (x<node.getKey())
delete(node.getLeftChild(),x);
else if (x>node.getKey())
delete(node.getRightChild(),x);
else{
if (node.getLeftChild()==null)
node = node.getRightChild();
else if (node.getRightChild()==null)
node = node.getLeftChild();
else{
BinaryTreeNode temp = findMin(node.getRightChild());
System.out.println(temp.getKey() + " " + node.getKey());
node.setKey(temp.getKey());
System.out.println(temp.getKey() + " " + node.getKey());
delete(node.getRightChild(), node.getKey());
}
}
}
and my findMin() method:
public BinaryTreeNode findMin(BinaryTreeNode node){
if (node.getLeftChild()==null)
return node;
else
return findMin(node.getLeftChild());
}
For a BST containing all integers 1-9 with a root of 4, my output for inorderPrint() yields
1,2,3,5,5,6,7,8,9
instead of
1,2,3,5,6,7,8,9
Update your deleted-node's parent's pointer with the correct node. You need to get rid of any traces of the deleted node. Have a temporary node to keep track of the parent node, so this is easier to do once you find the node to delete.
When you are setting node = node.getLeftChild(); (or symetrically node = node.getRightChild(); you are changing the value of the local variable node, and not the reference to this node from the father.
You are missing something like:node.father.left = ....(or node.father.right = ...).
You should change the values in the tree itself, and not only the references you hold in the method.

Remove the minimum value in a binary search tree

I understand the algorithms but I am not sure how to put it into actual codes. Please help! And also please explain in details. I really want to understand this besides just copying down the answer. ;)
Here are my codes:
public boolean getLeftChild(){
Node insertNode = root;
while(insertNode!=null){
insertNode = insertNode.left;
}
return true;
}
public Boolean removeMin(){
Node insertNode = root;
Node parentNode =root;
if (insertNode.left ==null){
insertNode.right = parentNode;
insertNode = null;
}else if (getLeftChild() ==true && insertNode.right != null){
insertNode.left = null;
}else{
parentNode.left = insertNode.right;
}
return true;
}
First things first: For trees I highly recommend recursion.
Just one example:
getSmallestNode(Node node){
if(node.left != null){
return getSmallestNode(node.left)
}
return node;
}
For the deletion, there can be two cases if you want do delete the smallest (and therefore the "most left leaf" child) of a binary tree.
Case 1: The leaf has no child nodes, in that case just set the according entry in the parent to null (mostLeftChild.getParent().left = null)
Case 2: The leaf has a right child node (there can't be a left child node because that means there would be a smaller node and your currently selected node isn't the smallest) in that case you replace the current left node with the smallest node of the right subtree mostLeftChild.getParent().left = getSmallestFromSubtree(mostLeftChild.right)
So now to make that into code, it could look something like this (No guarantee that it really works)
public Node deleteSmallest(Node node){
// haven't reached leaf yet
if(node.left != null{
return deleteSmallest(node.left)
}
// case 1, no child nodes
if(node.right == null){
node.getParent().left = null;
} else { // case 2, right child node
node.getParent().left = deleteSmallest(node.right)
}
return node;
}
And you would call it with deleteSmallest(root)

Changing the recursive insertion of the (binary Search tree) to non-recursive?

I am trying to change my recursive insert method of the BST into non-recursive( maybe While loop)
The reason for this changing because I want to see if it is possible.
Here is the code of insertion:
public void insert(String value)
{
//The node is stored in the root
root = insert(value,root);
}
private Character insert(String value,Character current)
{
if(current == null)
{
//Add the root if the tree empty
current = new Character(value);
}
else
//If the value that we want to insert < root value, then keep going to left till
//it's empty then inserted at left end. Done by recursion
if(value.compareTo(current.getElement())<=-1)
{
current.setLeft(insert(value,current.getLeft()));
}
else
//If the value that we want to insert > root value, then keep going to right till
//it's empty then inserted at right end. Done by recursion
if(value.compareTo(current.getElement())>=1)
{
current.setRight(insert(value,current.getRight()));
}
else
{
//Else, the number we want to insert in already in the tree
System.out.println("Duplicate numbers inserted" + current.getElement());
}
//returning the node tree so that we store it in the root
return current;
}
Could I change this code into non recursive ?
Cheers
Yes, but you need to alter the data structure a little bit to make it works.
The node has to know its left child and right child.
The algorithm looks like this:
current = root;
while(current != null){
if(value.compareTo(current.getElement())<=-1)
{
current = current.left_child;
}else if(value.compareTo(current.getElement())>=1){
current = current.right_child;
}else{
// Duplication
}
}
Actually there are some good examples before, you may want to check those out first:
Write a non-recursive traversal of a Binary Search Tree using constant space and O(n) run time
Nonrecursive/Iterative Binary Search Tree in C (Homework)
Yes, you could define your insert function non-recursively.
However, to do this, your insert function will have to define in-order traversal iterator for BST, which is recursively defined.
I believe there is a way to define in-order traversal non-recursively, but depending on implementation this can be very inefficient.
BST itself is basically recursively defined, and it is always efficient to define your insert function recursively. (I could write some pseudo-code if you really need it, but I think it is kind of meaningless and I do not know about the implementation detail of your in-order traversal iterator)
Please don't forget to select this as an answer :-)
Insert using while loop
public Node insert(Node root,int n) {
while (true) {
if (root.data>n) {
if (root.left==null) {
root.left= new Node(n);
return (root.left);
}
root=root.left;
}
else if (root.data<n) {
if (root.right == null) {
root.right= new Node(n);
}
}
}
}

Categories

Resources