Results 1 to 4 of 4
- 12-24-2012, 05:17 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 1
- Rep Power
- 0
Can't Figure Out How To Set A Sub-Node to Null
I'm trying to design an AVL Tree, and I've run into a bit of a roadblock with my Left-Right Rotate Function.

Here is the code for it:
Here is some of the relevant code for Node:Java Code:public Node LRBalance(Node n) { Node p = new Node(n.getLeftChild().getRightChild()); p.setRightChild(n); if(n.getLeftChild().getRightChild().getRightChild() != null) p.getRightChild().setLeftChild(n.getLeftChild().getRightChild().getRightChild()); if(n.getLeftChild() != null) p.setLeftChild(n.getLeftChild()); if(n.getLeftChild().getRightChild().getLeftChild() != null) p.getLeftChild().setRightChild(n.getLeftChild().getRightChild().getLeftChild()); return p; }
In my code I check to ensure that the node that I am copying is not null. However if it is null, I'm not sure what I should do. I don't know how to set that node on my copied tree to null. I think it's because I made leftChild and rightChild private and used accessor and mutator functions. Should I just go ahead and make them public, or is there a way for me to do this and keep them as private?Java Code:public class Node { private int value; private Node leftChild; private Node rightChild; public Node() { value = 0; leftChild = null; rightChild = null; } public Node(int val) { value = val; leftChild = null; rightChild = null; } public Node(Node n) { value = n.getValue(); if (n.leftChild != null) { leftChild = new Node(n.getLeftChild()); } else { leftChild = null; } if (n.rightChild != null) { rightChild = new Node(n.getRightChild()); } else { rightChild = null; } } public Node getLeftChild() { return leftChild; } public void setLeftChild(Node n) { leftChild = new Node(n); } public Node getRightChild() { return rightChild; } public void setRightChild(Node n) { rightChild = new Node(n); } }
- 12-24-2012, 08:27 AM #2
Re: Can't Figure Out How To Set A Sub-Node to Null
If both trees are instances of the same class, they can access each other's private members.
If that's not the case, then you need to post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-24-2012, 08:28 AM #3
Re: Can't Figure Out How To Set A Sub-Node to Null
Moved from Advanced Java.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-27-2012, 06:09 AM #4
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: Can't Figure Out How To Set A Sub-Node to Null
When you balance your trees (and subtrees) you don't need to create any new nodes. You just need to rearrange the nodes that you have. Upon entry, this is the root of the subtree. Upon exit, you return the new root of the tree.
Your code is trying to balance too many levels at the same time. With the magic of recursion, the other levels of the tree will be balanced automatically as each level of the recursive calls are made. Here is some code that I took from a working solution to the AVL Tree problem. Notice that I build upon RotateLeft() and RotateRight() in making my LRRotation method.
It shouldn't matter if your methods are public or not. It really shouldn't be necessary though since your Node class should be the only class that needs to access them.
Java Code:private static AVL_Node LRRotation(AVL_Node n) { // do the rotations AVL_Node leftNode = RotateLeft(n.getLeftNode()); n.setLeftNode(leftNode); n = RotateRight(n); // return the result return n; }
Similar Threads
-
Problem casting from org.w3c.dom.Node to my subclass of Node
By msphelix in forum Advanced JavaReplies: 1Last Post: 08-12-2012, 03:15 AM -
mapping one XML node to another XML node in GUI.
By ashu_i20 in forum XMLReplies: 1Last Post: 04-16-2012, 02:29 PM -
can someone help me to figure this out >.<
By kissmeeq in forum New To JavaReplies: 2Last Post: 08-01-2011, 03:19 PM -
Is it better to set array elements to null before setting the arrayreference to null?
By kreyszig in forum Advanced JavaReplies: 6Last Post: 10-18-2010, 10:40 AM -
[SOLVED] Cant figure out null pointer exception
By todd2230 in forum New To JavaReplies: 6Last Post: 05-06-2008, 07:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks