Skip to content

Instantly share code, notes, and snippets.

@anil477
Created July 2, 2017 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/4c55a3fa18373fabcd4f19f74eaef024 to your computer and use it in GitHub Desktop.
Save anil477/4c55a3fa18373fabcd4f19f74eaef024 to your computer and use it in GitHub Desktop.
Boundary Traversal of binary tree
// http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/
class Node
{
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}
class BinaryTree
{
Node root;
// A simple function to print leaf nodes of a binary tree
// This method is same as inorder traversal except that we need only the leaf nodes.
void printLeaves(Node node)
{
if (node != null)
{
printLeaves(node.left);
// Print it if it is a leaf node
if (node.left == null && node.right == null)
System.out.print(node.data + " ");
printLeaves(node.right);
}
}
// A function to print all left boundry nodes, except a leaf node.
// Print the nodes in TOP DOWN manner - note the data is printed before the recursive call
// Initially this method is called with the root.left and root
// NOTE - the boundary methods don't print the leaf node.
void printBoundaryLeft(Node node)
{
if (node != null)
{
if (node.left != null)
{
// to ensure top down order, print the node before calling itself for left subtree
System.out.print(node.data + " ");
printBoundaryLeft(node.left);
}
else if (node.right != null)
{
// if the tree does not have left nodes, then right nodes will form the boundary.
// This is taken here by this if condition.
// if there's no left-subtree but only right sub-tree and right-subtree has both left and right subtree,
// then left subtree should be printed.
// that's why the left condition is above the right condition
System.out.print(node.data + " ");
printBoundaryLeft(node.right);
}
// do nothing if it is a leaf node, this way we avoid
// duplicates in output
}
}
// A function to print all right boundry nodes, except a leaf node
// Print the nodes in BOTTOM UP manner - note the data is printed after the recursive call
// Initially this method is called with the root.right and root
// NOTE - the boundary methods don't print the leaf node.
void printBoundaryRight(Node node)
{
if (node != null)
{
if (node.right != null)
{
// to ensure bottom up order, first call for right subtree, then print this node
printBoundaryRight(node.right);
System.out.print(node.data + " ");
}
else if (node.left != null)
{
// if the tree does not have right nodes, then left nodes will form the boundary.
// This is taken here by this if condition.
// if there's no right-subtree but only left sub-tree and left-subtree has both left and right subtree,
// then right subtree should be printed.
// that's why the right condition is above the left condition
printBoundaryRight(node.left);
System.out.print(node.data + " ");
}
// do nothing if it is a leaf node, this way we avoid
// duplicates in output
}
}
// A function to do boundary traversal of a given binary tree
void printBoundary(Node node)
{
if (node != null)
{
// the root data is printed explicity and not included in any method as it will overlap
System.out.print(node.data + " ");
// Since it's required to print data in anti-clock wise direction, the following order is chosen
// Print the left boundary in top-down manner.
printBoundaryLeft(node.left);
// Print all leaf nodes
printLeaves(node.left);
printLeaves(node.right);
// Print the right boundary in bottom-up manner
printBoundaryRight(node.right);
}
}
// Driver program to test above functions
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(20);
tree.root.left = new Node(8);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(12);
tree.root.left.right.left = new Node(10);
tree.root.left.right.right = new Node(14);
tree.root.right = new Node(22);
tree.root.right.right = new Node(25);
tree.printBoundary(tree.root);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment