Showing posts with label traversal. Show all posts
Showing posts with label traversal. Show all posts

Saturday, 26 January 2019

Boundary traversal of binary tree (Border Elements)


public class PrintBoundryOftree {

    public static void main(String[] args) {
        int[] arr = {10, 5, 4, 7, 15, 14, 17};
        Node root = null;
        for (int val : arr) {
            root = insertNode(root, val);
        }
        printInorder(root);

        System.out.println("\n*** print boundry***\n");
        System.out.println("**print left**");
        printLeftBoundry(root);
        System.out.println("\n**print right boundry**\n");
        printRightBoundry(root.right); //start from right  due to root already cover in left boundry
        System.out.println("\n**leaf boundry**\n");
        printLeafBoundry(root);
    }

    static void printLeftBoundry(Node node) {
        if (null != node) {
            if (null != node.left) {
                System.out.println(node.value);
                printLeftBoundry(node.left);
            } else if (null != node.right) {
                System.out.println(node.value);
                printLeftBoundry(node.right);
            }
        }
    }

    static void printRightBoundry(Node node) {
        if (null != node) {
            if (null != node.right) {
                System.out.println(node.value);
                printRightBoundry(node.right);
            } else if (null != node.left) {
                System.out.println(node.value);
                printRightBoundry(node.left);
            }
        }
    }

    static void printLeafBoundry(Node node) {
        if (null != node) {
            printLeafBoundry(node.left);
            if (null == node.left && null == node.right) {
                System.out.println(node.value);
            }
            printLeafBoundry(node.right);

        }
    }

    static void printInorder(Node root) {
        if (null != root) {
            printInorder(root.left);
            System.out.println(root.value);
            printInorder(root.right);
        } else {
            return;
        }

    }

    static Node insertNode(Node root, Integer value) {
        Node newNode = new Node(value);
        if (root == null) {
            return newNode;
        } else {
            if (value <= root.value) {
                root.left = insertNode(root.left, value);
            } else {
                root.right = insertNode(root.right, value);
            }
            return root;
        }

    }

    static class Node {

        Integer value;
        Node left;
        Node right;

        Node(Integer value) {
            this.value = value;
            left = right = null;
        }
    }
}

output:
**original**
4
5
7
10
14
15
17

*** print boundry***

**print left**
10
5

**print right**

15

**left**

4
7
14
17

Ref: https://www.youtube.com/watch?v=uemjIijtu2I

Sunday, 14 October 2018

Give an algorithm for printing the level order data in reverse order

For example, the output for the below tree should be: 4 5 6 7 2 3 1

public class PrintDataInReverseOrder {
    public static void main(String args[]) {
             Node rootNode = new Node("1");
             rootNode.left = new Node("2");
             rootNode.left.left = new Node("4");
             rootNode.left.right = new Node("5");
             rootNode.right = new Node("3");
             rootNode.right.right = new Node("7");
             rootNode.right.left = new Node("6");
           
             printInreverse(rootNode);
    }
   
    static void printInreverse(Node node) {
       
        Queue<Node> queue = new LinkedBlockingQueue<>();
        queue.add(node);
        Stack<String> stack = new Stack<>();
        while(!queue.isEmpty()) {
            Node node1 = queue.remove();
            stack.add(node1.value);
            if(null!=node1.right) {
                queue.add(node1.right);
            }
            if(null!=node1.left) {
                queue.add(node1.left);
            }
        }
       
        while(!stack.isEmpty()) {
            System.out.print(stack.pop());
        }
    }
}

class BinayTree{
    static class Node{
        String value;
        Node left;
        Node right;
       
        Node(String val){
            value = val;
            left = right = null;
        }
       
    }
   
}

output: 4567231

Saturday, 22 September 2018

TreeTraversal(InOrder,PreOrder,PostOrder) with recursion and without recursion

public class TreeTraversal {
    public static void main(String args[]) {
         Node rootNode = new Node("4");
         rootNode.left = new Node("2");
         rootNode.left.left = new Node("1");
         rootNode.left.right = new Node("3");
         rootNode.right = new Node("6");
         rootNode.right.right = new Node("7");
         rootNode.right.left = new Node("5");
        // System.out.println(rootNode);
         System.out.println("********pre order*********");
         preorderTraversal(rootNode);
         System.out.println("\n\n********in order*********");
         inorderTraversal(rootNode);
         System.out.println("\n\n********post order*********");
         postorderTraversal(rootNode);
         System.out.println("\n\n********* pre order without recursion******");
         preOrderWithoutRecursion(rootNode);
         System.out.println("\n\n********in order without recursion********");
         inOrderWithoutRecursion(rootNode);
         System.out.println("\n\n*******post without recursion****");
         postOrderWithoutRecursion(rootNode);
    }
   
    public static void preorderTraversal(Node rootNode) {
        if(null!=rootNode) {
            System.out.print(rootNode.value);
             preorderTraversal(rootNode.left);
             preorderTraversal(rootNode.right);
        }
    }
    public static void inorderTraversal(Node rootNode) {
        if(null!=rootNode) {
            inorderTraversal(rootNode.left);
             System.out.print(rootNode.value);
             inorderTraversal(rootNode.right);
        }
    }
    public static void postorderTraversal(Node rootNode) {
        if(null!=rootNode) {
            postorderTraversal(rootNode.left);
            postorderTraversal(rootNode.right);
             System.out.print(rootNode.value);
        }
    }
   
    public static void preOrderWithoutRecursion(Node root) {
        Stack<Node> stNodes = new Stack<>();
        Node current = root;
        while(!stNodes.isEmpty() || current!=null) {
            if(null!=current) {
                stNodes.push(current);
                System.out.print(current.value);
                current = current.left;
            }else {
                Node node = stNodes.pop();
               
                current = node.right;
            }
        }
    }
   
    public static void inOrderWithoutRecursion(Node root) {
        Stack<Node> stNodes = new Stack<>();
        Node current = root;
        while(!stNodes.isEmpty() || current!=null) {
            if(null!=current) {
                stNodes.push(current);
                current = current.left;
            }else {
                Node node = stNodes.pop();
                System.out.print(node.value);
                current = node.right;
            }
        }
    }
    public static void postOrderWithoutRecursion(Node root) {
        Stack<Node> stack1 = new Stack<>();
        Stack<Node> stack2 = new Stack<>();
        Node current = root;
        stack1.push(root);
   
        while(!stack1.isEmpty() ) {
             Node node =  stack1.pop();
             stack2.push(node);
             if(null!=node.left) {
             stack1.push(node.left);
             }if(null!=node.right) {
                 stack1.push(node.right);
             }   
        }
       
        while(!stack2.isEmpty()) {
            System.out.print(stack2.pop().value);
        }
       
    }
   
}


class BinayTree{
    static class Node{
        String value;
        Node left;
        Node right;
       
        Node(String val){
            value = val;
            left = right = null;
        }
       
    }
   
}


output:
********pre order*********
4213657

********in order*********
1234567

********post order*********
1325764

********* pre order without recursion******
4213657

********in order without recursion********
1234567

*******post without recursion****
1325764

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...