Boundary Traversal


public Node printLeftPreOrder(Node n){
 if(n == null){
 return n;
 }
 System.out.print(" "+n.data);
 n.left = printLeftPreOrder(n.left);
 printLeaf(n.right);
 return n;
 }
 // this is easy implementation..
 /*public Node printLeftPreOrder(Node n){

 if(n == null){
 return n;
 }
 System.out.print(" "+n.data);

while (n.left != null) {
 n = n.left;
 System.out.print(" "+n.data);
 }


 printLeaf(n.right);
 return n;
 } */
public void printLeaf(Node n){
 if(n==null)
 return;

if(n.left == null && n.right == null){
 System.out.print(" "+n.data);
 return;
 }
 printLeaf(n.left);
 printLeaf(n.right);
 }

 public Node printRightPostOrder(Node n){
 if(n == null)
 return n;
 printLeaf(n.left);
 n.right = printRightPostOrder(n.right);
 System.out.print(" "+n.data);
 return n;
 }

 public void printTiangle(Node n){
 if(n == null)
 return;
 printLeftPreOrder(n.left);
 printRightPostOrder(n.right);
 System.out.print(" "+n.data);
 }
public void Leaf(Node n){

int sum =0;

 if(n==null)
 return ;

if(n.left == null && n.right == null){
 System.out.print(" "+n.data);
 sum = sum +n.data;
 }

printLeaf(n.left);
 printLeaf(n.right);

}

public int Sum(Node n){

int sum =0;

 if(n==null)
 return 0;
 printLeaf(n.left);
 printLeaf(n.right);

 if(n.left == null && n.right == null){
 System.out.print(" "+n.data);
 sum = sum +n.data;
 }
 return sum;

}

Advertisements

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

%d bloggers like this: