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

Friday, April 22, 2016

Level Order Traversal in a Binary Tree from Bottom to Top

    public void LevelOrderReverse()    {
        System.out.println(LevelOrderReverse(this));
    }
    private String LevelOrderReverse(BinarySearchTree T)    {
        StringBuffer sb = new StringBuffer();
        if(T!=null)    {
            Queue<BinarySearchTree> Q = new LinkedList<BinarySearchTree>();
            Stack<BinarySearchTree> S = new Stack<BinarySearchTree>();
            Q.add(T);
            while(Q.size() > 0)    {
                BinarySearchTree C = Q.remove();
                if (C.right != null)
                    Q.add(C.right);
                if (C.left != null)
                    Q.add(C.left);
                S.push(C);
            }
            while(S.size() > 0)
                sb.append(S.pop().data).append(", ");
        }
        return sb.toString();
    }

Tuesday, December 15, 2015

Find if a Tree is a Mirror copy of another Tree

    public boolean isMirrorTrees(BinaryTree T1, BinaryTree T2)    {
        if(T1==null && T2==null)
            return true;
        if(T1.data!=T2.data)
            return false;
        if((T1==null && T2!=null) || (T2==null && T1!=null))
            return false;
        if(isMirrorTrees(T1.left, T2.right)
                && isMirrorTrees(T2.left, T1.right))
            return true;
        return false;
    }

Create a Mirror Copy of a Binary Tree

    public BinaryTree MirorCopyOfTree(BinaryTree T)    {
        BinaryTree newTree = new BinaryTree(T.data);
        if(T.left!=null)
            newTree.right=MirorCopyOfTree(T.left);
        if(T.right!=null)
            newTree.left=MirorCopyOfTree(T.right);
        return newTree;
    }

Monday, December 14, 2015

get Root to given Node path in a Binary Tree

    public void rootToNode(int data)    {
        getRootToNodePath(this, data, new StringBuffer(), false);
    }
    private void getRootToNodePath(BinarySearchTree T, int data, StringBuffer sb, boolean isFound)    {
        if(T!=null && !isFound)    {
            sb.append(T.data).append(", ");
            if(T.data==data)    {
                System.out.println(sb.toString());
                isFound = true;
                return;
            }    else    {
                getRootToNodePath(T.left, data, new StringBuffer(sb), isFound);
                getRootToNodePath(T.right, data, new StringBuffer(sb), isFound);
            }
        }

    }

Check if two Binary Tree are same

    public boolean isTreesSame(BinaryTree T1, BinaryTree T2)    {
        if(T1==null && T2==null)
            return true;
        if(T1==null || T2==null)
            return false;
        if(T1.data==T2.data
                && isTreesSame(T1.left, T2.left)
                && isTreesSame(T1.right, T2.right))
            return true;
        return false;
    }

Saturday, December 12, 2015

is Binary Tree a Binary Search Tree

    public boolean isBinarySearchTree()    {
        return isBinaryTree(this, (-10^23), 10^23);
    }
    private boolean isBinarySearchTree(BinaryTree T, int min, int max)    {
        if(T==null) return true;
        if(T.data>min && T.data<max
                && isBinaryTree(T.left, min, T.data)
                && isBinaryTree(T.right, T.data, max))
            return true;
        return false;
    }

Tuesday, November 24, 2015

Binary Tree Insertion

NOTE: This is not a BinarySearchTree

public class BinaryTree {
    BinaryTree left, right;
    int data;
    public BinaryTree(int data) {
        this.data=data;
        this.left=null;
        this.right=null;
    }
    public void insert(int data)    {
        BinaryTree T = this;
        Queue<BinaryTree> Q = new LinkedList<BinaryTree>();
        Q.add(T);
        while(Q.size()>0)    {
            BinaryTree Current = Q.remove();
            if(Current.left==null)    {
                Current.left=new BinaryTree(data);
                break;
            }
            else if(Current.right==null)    {
                Current.right=new BinaryTree(data);
                break;
            }
            else    {
                Q.add(Current.left);
                Q.add(Current.right);
            }
        }
    }

}

Sunday, November 8, 2015

Find all Root to Leaf Path of a Binary Tree

 This program uses the Binary Search Tree program available here: link
The same program can also be used for Binary Tree

    public void FindAllRoot2LeefPath()    {
        ArrList = new ArrayList<CustomLinkedList>();
        FindAllRoot2LeefPath(this, new ArrayList<Integer>());
        for(CustomLinkedList L:ArrList)    {
            L.printList();
        }
    }
   
    public ArrayList<CustomLinkedList> ArrList;
   
    private ArrayList<Integer> FindAllRoot2LeefPath(BinarySearchTree T, ArrayList<Integer> path)    {
        if(T!=null){
            path.add(T.data);
            if(T.left==null && T.right==null)    {
                CustomLinkedList L = new CustomLinkedList(path.get(0));
                L.appendList(path);
                ArrList.add(L);
                System.out.println(path);
            }    else    {
                FindAllRoot2LeefPath(T.left, new ArrayList<>(path));
                FindAllRoot2LeefPath(T.right, new ArrayList<>(path));
            }
        }
        return path;
    }
/*-----------------------------------------------------------------------------------------------*/
CustomLinkedList is a program given here: link
I added one more method there for this:

    public void appendList(ArrayList<Integer> ArrList)    {
        int size = ArrList.size();
        /*Starts from i=1 because
         * Head Element already need to be added when creating a new list*/
        for(int i=1;i<size;i++)    {
            append(ArrList.get(i));           
        }
    }

Height of a Binary Tree

This program uses the Binary Search Tree program available here: link
The same program can also be used for Binary Tree 

    public int FindHeight()    {
        return FindHeight(this);
    }
   
    private int FindHeight(BinarySearchTree T)    {
        if(T!=null)    {
            return max(FindHeight(T.left), FindHeight(T.right))+1;
        }    else return -1;
    }
   
    private int max(int a, int b)    {
        if (a>b) return a;
        else return b;
    }

Level Order Traversal or BFS in a Binary Tree

In a Binary Tree, Level Order Traversal is also known as Breadth-first search or BFS. This BFS is different from a non-Binary Tree Graph.

This program uses the Binary Search Tree program available here: link
The same program can also be used for Binary Tree

//BinarySearchTree Reffers to the link given above
     public String LevelOrder()    {
        Queue<BinarySearchTree> Q = new LinkedList<BinarySearchTree>();
        StringBuffer sb = new StringBuffer();
        Q.add(this); //Inserting Root Node to Queue
        while(Q.size()!=0)    {
            BinarySearchTree CurrentNode = Q.remove();
            sb.append(CurrentNode.data).append(", ");
            if(CurrentNode.left!=null)
                Q.add(CurrentNode.left);
            if(CurrentNode.right!=null)
                Q.add(CurrentNode.right);
        }
        return sb.toString();
    }
UA-39217154-2