Showing posts with label Tree. Show all posts
Showing posts with label Tree. Show all posts

Saturday, April 13, 2019

Spiral Order Traversal of a Tree [Binary Tree Zigzag Level Order Traversal]

Problem statement:
Write a function to print spiral order traversal of a tree. 
For example.

Input - 
10
J
H
I
A
C
D
F
E
B
G
where 10 is the number of elements in the input array.

Output -
A
BC
FED
GHIJ

Wednesday, June 20, 2018

How do you create tree data structure ?

Problem statement: How will you create tree in java?
  1. /* Class containing left and right child of current
  2. node and data value*/
  3. class Node
  4. {
  5. int data;
  6. Node left, right;

  7. public Node(int data1)
  8. {
  9. data = data1;
  10. left = right = null;
  11. }
  12. }

  13. // A Java program to introduce Binary Tree
  14. class BinaryTree
  15. {
  16. // Root of Binary Tree
  17. Node root;

  18. // Constructors
  19. BinaryTree(int data1)
  20. {
  21. root = new Node(data1);
  22. }

  23. BinaryTree()
  24. {
  25. root = null;
  26. }

  27. public static void main(String[] args)
  28. {
  29. BinaryTree tree = new BinaryTree();

  30. /*create root*/
  31. tree.root = new Node(1);

  32. /* following is the tree after above statement
  33.  
  34.               1
  35.             /   \
  36.           null  null     */

  37. tree.root.left = new Node(2);
  38. tree.root.right = new Node(3);

  39. /* 2 and 3 become left and right children of 1
  40.                1
  41.              /   \
  42.             2      3
  43.           /    \    /  \
  44.         null null null null  */


  45. tree.root.left.left = new Node(4);
  46. tree.root.left.left.right = new Node(9);
  47.         /* 4 becomes left child of 2
  48.                     1
  49.                 /       \
  50.                2          3
  51.              /   \       /  \
  52.             4    null  null  null
  53.            /   \
  54.           null 9
  55.          */
  56. System.out.println(tree.root.right.data);
  57. }
  58. }
Output: 3

Saturday, June 16, 2018

How do you check whether given trees are isomorphic to each other or not?

Problem statement: Given two trees how do we check whether the trees are isomorphic or not !!

Two binary trees root1 & root2 are isomorphic if they are the same structure. The values of nodes does not affect whether two trees are isomorphic or not.  



  1. int IsIsometric(TreeNode root1, TreeRoot root2){
  2. f(root1 == null && root2 == null)
  3. return 1;
  4. if((root1 == null && root2 != null) || (root1 !=null && root2 == null)) return 0;
  5. return (IsIsometric(root1.getLeft(), root2.getLeft()) && IsIsometric(root1.getRight(), root2.getRight()));
  6. }
Time complexity: O(n), Space complexity: O(n)

Friday, June 1, 2018

How many different binary trees are possible with n nodes?

Problem statement: what do you say, how many different binary trees are possible with n nodes?
  • In general, 
  1. if there are n nodes,
  2. then there exist 2n - n different trees
  • example - 
  1. number of node = 3
  2. then different trees = 23 - 3 = 8 - 3 = 5

Monday, May 7, 2018

Tree Data structure Implementation



  1. class Node {

  2. int data;
  3. Node left;
  4. Node right;

  5. Node(int data) {
  6. this.data = data;

  7. }
  8. }

  9. public class BaseTree {
  10. public static void main(String[] args) {
  11. Node root=new Node(1);

  12. Node a1=new Node(2);
  13. Node a2=new Node(3);
  14. Node a3=new Node(4);
  15. Node a4=new Node(5);
  16. Node a5=new Node(6);
  17. Node a6=new Node(7);


  18. root.left=a1;
  19. root.right=a2;

  20. a1.left=a3;
  21. a1.right=a4;

  22. a2.left=a5;
  23. a2.right=a6;


  24. System.out.println(height(root)-1);
  25. //preOrder(root);
  26. //inOrder(root);
  27. //postOrder(root);
  28. //System.out.println(root.left.right.data);
  29. //System.out.println(root.data);
  30. //System.out.println(root.left.data);
  31. //System.out.println(root.left.left.data);
  32. //System.out.println(root.left.right.data);
  33. //System.out.println(root.right.left.data);
  34. //System.out.println(root.right.right.data);
  35. }

  36. public static void preOrder(Node root){
  37. if(root==null){
  38. return;
  39. }
  40. System.out.println(root.data);
  41. preOrder(root.left);// root=root.left;root=root.left.left;
  42. preOrder(root.right);
  43. }
  44. public static void inOrder(Node root){
  45. if(root==null){
  46. return;
  47. }
  48. preOrder(root.left);
  49. System.out.println(root.data);
  50. preOrder(root.right);
  51. }
  52. public static void postOrder(Node root){
  53. if(root==null){
  54. return;
  55. }
  56. preOrder(root.left);
  57. preOrder(root.right);
  58. System.out.println(root.data);
  59. }


  60. public static int height(Node root){
  61. if(root==null){
  62. return 0;
  63. }
  64. else
  65.     {
  66.         int lDepth = height(root.left);
  67.         int rDepth = height(root.right);

  68.         if (lDepth > rDepth)
  69.             return (lDepth + 1);
  70.          else
  71.             return (rDepth + 1);
  72.     }
  73. }
}

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...