Level Traversal for a BT iteratively.
Very important function to find max/min, node count, check for a node if exists.
private void iterativeLevelTraversal( Node node){
Queue<Node> queue = new LinkedList<Node>();
Node temp_node = root ;
// we can find min-max//comparing leaf elements.
while( temp_node != null){
// write all your check functions over here.
// it can be finding the max/min, node count,
// check if the node exists, find the sum of nodes many such....!
System.out.print( temp_node.getValue() + " ");
if( temp_node.getLeftNode() != null){
queue.add( temp_node.getLeftNode());
}
if( temp_node.getRightNode() != null){
queue.add( temp_node.getRightNode());
}
temp_node = queue.poll();
}
}
Very important function to find max/min, node count, check for a node if exists.
private void iterativeLevelTraversal( Node node){
Queue<Node> queue = new LinkedList<Node>();
Node temp_node = root ;
// we can find min-max//comparing leaf elements.
while( temp_node != null){
// write all your check functions over here.
// it can be finding the max/min, node count,
// check if the node exists, find the sum of nodes many such....!
System.out.print( temp_node.getValue() + " ");
if( temp_node.getLeftNode() != null){
queue.add( temp_node.getLeftNode());
}
if( temp_node.getRightNode() != null){
queue.add( temp_node.getRightNode());
}
temp_node = queue.poll();
}
}
No comments:
Post a Comment