Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default 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:

    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;
        }
    Here is some of the relevant code for Node:

    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);
        }
    }
    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?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Can't Figure Out How To Set A Sub-Node to Null

    Moved from Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default 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

  1. Replies: 1
    Last Post: 08-12-2012, 03:15 AM
  2. Replies: 1
    Last Post: 04-16-2012, 02:29 PM
  3. can someone help me to figure this out >.<
    By kissmeeq in forum New To Java
    Replies: 2
    Last Post: 08-01-2011, 03:19 PM
  4. Replies: 6
    Last Post: 10-18-2010, 10:40 AM
  5. [SOLVED] Cant figure out null pointer exception
    By todd2230 in forum New To Java
    Replies: 6
    Last Post: 05-06-2008, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •