Adding a Node to a BST recursively using comparable in Java - java

So I've been trying to insert or add a node to a BST recursively and I'm stumped. I keep getting
Exception in thread "main" java.lang.StackOverflowError
which I'm assuming is being caused by the recursion, but I don't exactly know where to go from here and would love some direction if anyone could help provide it. :)
public void add(Type obj) {
TreeNode<Type> newNode = new TreeNode<Type>(obj);
if (root == null) {
root = newNode;
} else {
addNode(root, newNode);
}
}
private void addNode(TreeNode<Type> current, TreeNode<Type> newNode) {
current = root;
if (current == null) {
current = newNode;
} else if (newNode.getValue().compareTo(current.getValue()) < 0) {
if (current.getLeft() == null) {
current.setLeft(newNode);
} else {
addNode(current.getLeft(), newNode);
}
} else if (newNode.getValue().compareTo(current.getValue()) > 0) {
if (current.getRight() == null) {
current.setRight(newNode);
} else {
addNode(current.getRight(), newNode);
}
}
}//end add

private void addNode(TreeNode<Type> current, TreeNode<Type> newNode) {
current = root;
if (current == null) {
current = newNode;
} else if (newNode.getValue().compareTo(current.getValue()) < 0) {
if (current.getLeft() == null) {
current.setLeft(newNode);
} else {
addNode(current.getLeft(), newNode);
}
Look you are again and again setting the current point equal to root. Which causes StackOverFlow, you should not point to root. You can change it like this: You need to remove this line:
current = root
if(root == null){
root = newNode;
return;
}

First, you're assigning current = root; and then on the next line you're checking:
if (current == null)
This condition will always return false (assuming root is not null).
What you're doing inside the "else if"s is good but you should remove the first if part.
Second, you need to handle the case in which root is null, better do it in a separate method that first check if root is null and only if it's not - calls this method (addNode) with root and the inserted node

Related

Generic binary search tree isn't adding new nodes correctly (Java)

I'm writing a binary search tree in java with generic classes, but the nodes aren't adding correctly and I can't figure out why.
Here's my insert method (iterative):
public void insert (E element){
//iterative insert element to tree
Node<E> node = new Node<>(element); //create new node for element
Node<E> current = root;
if (root == null) root = node; // if no root, new node is the root
else {
while (current != null){ //traverse tree to get to correct parent node
if (element.compareTo(current.data) < 0) { //case 1: node is smaller than current
if (current.left == null){
node.parent = current;
current.left = node;
}
else {
current = current.left;
}
}
else if (element.compareTo(current.data) > 0) { //case 2: node is larger than current
if (current.right == null){
node.parent = current;
current.right = node;
}
else {
current = current.right;
}
}
else { //case 3: node already exists
throw new RuntimeException("Node already exists");
}
}
}
size++;
}
the problem happens when I'm running my test class. there first node is added and becomes the root, but the next insert after that throws the exception no matter what the value is. It's acting like the value I'm trying to add already exists in the tree. If I comment out the exception, the test class doesn't compile, as if it's running an infinite loop.
What am I doing wrong here?
How would current ever become null in your while loop?
You don't break out from your while loop even when you insert new element:
if (current.left == null) {
node.parent = current;
current.left = node;
}
On the next iteration you assign current to the value you just inserted into current.left:
} else {
current = current.left;
}
and yet on the next iteration you now have current.data equal to element, which leads to the exception. Add break; after you insert the element:
if (current.left == null) {
node.parent = current;
current.left = node;
break;
}

How to print out a linkedlist?

Can somebody help me fix this issue with the code that I wrote?
When I run it doesn't print out the values of the linked list. I don't understand what is the problem, the compiler keeps giving a blank screen when I run the code.
public class Node {
int data;
Node next;
public static void main (String Args [])
{
Link object = new Link ();
object.insert(15);
object.insert(30);
object.insert(50);
object.insert(70);
object.show();
}
}
public class Link {
Node head;
void insert (int data)
{
Node node = new Node();
node.data=data;
if (head == null)
{
node=head;
}
else
{
Node n = head;
while (n.next != null)
{
n=n.next;
}
n.next=node;
}
}
void show ()
{
Node n = head;
while (n != null)
{
System.out.println(n.data);
n=n.next;
}
}
}
Your code is doing this:
if (head == null)
{
node=head;
}
This sets the null in head into the variable node. You aren't setting the value of head.
You should be doing this (setting the value of node into the variable head):
if (head == null)
{
head = node;
}
In your Link class, you need to change the following:
if (head == null)
{
node=head; //<-- change this to head = node;
}
¿Do you have to do it by that way? Java has already a LinkedList utility, makes it easier.

Remove Duplicates from Linked List

So I have a linked list that I am trying to remove duplicates from.
My basic algorithm that I thought up is to pretty much use the runner technique. Where I keep two pointers to compare adjacent elements. If they are the same I change the pointer of p1 to point to p1.next.next if not I keep traversing the list. However I keep getting a null pointer exception in the solution I have typed.
Node RemoveDuplicates(Node head) {
// This is a "method-only" submission.
// You only need to complete this method.
if (head == null){
return null;
}
Node current = head;
Node runner = head;
while(current != null && runner != null && runner.next != null){
runner = runner.next;
if(runner.data == current.data){
if(current.next != null){
current = current.next.next;
}
}else{
current = current.next;
}
}
return current;
}
At the point that I exit the while loop current is null. Which I think is the problem. How would I return the head of the altered list.
OK, although you've already accepted an answer, here's some example code using recursion to remove the dups from an ordered list per your request in the comments. (if your list isn't ordered, order it :) )
public Node removeDups(Node root) {
if (root.next == null)
return root;
root.next = removeDups(root.next);
if (root.data == root.next.data)
return root.next;
return root;
} // call as root = removeDups(root);
As you mentioned, recursion isn't really necessary here but you're using a Node-based linked list which is recursively defined. So, when it makes sense, the elegance of the solution has its benefits.
What I like about it is that you're not doing any node.next.next or needing to check for that null case. Once the stack starts unwinding, you're already in a position to start checking for dups. Then it's just a matter of comparing root.data and root.next.data; both of which you already know exist.
You can do this with single traversal with 2 pointer.and also this code works with single while loop.
public Node deleteDuplicates(Node head) {
Node current=head;
if (head == null)
return null;
else
{
Node runner=head.next;
while(head.next!=null && runner!=null)
{
if(head.val == runner.val)
prev=runner.next;
else
{
head.next=runner;
head=head.next;
prev=runner.next;
}
}
head.next=runner;
}
return current;
}
First of all, you'll want to return head at the end, so that you are returning the list, not just the last element.
Second thing, you'll want to modify the .next references instead of assigning them in some of the cases.
Note this doesn't work if the list isn't sorted.
Before: 1 1 3 3
After: 1 3
This code works (and I've tested it)
static Node RemoveDuplicates(Node head) {
if (head == null) return null;
Node current = head;
Node runner = head;
while (current != null && current.next != null) {
runner = current.next;
while (runner != null && runner.data == current.data) {
current.next = runner.next; // skip the repeat
runner = runner.next;
}
current = current.next;
}
return head;
}
You can do this in a single traversal. Just maintain two pointers temp
and next_of_next. Make temp iterate for each node and when the data of temp and the next node is equal, point next_of_next to the alternate node after temp and delete the node after temp.
Node removeDuplicates(Node head)
{
Node temp = head;
Node next_of_next;
if (head == null)
return;
while (temp.next != null)
{
if (temp.data == temp.next.data)
{
next_of_next = temp.next.next;
temp.next = null;
temp.next = next_of_next;
}
else
temp = temp.next;
}
return head ;
}
Here an apporach without recursion using a HashSet:
public void RemoveDuplicates()
{
if (head != null)
{
var hm = new HashSet<T>();
Node current = head;
Node prev = null;
while (current != null)
{
if (!hm.Contains(current.Value))
{
hm.Add(current.Value);
prev = current;
current = current.Next;
}
else
{
prev.Next = current.Next;
current = prev.Next;
}
}
}
return head;
}

Remove element from Binary Search Tree

I am doing an assignment, implementing own Binary Search Tree. The thing is, we have our own implementation of Node its parent is not directly accessible.
I have searched for answers, but I do not want to copy the solution entirely and I still don't seem to get it right, though. I miss some cases when the element is not removed.
Can you please help what am I doing wrong?
This is the remove method:
void remove(E elem) {
if(elem != null){
if (root != null && contains(elem)) {
removeFromSubtree(elem, root, null);
}
}
}
void removeFromSubtree(E elem, Node<E> current, Node<E> parent) {
if(elem.less(current.contents)){
if(current.left == null) return ;
removeFromSubtree(elem, current.left, current);
} else if(elem.greater(current.contents)){
if(current.right == null)return;
removeFromSubtree(elem, current.right, current);
} else {
if(current.left != null && current.right != null){
//both children
if(parent == null){
Node<E> n = new Node<>(null, null);
n.left = root;
removeFromSubtree(root.contents, n, null);
root = n.left;
root.setParent(null);
}
E min = subtreeMin(current.right);
current.contents = min;
removeFromSubtree(min, current.right, current);
} else if(current.left != null){
//left child
if (parent == null) {
root = current.left;
current.left.setParent(null);
return ;
}
setParentChild(current, parent, current.left);
} else if(current.right != null){
//right child
if (parent == null) {
root = current.right;
current.right.setParent(null);
return ;
}
setParentChild(current, parent, current.right);
} else {
if (parent == null) {
root = null;
return ;
}
setParentChild(current, parent, null);
}
}
}
Nodes use generic interface
class Node<E extends DSAComparable<E>>
which has just methods for comparation. It looks like this
interface DSAComparable<E extends DSAComparable<E>> {
boolean less(E other);
boolean greater(E other);
boolean equal(E other);
}
I use another methon inside remove that sets node's parent's child, depending if its left child or right child.
void setParentChild(Node<E> node, Node<E> parent,Node<E> value){
if(parent!= null){
if (parent.left == node) {
parent.left = value;
} else {
parent.right = value;
}
if(value!= null) value.setParent(parent);
}
}
Method subtreeMin(Node node) finds the smallest value in a subtree (the most left one)
Understanding your code is not so easy, since it still lacks of details.
I would refer to such an implementation of the Binary Search Tree that you can find online.
See for instance the one from Algorithms, 4th Ed..

Balancing check never terminates in Avl tree insertion

Writing an AVL Tree to hold generics for my data structures course; in my add() method, after actually inserting an element, I step back up through its ancestors checking their scores. For the first few additions it works, but (presumably at the point where balancing does need to be done), the loop back up the path fails to terminate. I've tried everything I can think of to make sure the root of the tree's parent isn't getting set to another node or anything like that. I'm calculating balance scores as right minus left, so positive means a tree is right-heavy and negative means left-heavy. Here's my add():
public void add(T e){
if (e == null)
return;
if (root == null) {
root = new TNode<T>(null, e);
return;
}
boolean added = false;
TNode<T> current = root;
while (current != null && added != true) { //insertion loop
if (current.compareTo(e) == 0)
return;
else if (current.compareTo(e) < 0) {
if (current.getRight() == null) {
current.setRight(new TNode<T>(current, e));
added = true;
}
else
current = current.getRight();
}
else if (current.compareTo(e) > 0) {
if (current.getLeft() == null) {
current.setLeft(new TNode<T>(current, e));
added = true;
}
else
current = current.getLeft();
}
}
if (useAVL == false)
return;
//balancing, checking up from added node to find where tree is unbalanced; currently loop does not terminate
//current is now parent of inserted node
while (current.hasParent()) {
if (current.getAvl() > 1) {
if (current.getRight().getAvl() > 0)
current = rotateLeft(current);
else if (current.getRight().getAvl() < 0) {
current.setRight(rotateRight(current.getRight()));
current = rotateLeft(current);
}
}
if (current.getAvl() < -1) {
if (current.getLeft().getAvl() < 0)
current = rotateRight(current);
else if (current.getLeft().getAvl() > 0) {
current.setLeft(rotateLeft(current.getLeft()));
current = rotateRight(current);
}
}
current = current.getParent();
}
root = current;
root.setParent(null);
}
And here's the right rotation method:
private TNode<T> rotateRight(TNode<T> old) {
TNode<T> oldRoot = old;
TNode<T> newRoot = old.getLeft();
TNode<T> rightChildNewRoot = newRoot.getRight(); //was right child of what will become root, becomes left child of old root
newRoot.setRight(oldRoot);
oldRoot.setParent(newRoot);
oldRoot.setLeft(rightChildNewRoot);
if (rightChildNewRoot != null)
rightChildNewRoot.setParent(oldRoot);
newRoot.setParent(oldRoot.getParent());
return newRoot;
}

Categories