Results 1 to 7 of 7
  1. #1
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Object of its own class?

    Hi.
    I'm preparing for an exam in a course focusing on algorithms, and I need help understanding the following code:
    Java Code:
    protected static class Node<E> {
    		public E data;
    		public Node<E> left;
    		public Node<E> right;
    
    		public Node(E data) {
    			this.data = data;
    			left = null;
    			right = null;
    		}
    
    		public String toString() {
    			return data.toString();
    		}
    	}
    What does left and right do? Are they objects of their own class?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Object of its own class?

    Yes. This would be a typical class to describe the node of a binary tree, where each node may have a reference to a possible left or right child, which is also of type Node<E>.

    Regards,
    Jim
    Last edited by jim829; 02-04-2014 at 01:34 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: Object of its own class?

    Quote Originally Posted by jim829 View Post
    Yes. This would be a typical class to describe the node of a binary tree, where each node may have a reference to a possible left or right child, which is also of type Node<E>.

    Regards,
    Jim
    So each node has two children, ok I think I get it now. Thank you. :)

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Object of its own class?

    It might also be a bi-directional linked list where you have a link to the node to the left and a node to the right to be able to navigate back and forth.

    If it were a tree node, I'd also expect a 'parent' so you could build algorithms that go back up the tree once they reach a dead end for example.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Object of its own class?

    Seems like a tree node, my TreeNode class when working with bin trees and BST looked like this.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Object of its own class?

    Perhaps your tree code just stinks. Ever consider that?

    I'M KIDDING, if all you need to do is go down from the root to the children then you don't need a reference back to the parent. Probably more datastructures with a "node" can be reasoned, that was not the original question anyway.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Object of its own class?

    my professor gave us the tree node class so his tree code stinks

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  2. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  3. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  4. Replies: 3
    Last Post: 03-15-2010, 08:44 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

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