Splay Tree insert() issue PART 2

So now my grabnode() is having trouble handling the case where the node that is trying to get inserted already exists (in this case, i want to return that nodes key value when it is found in the tree, so i may change its item value in the insert method) instead I get a java.lang.classCastException when a duplicate arises.
      public void insert(Object key, Object item){
            TreeNode parentNode, n, newNode;
            Object nObj;
            parentNode = null;
            if (isEmpty()) {
                _root = new TreeNode(key, item);
            }else{
            //See if the node exists or not
                  nObj = grabNode(key,_root);
            
            if(find(key) == null){
                  //The node doesn't exist, so create it
                  parentNode = findParent(key,_root);
                  newNode = new TreeNode(key, item);
                  newNode.setParent(parentNode);
                  Comparable cKey = (Comparable)newNode.getKey();
                  Comparable tKey = (Comparable) parentNode.getKey();
                  if(cKey.compareTo(tKey) < 0){
                        parentNode.setLeft(newNode);
                  }else if(cKey.compareTo(tKey) > 0){
                        parentNode.setRight(newNode);
                  }      
                 //splay(newNode);
                  _size++;
            }else{
                  n = (TreeNode)find(key);
                  //The node does exist, so change its item value
                  n.setItem(item);
                  //splay(n);
            }
            }
      }


      public Object grabNode(Object key, TreeNode x){
            Comparable cKey;
            Comparable tKey;
            Object returnVal = null;
            if(x == null){
                  return null;
            }else{
                  cKey = (Comparable)key;
                  tKey = (Comparable)x.getKey();
              if(cKey.compareTo(tKey) < 0){
              //NODE IS TO THE LEFT OF THE ROOT
                    if(x.getLeft()!=null){
                          return grabNode(key,x.getLeft());
                    }else{
                          return null;
                    }
              }else if(cKey.compareTo(tKey) > 0){
              //NODE IS TO THE RIGHT OF THE ROOT
                    if(x.getRight()!=null){
                          return grabNode(key,x.getRight());
                    }else{
                          return null;
                    }
              }else{
                    //the node exists!!
                    return x.getKey();
              }
            }
      }
LVL 9
D4LyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Make sure the object *is* Comparable
0
D4LyAuthor Commented:
??
0
CEHJCommented:
>>
Comparable cKey = (Comparable)newNode.getKey();
              Comparable tKey = (Comparable) parentNode.getKey();
>>

is where you're casting. You need to check that the keys *are* Comparable
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

petmagdyCommented:
please do this:
Object object = find(key);
system.out.println("Class type: " + object.getClass().getName());

what does it prints?
0
objectsCommented:
post the stack trace for the exception
0
D4LyAuthor Commented:
Class type: java.lang.Integer

here's my find():

public Object find(Object key){
            if (_root == null) return null;
            return grabNode(key, _root);
      }
0
D4LyAuthor Commented:
java.lang.ClassCastException
at SplayTree.insert(SplayTree.java:159)
      at Question1.insertValues(Question1.java:42)
      at Question1.doIt(Question1.java:19)
      at Question1.main(Question1.java:94)
Exception in thread "main"

I must leave doIt main, and insertValues as-is.
0
petmagdyCommented:
then u can't cast it to TreeNode thats it
0
objectsCommented:
And check that find() actually returns a TreeNode as petmagdy  has suggested.
0
D4LyAuthor Commented:
ok. this works perfectly fine assuming no duplicates emerge. when a duplicate does emerge (the key is found) the error occurs. so if i can't cast to TreeNode, what is the fix? or a direction toward the fix?
0
petmagdyCommented:
grapNode should return x not x.getInt()
0
petmagdyCommented:
    public Object grabNode(Object key, TreeNode x){
          Comparable cKey;
          Comparable tKey;
          Object returnVal = null;
          if(x == null){
               return null;
          }else{
               cKey = (Comparable)key;
               tKey = (Comparable)x.getKey();
             if(cKey.compareTo(tKey) < 0){
             //NODE IS TO THE LEFT OF THE ROOT
                  if(x.getLeft()!=null){
                       return grabNode(key,x.getLeft());
                  }else{
                       return null;
                  }
             }else if(cKey.compareTo(tKey) > 0){
             //NODE IS TO THE RIGHT OF THE ROOT
                  if(x.getRight()!=null){
                       return grabNode(key,x.getRight());
                  }else{
                       return null;
                  }
             }else{
                  //the node exists!!
                  return x.getKey();   <--- here is ur problem u should return x;
             }
          }
     }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
see the note at this line:

                  return x.getKey();
0
D4LyAuthor Commented:
find does not return treenode since it is type object, as specified in the interface implemented in the class find() is found in.

petmagdy, how am i returning x.getInt? x is type TreeNode, so is x.getLeft, x.getRight, and x.getParent
0
petmagdyCommented:
sorry read my last 2 comments
0
D4LyAuthor Commented:
sorry...thx for such immediate responses everyone.

petmagdy, it works great now. can you explain why?
0
petmagdyCommented:
grapNode is meant to return the TreeNode itself not the key of the tree node,  u was expecting TreeNode in the return result
0
D4LyAuthor Commented:
AH. thx!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.