Posted on by Kalkicode
Code Binary Tree

boundary traversal of binary tree java

Java program for boundary traversal of binary tree. Here more information.

/* 
  Java program for
  Boundary Traversal of binary tree
*/
// Binary Tree Node
class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		// Set initial tree root
		this.root = null;
	}
	public boolean isLeaf(TreeNode node)
	{
		if (node != null && node.left == null && node.right == null)
		{
			return true;
		}
		return false;
	}
	// Print all leftmost node 
	public void printLeftMost(TreeNode node)
	{
		if (node != null && node.left != null)
		{
			System.out.print("  " + node.data);
			// Recursively visit left subtree
			printLeftMost(node.left);
		}
	}
	// Print rightmost node in reverse order
	public void printRightMost(TreeNode node)
	{
		if (node != null && node.right != null)
		{
			printRightMost(node.right);
			System.out.print("  " + node.data);
		}
	}
	// Display all leaf nodes
	public void printLeafNode(TreeNode node)
	{
		if (node != null)
		{
			if (this.isLeaf(node))
			{
				// Display left node
				System.out.print("  " + node.data);
			}
			printLeafNode(node.left);
			printLeafNode(node.right);
		}
	}
	public void boundaryView()
	{
		if (this.root == null)
		{
			return;
		}
		// Step A
		// print root value
		System.out.print(this.root.data);
		if (this.isLeaf(this.root))
		{
			// When only single node
			return;
		}
		// Step B
		// Print leftmost nodes
		printLeftMost(this.root.left);
		// Step C
		// Print all leaf nodes
		printLeafNode(this.root);
		// Step D
		// Print rightmost nodes
		printRightMost(this.root.right);
	}
	public static void main(String[] args)
	{
		// Create new tree
		BinaryTree tree = new BinaryTree();
		/* Make A Binary Tree
		  -----------------------
		             1
		           /   \
		          2     3
		         /  \   / \
		        4    5 6   7
		       /    /   \    \
		      12   8     9    10
		            \
		             11
		*/
		// Add tree nodes
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(2);
		tree.root.right = new TreeNode(3);
		tree.root.right.right = new TreeNode(7);
		tree.root.right.left = new TreeNode(6);
		tree.root.left.left = new TreeNode(4);
		tree.root.left.right = new TreeNode(5);
		tree.root.left.right.left = new TreeNode(8);
		tree.root.left.right.left.right = new TreeNode(11);
		tree.root.left.left.left = new TreeNode(12);
		tree.root.right.left.right = new TreeNode(9);
		tree.root.right.right.right = new TreeNode(10);
		// Display boundary view
		tree.boundaryView();
	}
}

Output

1  2  4  12  11  9  10  7  3

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment