GithubHelp home page GithubHelp logo

dsjava's Introduction

AVL Tree

public AVLNode rotateWithLeftChild() {
    AVLNode lc = left;
    left = lc.right;
    lc.right = this;
    return lc;
}

public AVLNode rotateWithRightChild() {
    AVLNode rc = right;
    right = rc.left;
    rc.left = this;
    return rc;
}
public AVLNode insert(int key) {
    ....
    ....
    ....

    balanceFactor = computeBalance();
    switch(balanceFactor) {
        case 2: //left
            if (left.balanceFactor == -1)
            {
                left = left.rotateWithRightChild();
                return rotateWithLeftChild();
            }
            else return rotateWithLeftChild();

        case -2: //right
            if (right.balanceFactor == 1)
            {
                right = right.rotateWithLeftChild();
                return rotateWithRightChild();
            }
            else return rotateWithRightChild();

    }
    return this;
}

Min Heap

  • Right Child - (2*index)+2

  • Parent - (index-1)/2)

  • Left Child - (2*index)+1

  • All nodes >= (n-1)/2 are leafs

  • index (n-1)/2 may have left child only if n is even.

private void fixHeap(int i)
{
    //if no child exists i.e. leaf, no need to fix
    if(!isLeaf(i))
        // if both children exists
        if(getRightChild(i) != -1 && getLeftChild(i) != -1)
            if(arr[i] > arr[getLeftChild(i)] || arr[i] > arr[getRightChild(i)])
            {
                if (arr[getLeftChild(i)] < arr[getRightChild(i)])
                {
                    swap(i,getLeftChild(i));
                    fixHeap(getLeftChild(i));
                }
                else
                {
                    swap(i,getRightChild(i));
                    fixHeap(getRightChild(i));
                }
            }
        //if right doesn't exist (there is no case where left doesnt exist, but right exists)
        else if(getRightChild(i) == -1 && arr[i] > arr[getLeftChild(i)])
        {
            swap(i,getLeftChild(i));
            fixHeap(getLeftChild(i));
        }
}
public void buildHeap()
{
    for(int i=(size-2)/2; i>=0; i--)
        fixHeap(i);
}

Graph

DFS

public int dfs(int visitCount) {
    System.out.print(label + " ");
    visited = true;

    for (int j=0; j<adjList.size(); j++)
        if ( !adjList.get(j).visited )
           adjList.get(j).dfs();

    return visitCount;
}
public void dfs() {
    for(int i=0; i<size; i++) {
        if (!node[i].visited )
           node[i].dfs(++visitCount);
    }
    System.out.println();
}

BFS

public void bfs() {
    ArrayDeque<GraphNode> q = new ArrayDeque<GraphNode>();
    for (int i=0; i<size; i++)
        if ( !node[i].visited ){
            q.add(node[i]);
            node[i].visited = true;

            while ( !q.isEmpty() ) {
                GraphNode curr = q.remove();
                System.out.print(curr.label + " ");

                for (int j=0; j<curr.adjList.size(); j++) {
                    GraphNode y = curr.adjList.get(j);
                    if ( !y.visited ) {
                        q.add(y);
                        y.visited = true;
                    }
                }
            }
        }
}

Disjoint Set

public DSNode getRoot() {
    if (parent == null)
        return this;
    else
        return parent.getRoot();
}
public void union(int parent, int child) {
    DSNode p = findParent(parent);
    DSNode c = findParent(child);
    if ( p != c )
        c.parent = p;
}

Hash Table

int hash(int x) { return (x*x*x + 3*x*x + 1) % nodes.length; }
public void put(int key, int value) {
    int index = hash(key);
    HashNode node = new HashNode(key,value);
    if(nodes[index] == null)
        nodes[index] = new LinkedList<>();
    nodes[index].add(node);
}
public int get(int key) {
    LinkedList<HashNode> list = nodes[hash(key)];
    if(list != null)
        for(int i=0; i<list.size(); i++)
            if(list.get(i).key == key)
                return list.get(i).value;
    return Integer.MAX_VALUE;
}

dsjava's People

Contributors

aswinshenoy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.