Binary Search Tree

package me.premaseem.datastructure.binarySearchTree;


class Node {
    Node left, right;
    int data;

    public Node(int d) {
        data = d;
    }

}

public class BinarySearchTree {

    Node root;
    // Driver Program to test above functions
    public static void main(String[] args) {
        BinarySearchTree tree = new BinarySearchTree();

        /* Let us create following BST
              50
           /     \
          30      70
         /  \    /  \
       20   40  60   80 */

        tree.insert(50);
        tree.insert(30);
        tree.insert(20);
        tree.insert(40);
        tree.insert(70);
        tree.insert(60);
        tree.insert(80);

//      print inorder traversal of the BST
        tree.inOrder();
        
        tree.search(100);
        tree.search(30);
        
        tree.delete(30);
        tree.delete(30);
        tree.delete(50);
        
        System.out.println();
        tree.inOrder();
    }

    private void delete(int d) {
        root = deleteR(root,d);
    }

    private Node deleteR(Node n ,int d) {
        if(n == null){
            return n;
        }
        if(d < n.data){
            n.left = deleteR(n.left,d);
        }else if(d > n.data){
            n.right = deleteR(n.right,d);
        } else {
            // 1 or all nodes are null
            if(n.left == null){
                return n.right;
            }else if (n.right == null){
                return n.left;
            }

            n.data = findMin(n.right);
            n.right = deleteR(n.right,n.data);
            // if both node are not null
        }
        return n;

    }

    int findMin(Node n){
        int min = n.data;
        if(n.left != null){
            min = n.data;
            n = n.left;
        }
        return min;
    }


    private void insert(int d) {
        root = insertR(root,d);
    }

    private Node insertR(Node n ,int d) {
        if(n == null){
            return new Node(d);
        }
        if(d < n.data){
            n.left = insertR(n.left, d);
        }else {
            n.right = insertR(n.right, d);
        }
        return n;
    }

    void inOrder(){
        inOrderR(root);
    }

    private void inOrderR(Node n) {
        if(n == null){
            return;
        }
        inOrderR(n.left);
        System.out.print(" >> "+ n.data);
        inOrderR(n.right);
    }

    void search(int d){
        searchR(root, d);
    }

    private void searchR(Node n, int d) {
        if(n == null){
            System.out.println("not found ");
            return;
        }

        if(d == n.data){
            System.out.println("Element found 🙂 ");
            return;
        }
        if(d < n.data){
            searchR(n.left, d);
        }else {
            searchR(n.right,d);
        }

    }
}

Github Code Repository for coding challenge: 
https://github.com/premaseem/AlgoAndDataStructureInJava/blob/master/src/me/premaseem/datastructure/binarySearchTree/BinarySearchTree.java

Youtube playlist for more video like this:


## Follow me on
LinkedIn: http://in.linkedin.com/in/premaseem
Facebook: https://www.facebook.com/premaseem
Twitter:  https://twitter.com/premaseem
My Blog: https://premaseem.wordpress.com

Selection sort explained

Youtube playlist for more video like this:https://www.youtube.com/watch?v=jzVoFUwlMZg&list=PL13Vva6TJcSsZtUipeopvYnNd0zzg4wbT
Github Code Repository for coding challenge: https://github.com/premaseem/AlgoAndDataStructureInJava

For details visit: https://premaseem.wordpress.com/category/coding-challenge/
Follow me on YouTube: https://www.youtube.com/user/premaseemutube
LinkedIn: https://www.linkedin.com/in/premaseem
Twitter: https://twitter.com/premaseem

Bubble sort algorithm explained

Youtube playlist for more video like this:https://www.youtube.com/watch?v=jzVoFUwlMZg&list=PL13Vva6TJcSsZtUipeopvYnNd0zzg4wbT
Github Code Repository for coding challenge: https://github.com/premaseem/AlgoAndDataStructureInJava

For details visit: https://premaseem.wordpress.com/category/coding-challenge/
Follow me on YouTube: https://www.youtube.com/user/premaseemutube
LinkedIn: https://www.linkedin.com/in/premaseem
Twitter: https://twitter.com/premaseem

Insertion sort explained in detail

Youtube playlist for more video like this:https://www.youtube.com/watch?v=jzVoFUwlMZg&list=PL13Vva6TJcSsZtUipeopvYnNd0zzg4wbT
Github Code Repository for coding challenge: https://github.com/premaseem/AlgoAndDataStructureInJava

For details visit: https://premaseem.wordpress.com/category/coding-challenge/
Follow me on YouTube: https://www.youtube.com/user/premaseemutubeLinkedIn: https://www.linkedin.com/in/premaseemTwitter: https://twitter.com/premaseem

If you have a better/ different solution, please share your solution in the comment. 

Circular queue implementation using linked list in java

#datastructure #codingchallenge 

Need to build a circular queue, first item will get overwritten when the queue overflows or the capacity finishes. In normal sense, newest item would replace the oldest item.  

Circular Queue is also a linear data structure, which follows the principle of FIFO(First In First Out),

but instead of ending the queue at the last position, it again starts from the first position after the last,

hence making the queue behave like a circular data structure.

Youtube playlist for more coding challenge like this:

Github Code Repository for coding challenge:

https://github.com/premaseem/AlgoAndDataStructureInJava/tree/master/src/codechallenge

For details visit:

https://premaseem.wordpress.com/category/coding-challenge/

If you have a better/ different solution, please share your solution in the comment.