/algorithms-and-data-structures

My implementation of algorithms and data structures

Primary LanguageJavaMIT LicenseMIT

Algorithms and data structures in java

Code organization

Each algorithm is implemented on a single file (I didn't split the classes on separate files) so you can run each algorithm from a single file, which make it easy to run it on CoderPad, for example, or by just copy/pasting any file in your java environment (independently from each other) without any external dependency.

Tools

Drawing a graph on the browser

GraphvizOnline is a tool for graph drawing. There are many tools like this online, all based on Graphviz.

You can specify a tree like this:

digraph {
    C;
    B -> C;
    B -> D;
 }

which outputs this image:

Demo Digraph

Also, a linked list:

digraph {
    node [shape=record];
    node0[label="<data> 12 | <pointer> next"]
    node2[label="<data>  1 | <pointer> next"]
    node0:pointer -> node2:data
}

Demo Digraph 2

and you can even draw a fancier version, like this:

digraph g {
    rankdir=LR;
    node [shape=record];
    a [label="{ <data> 12 | <pointer>  }", width=1.2]
    b [label="{ <data> 1 | <pointer>  }"];
    c [label="{ <data> 7 | <pointer>  }"];
    d [shape=box];
    a:pointer:c -> b:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false, arrowsize=1.2];
    b:pointer:c -> c:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
    c:pointer:c -> d      [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
}

Demo Digraph 3

Draw.io is more manual, for general purpose drawing, but could be useful in some cases.

Interactive construction of data structures

I've used this interactive tool:

https://visualgo.net/en/heap

also, this one:

https://www.cs.usfca.edu/~galles/visualization/Heap.html

Tests

  • I implemented the test cases the main function of each class.
  • There is always a full test coverage, without any external dependencies
  • The algorithms are implemented using the TDD methodology (write tests first, then implement the code that passes the tests). TDD is suitable for this case because the requirements are well known.
  • Everything is on this single file (to make it easy to reproduce in codepad, for example)
  • I use an online graph renderer called digraph dot (graphviz) to represent the graph.