Everything about Binary Tree and its all traversal techniques (recursive and itterative) with examples

package org.dpq.ds.tree;import java.util.Stack;public class Tree<T>{    public static void main(String[] args) {        TreeNode<Integer> root = new TreeNode<Integer>(1);        root.setLeft(new TreeNode<Integer>(2));        root.setRight(new TreeNode<Integer>(3));        root.getLeft().setLeft(new TreeNode<Integer>(4));        root.getLeft().setRight(new TreeNode<Integer>(5));        root.getRight().setLeft(new TreeNode<Integer>(6));        root.getRight().setRight(new TreeNode<Integer>(7));        Tree<Integer> tree = new Tree<Integer>();        //Tree//         1//        / \//       2   3//      /\   /\//     4  5 6  7        //expected result for inorder(LNR) 4 2 5 1 6 3 7        //expected result for preorder(NLR) 1 2 4 5 3 6 7        //expected result for preorder(NLR) 4 5 2 6 7 3 1        System.out.println("recursive inorder \n");        tree.inOrder(root);        System.out.println("recursive preOrder \n");        tree.preOrder(root);        System.out.println("recursive postOrder \n");        tree.postOrder(root);        System.out.println(" preOrder Iterative \n");        tree.preOrderIterative(root);        System.out.println(" preOrder Iterative Another \n");        tree.preOrderIterativeAnother(root);        System.out.println(" inOrder Iterative \n");        tree.inOrderIterative(root);        System.out.println(" POSTOrderIterative \n");        tree.postOrderIterative(root);    }    void postOrderIterative(TreeNode<T> root){        if(root == null) return;        Stack<TreeNode<T>> stack1 = new Stack<>();        Stack<TreeNode<T>> stack2 = new Stack<>();        TreeNode<T> current = root;        stack1.push(current);        while( !stack1.empty() ){            current = stack1.pop();            stack2.push(current);            if(current.getLeft() != null) stack1.push(current.getLeft());            if(current.getRight() != null) stack1.push(current.getRight());        }        while(!stack2.isEmpty()) System.out.print(stack2.pop().getData());    }    void inOrderIterative(TreeNode<T> root){        if(root == null) return;        Stack<TreeNode<T>> stack = new Stack<>();        TreeNode<T> current = root;        while(current!=null || !stack.empty()){            while(current!=null){                stack.push(current);                current = current.getLeft();            }            current = stack.pop();            System.out.print(current.getData());            current=current.getRight();        }    }    void preOrderIterativeAnother(TreeNode<T> root){        if(root == null) return;        Stack<TreeNode<T>> stack = new Stack<>();        TreeNode<T> current = root;        while(current!=null || !stack.empty()){            while(current!=null){                System.out.print(current.getData());                if(current.getRight() != null) stack.push(current.getRight());                current = current.getLeft();            }            if(!stack.empty()) current = stack.pop();        }    }    void preOrderIterative(TreeNode<T> root){        if(root == null) return;        Stack<TreeNode<T>> stack = new Stack<>();        stack.push(root);        while(!stack.empty()){            TreeNode<T> myNode = stack.peek();            System.out.print(myNode.getData());            stack.pop();            if(myNode.getRight() != null) stack.push(myNode.getRight());            if(myNode.getLeft() != null) stack.push(myNode.getLeft());        }    }    void postOrder(TreeNode<T> root){        if(root == null) return;        postOrder(root.getLeft());        postOrder(root.getRight());        System.out.print(root.getData()+" ");    }    void inOrder(TreeNode<T> root){        if(root == null) return;        inOrder(root.getLeft());        System.out.print(root.getData()+" ");        inOrder(root.getRight());    }    void preOrder(TreeNode<T> root){        if(root == null) return;        System.out.print(root.getData()+" ");        preOrder(root.getLeft());        preOrder(root.getRight());    }}class TreeNode<T> {    private T data;    private TreeNode<T> left;    private TreeNode<T> right;    public TreeNode(T data){        this.data = data;    }    public TreeNode<T> getLeft() {        return left;    }    public void setLeft(TreeNode<T> left) {        this.left = left;    }    public TreeNode<T> getRight() {        return right;    }    public void setRight(TreeNode<T> right) {        this.right = right;    }    public T getData() {        return data;    }    @Override    public String toString() {        return "TreeNode{" +                "data=" + data +                ", left=" + left +                ", right=" + right +                '}';    }}

Popular posts from this blog

What is Garbage collection in Spark and its impact and resolution

Window function in PySpark with Joins example using 2 Dataframes (inner join)

Complex SQL: fetch the users who logged in consecutively 3 or more times (lead perfect example)