m2skills

boundary traversal java

Jun 13th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. // http://code2begin.blogspot.com
  2. // program to print boundary traversal of a given binary tree
  3.  
  4. /**
  5.  * Created by MOHIT on 25-05-2018.
  6.  */
  7. import java.util.*;
  8.  
  9. // node class
  10. class node{
  11.     int data;
  12.     node left;
  13.     node right;
  14.  
  15.     // function that returns a pointer to new node
  16.     public node(int element){
  17.         this.data = element;
  18.         this.left = null;
  19.         this.right = null;
  20.        
  21.     }
  22. };
  23.  
  24. public class BinaryTree {
  25.    
  26.     // function to print the left boundary of the tree
  27.     static void print_left_boundary(node root){
  28.         if (root != null){
  29.             if (root.left != null){
  30.                 System.out.print(root.data + " ");
  31.                 print_left_boundary(root.left);
  32.             }else if (root.right != null){
  33.                 System.out.print(root.data + " ");
  34.                 print_left_boundary(root.right);
  35.             }
  36.         }
  37.         return;
  38.     }
  39.    
  40.    
  41.     // function to print the left boundary of the tree
  42.     static void print_right_boundary(node root){
  43.         if (root != null){
  44.             if (root.right != null){
  45.                 print_right_boundary(root.right);
  46.                 System.out.print(root.data + " ");
  47.             }else if (root.left != null){
  48.                 print_right_boundary(root.left);
  49.                 System.out.print(root.data + " ");
  50.             }
  51.         }
  52.         return;
  53.     }
  54.    
  55.     // function to print the leaf nodes of the binary tree
  56.     static void print_leaves(node root){
  57.         if (root != null){
  58.             print_leaves(root.left);
  59.             if(root.left == null && root.right == null){
  60.                 System.out.print(root.data + " ");
  61.             }
  62.             print_leaves(root.right);
  63.         }
  64.     }
  65.    
  66.     // function to print the boundary traversal of the binary tree
  67.     static void boundary_traversal(node root){
  68.         print_left_boundary(root);
  69.         print_leaves(root);
  70.         print_right_boundary(root);
  71.         return;
  72.     }
  73.  
  74.     public static void main(String arg[]) {
  75.         node head = new node(1);
  76.         head.left = new node(2);
  77.         head.right = new node(3);
  78.         head.left.left = new node(4);
  79.         head.left.right = new node(5);
  80.         head.right.right = new node(6);
  81.         head.left.left.right = new node(7);
  82.         head.right.right.left = new node(8);
  83.         head.left.left.right.left = new node(9);
  84.         head.left.left.right.left.left = new node(10);
  85.         head.right.right.left.right = new node(11);
  86.         System.out.print("Boundary traversal of the binary tree is : ");
  87.         boundary_traversal(head);
  88.     }
  89. }
Add Comment
Please, Sign In to add comment