Validating a Tree


public static boolean isValid( Node node ) {

 // A null node is valid 🙂
 if( node == null ) {
 return true;
 }

 // Check that this is not a root node (which we don't need to validate)

 if( node.parent != null ) {
 // Determine if this is a left child or right child.
 if( node == node.parent.left ) {
 return node.data < node.parent.data;
 } else {
 return node.data > node.parent.data;
 }
 }

 // Continue with the rest of left and right subtrees.
 return isValid( node.left ) && isValid( node.right );
 }


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Website

arpit tak

arpit tak

I like JAVA . I code. I chill. I blog.I eat. I sleep. I dream.

Personal Links

View Full Profile →

Followers

%d bloggers like this: