0

I have completed the recursive insert function and it works perfectly, but I can not get the non recursive solution to work.

public void insert(T item){
root= nonRecursive(root,item);
}
public BinaryTreeNode<T> nonRecursive(BinaryTreeNode<T> tree, T item){

if(root==null){
  root=new BinaryTreeNode<T>(item);
  return root;
}
else{
 BinaryTreeNode<T> next = new BinaryTreeNode<T>();
 Comparable<T> temp = (Comparable<T>) root.info;
  if(temp.compareTo(item)== 0){
   return null;
  }
  else if(temp.compareTo(item) > 0){
    next=root.lLink;
  }
  else{
   next=root.rLink; 
  }
  while(next != null){
    Comparable<T> temp2 = (Comparable<T>) next.info;
    if(temp.compareTo(item) == 0){
      return null;
    }
    else if(temp2.compareTo(item) > 0){
     next=next.lLink; 
    }
    else{
     next=next.rLink; 
    }

  }
  next=new BinaryTreeNode<T>(item);
  return root;
}

} and then the recursive one is:

public void insert(T item) {
  root = recInsert(root, item);
}
public BinaryTreeNode<T> recInsert(BinaryTreeNode<T> tree, T item) {
  if(tree == null) {
 //create new node
    tree = new BinaryTreeNode<T>(item);
  }
  else {
    Comparable<T> temp = (Comparable<T>) tree.info;
    if (temp.compareTo(item) == 0) {
      System.err.print("Already in ­ duplicates are not allowed.");
      return null;
    }
    else if (temp.compareTo(item) > 0)
      tree.lLink = recInsert(tree.lLink, item);
    else
      tree.rLink = recInsert(tree.rLink, item);
  }
  return tree;
}

does anyone know what I am doing wrong? I thought I had gotten it but now it only returns the first number I enter in

mdavies23
  • 67
  • 7
  • line 191 is current.lLink=node; – mdavies23 Dec 09 '15 at 02:54
  • Well, there's only one thing in that line that could possibly be `null`. Possible dupe? http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – markspace Dec 09 '15 at 02:56
  • That means `current` is null. Please learn to use a debugger and trace your logic and it should be easy to find out what went wrong – Adrian Shum Dec 09 '15 at 02:56
  • and you don't even have comparison happening in your "non-recursive" version. Can you make sure your code is complete to up reasonable extends before asking? And, can you learn how to format and properly indent your code? – Adrian Shum Dec 09 '15 at 02:57
  • It is really frustrating when you fix someone's code formatting once, and they replace again with messily formatted code :( – Adrian Shum Dec 09 '15 at 06:15

2 Answers2

0

here you go then

in your code,

if(current == null){
    current.lLink=node;

if current is null, then how can it have a iLink ?

maybe you need to do

if(current == null){
    current = new Node ();
    current.lLink=node;
Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
  • are you kidding? if `current` is null, how can you access `current.lLink` and assign it with new value? – Adrian Shum Dec 09 '15 at 02:59
  • @AdrianShum Sorry, the first code is the OP's code, I am showing him how to fix. I will reword it for more clarity. – Scary Wombat Dec 09 '15 at 03:01
  • sorry I mixed that up. However, if we look into OP's code again, it is not even near complete. He just do meaningless loops inside, not even doing one single comparison. So it is probably not meaningful to tell him "how" to fix that – Adrian Shum Dec 09 '15 at 03:04
  • and your way of fixing is meaningless too. You just create a new `Node` for current. But that node is never part of the tree – Adrian Shum Dec 09 '15 at 03:05
  • @AdrianShum I guess that getting this to work for the OP will be an iterative process, and I was simply fixing his NPE. – Scary Wombat Dec 09 '15 at 03:51
  • just ask him to remark the assignment statement will be even a shorter fix for NPE if you don't care about whether the fix is meaningful or not :P – Adrian Shum Dec 09 '15 at 03:54
0

Your code is not even close to finish.

You haven't even done one comparison. What you did is simply meaningless loops.

If you are looking for a non-recursive logic, here is the pseudo code. Your job is to understand it and write it in Java.

insert(item) {
    Node itemNode = new Node(item);

    if root is null {
        root = itemNode
        return
    }

    currentNode = root;

    keep looping until node is inserted {
        if currentNode is equals to itemNode {
            show error and exit
        } else if itemNode is smaller than currentNode {
            if (currentNode has no left){
                set currentNode's left to itemNode
                // Item Inserted!!!!
            } else {  // there are node at currentNode's left
                 set currentNode to currentNode's left (and continue lookup)
            }
        } else { // item node is greater than current node
             // do similar thing as the "itemNode < currentNode logic", 
             // of course on currentNode's right
        }
    }
}
Adrian Shum
  • 35,318
  • 9
  • 72
  • 119
  • I edited my original post with what i think is correct. It only prints out the first number i enter into the tree. Do you see any reason why? – mdavies23 Dec 09 '15 at 05:15
  • is it really that hard to properly format your code? – Adrian Shum Dec 09 '15 at 05:18
  • learn to use a debugger. You put your `next=new BinaryTreeNode(item);` in a nonsense position. You haven't added your node into the tree. Read the psuedo code again to make sure you really understand it. StackOverflow is not meant to be a manual-debugger site – Adrian Shum Dec 09 '15 at 05:20