Trees in Java

import org.testng.annotations.Test;

class TreeNode{

TreeNode left;

TreeNode right;

int data;

TreeNode (int data)

{

this.data=data;

}

public void inorder(TreeNode root)

{

if(root !=null)

{

inorder(root.left);

System.out.println(root.data);

inorder(root.right);

}

}

public void preorder(TreeNode root)

{

if(root !=null)

{

System.out.println(root.data);

preorder(root.left);

preorder(root.right);

}

}

public void postorder(TreeNode root)

{

if(root !=null)

{

postorder(root.left);

postorder(root.right);

System.out.println(root.data);

}

}

}

public class TreeTest {

@Test

public void testTree() {

TreeNode root=new TreeNode(20);

TreeNode leftnode=new TreeNode(10);

TreeNode rightnode=new TreeNode(40);

root.left=leftnode;

root.right=rightnode;

System.out.println(“Using Inorder”);

root.inorder(root);

System.out.println(“Using Preorder”);

root.preorder(root);

System.out.println(“Using Postorder”);

root.postorder(root);

}

}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Uncategorized. Bookmark the permalink.

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