java - Detecting if a node in a tree has child(s) or child nodes have been visited -


i struggling logic behind detecting nodes in tree exists or have been visited.

i have tree nodes (a node has left , right child node).

i want check 2 things on node: if there no child nodes if there child(s) nodes want check if have been visited.

i have large condition hate of. there way can simplify it?

public boolean finished(){       return right == null && left == null || ((right != null && right.visited && (left != null && left.visited)) } 

i want finished() true: if right , left node dont exist if right exists , has been visited if left exists , has been visited

i think need or if right exists and visited and left null

i'm bit confused :s

i think may after

if( (right == null || right.visited) && ( left == null || left.visited) ) 

so each child node if either empty or has been visited finished.

this covers cases

right == null; left == null;  right == null; left.visited;  right.visited; left == null;  right.visited; left.visited; 

which think given description cases wanted check for.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -