Skip to content

update fork #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 49 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
e359b7c
test data coverage InterpolationSearch
tuanlinhtl17 Sep 27, 2019
4a49b1a
make methods public or private
williamfiset Sep 28, 2019
6975fa2
Fix fft
williamfiset Sep 28, 2019
4036daa
public/private methods
williamfiset Sep 28, 2019
c935ed2
public/private methods
williamfiset Sep 28, 2019
90a6f2a
Fix broken link
williamfiset Sep 28, 2019
415fdfc
public/private refactor
williamfiset Sep 28, 2019
8fd6c10
Added TODOs
williamfiset Sep 28, 2019
b10081e
Merge pull request #89 from tuanlinhtl17/master
williamfiset Sep 28, 2019
a116630
Added private/public
williamfiset Sep 28, 2019
5af74f5
Merge branch 'master' of https://github.com/williamfiset/algorithms
williamfiset Sep 28, 2019
b6ea57a
Tree work
williamfiset Sep 29, 2019
a3446ef
formatting
williamfiset Sep 29, 2019
466a762
Update README.md
williamfiset Sep 29, 2019
a5b524a
Trees
williamfiset Sep 29, 2019
5b27c47
Merge branch 'master' of https://github.com/williamfiset/algorithms
williamfiset Sep 29, 2019
5ed633b
Trees
williamfiset Sep 29, 2019
13b776f
Trees
williamfiset Sep 29, 2019
ebd762c
Trees
williamfiset Sep 29, 2019
61884d9
Trees
williamfiset Sep 29, 2019
e44ba63
Trees
williamfiset Sep 29, 2019
9a4634f
Trees
williamfiset Oct 1, 2019
fd286bc
Trees
williamfiset Oct 1, 2019
88fe2cf
Trees
williamfiset Oct 1, 2019
7588cae
Trees
williamfiset Oct 1, 2019
fd9fbd0
Trees
williamfiset Oct 1, 2019
ccd5935
Trees
williamfiset Oct 3, 2019
aa069fc
Trees
williamfiset Oct 3, 2019
ffe5d35
Trees
williamfiset Oct 4, 2019
d89cc37
Trees
williamfiset Oct 4, 2019
1f21fc7
Trees
williamfiset Oct 4, 2019
bd5f367
Trees
williamfiset Oct 5, 2019
f6b15cb
Trees
williamfiset Oct 5, 2019
ec79be1
Trees
williamfiset Oct 6, 2019
80fb7aa
Trees
williamfiset Oct 6, 2019
d69be37
Trees
williamfiset Oct 6, 2019
6d27ff9
Trees
williamfiset Oct 6, 2019
fc722eb
Trees
williamfiset Oct 9, 2019
960e01a
Trees
williamfiset Oct 10, 2019
2967c19
Trees
williamfiset Oct 11, 2019
d92e29b
Trees
williamfiset Oct 13, 2019
f10c5d6
Trees
williamfiset Oct 13, 2019
c12f30b
Trees
williamfiset Oct 13, 2019
4320b81
Trees
williamfiset Oct 13, 2019
b00a55a
Tree algorithm slides
williamfiset Oct 13, 2019
05dbe78
Tree algorithm slides
williamfiset Oct 13, 2019
fee9632
formatting
williamfiset Oct 14, 2019
1b20f5a
formatting
williamfiset Oct 14, 2019
0c4e37d
formatting
williamfiset Oct 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

Algorithms and data structures are amongst the most fundamental ingredients in the recipe for efficient code and good software design; knowledge of how to create and design excellent algorithms is an essential skill required in becoming an exemplary programmer. The goal of this repository is to demonstrate how to correctly implement the most common data structures and algorithms in the simplest and most elegant ways.

# Contributing

This repository is contribution friendly :smiley:. If you're an algorithms enthusiast and want to add or improve an algorithm your contribution is welcome! Please be sure to checkout the [Wiki](https://github.com/williamfiset/Algorithms/wiki) for instructions.


# Data Structures
* [:movie_camera:](https://www.youtube.com/watch?v=q4fnJZr8ztY)[Balanced Trees](https://github.com/williamfiset/algorithms/tree/master/com/williamfiset/algorithms/datastructures/balancedtree)
* [Avl Tree (recursive)](https://github.com/williamfiset/algorithms/blob/master/com/williamfiset/algorithms/datastructures/balancedtree/AVLTreeRecursive.java)
Expand Down Expand Up @@ -76,7 +81,7 @@ Algorithms and data structures are amongst the most fundamental ingredients in t
* [Convex polygon area](https://github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/geometry/ConvexPolygonArea.java) **- O(n)**
* [Convex polygon cut](https://github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/geometry/ConvexPolygonCutWithLineSegment.java) **- O(n)**
* [Convex polygon contains points](https://github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/geometry/ConvexPolygonContainsPoint.java) **- O(log(n))**
* [Coplanar points test (are four 3D points on the same plane)](https://github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/geometry/CoplanarPointsTest.java) **- O(1)**
* [Coplanar points test (are four 3D points on the same plane)](https://github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/geometry/CoplanarPoints.java) **- O(1)**
* [Line class (handy infinite line class)](https://github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/geometry/Line.java) **- O(1)**
* [Line-circle intersection point(s)](https://github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/geometry/LineCircleIntersection.js) **- O(1)**
* [Line segment-circle intersection point(s)](https://github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/geometry/LineSegmentCircleIntersection.js) **- O(1)**
Expand Down Expand Up @@ -203,10 +208,6 @@ Algorithms and data structures are amongst the most fundamental ingredients in t
* [Rabin-Karp algorithm (finds pattern matches in text)](https://github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/strings/RabinKarp.java) **- O(n+m)**
* [Substring verification with suffix array](https://github.com/williamfiset/Algorithms/blob/master/com/williamfiset/algorithms/strings/SubstringVerificationSuffixArray.java) **- O(nlog(n)) SA construction and O(mlog(n)) per query**

# Contributing

This repository is contribution friendly :smiley:. If you're an algorithms enthusiast and want to add or improve an algorithm your contribution is welcome! Please be sure to checkout the [Wiki](https://github.com/williamfiset/Algorithms/wiki) for instructions.

# License

This repository is released under the [MIT license](https://opensource.org/licenses/MIT). In short, this means you are free to use this software in any personal, open-source or commercial projects. Attribution is optional but appreciated.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
package com.williamfiset.algorithms.datastructures.linkedlist;

public class DoublyLinkedList<T> implements Iterable<T> {

private int size = 0;
private Node<T> head = null;
private Node<T> tail = null;

// Internal node class to represent data
private class Node<T> {
T data;
Node<T> prev, next;
private static class Node<T> {
private T data;
private Node<T> prev, next;

public Node(T data, Node<T> prev, Node<T> next) {
this.data = data;
Expand Down Expand Up @@ -58,8 +57,6 @@ public void add(T elem) {

// Add a node to the tail of the linked list, O(1)
public void addLast(T elem) {

// The linked list is empty
if (isEmpty()) {
head = tail = new Node<T>(elem, null, null);
} else {
Expand All @@ -71,15 +68,12 @@ public void addLast(T elem) {

// Add an element to the beginning of this linked list, O(1)
public void addFirst(T elem) {

// The linked list is empty
if (isEmpty()) {
head = tail = new Node<T>(elem, null, null);
} else {
head.prev = new Node<T>(elem, null, head);
head = head.prev;
}

size++;
}

Expand All @@ -97,8 +91,7 @@ public T peekLast() {

// Remove the first value at the head of the linked list, O(1)
public T removeFirst() {

// Can't remove data from an empty list -_-
// Can't remove data from an empty list
if (isEmpty()) throw new RuntimeException("Empty list");

// Extract the data at the head and move
Expand All @@ -119,8 +112,7 @@ public T removeFirst() {

// Remove the last value at the tail of the linked list, O(1)
public T removeLast() {

// Can't remove data from an empty list -_-
// Can't remove data from an empty list
if (isEmpty()) throw new RuntimeException("Empty list");

// Extract the data at the tail and move
Expand All @@ -141,7 +133,6 @@ public T removeLast() {

// Remove an arbitrary node from the linked list, O(1)
private T remove(Node<T> node) {

// If the node to remove is somewhere either at the
// head or the tail handle those independently
if (node.prev == null) return removeFirst();
Expand All @@ -166,26 +157,30 @@ private T remove(Node<T> node) {

// Remove a node at a particular index, O(n)
public T removeAt(int index) {

// Make sure the index provided is valid -_-
if (index < 0 || index >= size) throw new IllegalArgumentException();
// Make sure the index provided is valid
if (index < 0 || index >= size) {
throw new IllegalArgumentException();
}

int i;
Node<T> trav;

// Search from the front of the list
if (index < size / 2) {
for (i = 0, trav = head; i != index; i++) trav = trav.next;

for (i = 0, trav = head; i != index; i++) {
trav = trav.next;
}
// Search from the back of the list
} else for (i = size - 1, trav = tail; i != index; i--) trav = trav.prev;
} else
for (i = size - 1, trav = tail; i != index; i--) {
trav = trav.prev;
}

return remove(trav);
}

// Remove a particular value in the linked list, O(n)
public boolean remove(Object obj) {

Node<T> trav = head;

// Support searching for null
Expand All @@ -210,16 +205,23 @@ public boolean remove(Object obj) {

// Find the index of a particular value in the linked list, O(n)
public int indexOf(Object obj) {

int index = 0;
Node<T> trav = head;

// Support searching for null
if (obj == null) {
for (; trav != null; trav = trav.next, index++) if (trav.data == null) return index;

for (; trav != null; trav = trav.next, index++) {
if (trav.data == null) {
return index;
}
}
// Search for non null object
} else for (; trav != null; trav = trav.next, index++) if (obj.equals(trav.data)) return index;
} else
for (; trav != null; trav = trav.next, index++) {
if (obj.equals(trav.data)) {
return index;
}
}

return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion com/williamfiset/algorithms/dp/EditDistance.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class EditDistance {

// Computes the cost to convert a string 'a' into a string 'b' using dynamic
// programming given the insertionCost, deletionCost and substitutionCost, O(nm)
static int editDistance(
public static int editDistance(
String a, String b, int insertionCost, int deletionCost, int substitutionCost) {

final int AL = a.length(), BL = b.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class AngleBetweenVectors2D {

// Return the smaller of the two angles between two 2D vectors in radians
static double angleBetweenVectors(double v1x, double v1y, double v2x, double v2y) {
public static double angleBetweenVectors(double v1x, double v1y, double v2x, double v2y) {

// To determine the angle between two vectors v1 and v2 we can use
// the following formula: dot(v1,v2) = len(v1)*len(v2)*cosθ and solve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class AngleBetweenVectors3D {

// Return the smaller of the two angles between two 3D vectors in radians
static double angleBetweenVectors(
public static double angleBetweenVectors(
double v1x, double v1y, double v1z, double v2x, double v2y, double v2z) {
// To determine the angle between two vectors v1 and v2 we can use
// the following formula: dot(v1,v2) = len(v1)*len(v2)*cosθ and solve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CircleCircleIntersectionArea {
// Due to double rounding precision the value passed into the acos
// function may be outside its domain of [-1, +1] which would return
// the value Double.NaN which we do not want.
static double arccosSafe(double x) {
private static double arccosSafe(double x) {
if (x >= +1.0) return 0;
if (x <= -1.0) return PI;
return acos(x);
Expand Down Expand Up @@ -142,7 +142,7 @@ public static Point2D[] circleCircleIntersection(Point2D c1, double r1, Point2D
// Rotate point 'pt' a certain number of radians clockwise
// relative to some fixed point 'fp'. Note that the angle
// should be specified in radians, not degrees.
public static Point2D rotatePoint(Point2D fp, Point2D pt, double angle) {
private static Point2D rotatePoint(Point2D fp, Point2D pt, double angle) {

double fpx = fp.getX();
double fpy = fp.getY();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static double triangleArea(
}

// Return the smaller of the two angles between two 2D vectors in radians
static double angleBetweenVectors(double v1x, double v1y, double v2x, double v2y) {
private static double angleBetweenVectors(double v1x, double v1y, double v2x, double v2y) {

// To determine the angle between two vectors v1 and v2 we can use
// the following formula: dot(v1,v2) = len(v1)*len(v2)*cosθ and solve
Expand Down
27 changes: 13 additions & 14 deletions com/williamfiset/algorithms/geometry/ClosestPairOfPoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@

import java.util.*;

// Custom point class
class PT {
double x, y;
public class ClosestPairOfPoints {

public PT(double xx, double yy) {
x = xx;
y = yy;
}
private static final double EPS = 1e-9;

public double dist(PT pt) {
double dx = x - pt.x, dy = y - pt.y;
return sqrt(dx * dx + dy * dy);
}
}
public class PT {
double x, y;

public class ClosestPairOfPoints {
public PT(double xx, double yy) {
x = xx;
y = yy;
}

static final double EPS = 1e-9;
public double dist(PT pt) {
double dx = x - pt.x, dy = y - pt.y;
return sqrt(dx * dx + dy * dy);
}
}

// Sorts points by X coordinate
private static class X_Sort implements Comparator<PT> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class ConvexHullGrahamScan {

// Construct a convex hull and return it as a stack of points
static Stack<Point2D> createConvexHull(Point2D[] pts) {
public static Stack<Point2D> createConvexHull(Point2D[] pts) {
int k1, k2, N = pts.length;
Stack<Point2D> hull = new Stack<Point2D>();
Arrays.sort(pts, new PointOrder());
Expand Down Expand Up @@ -43,7 +43,7 @@ static Stack<Point2D> createConvexHull(Point2D[] pts) {
}

// Compare other points relative to polar angle (between 0 and 2*PI) they make with this point
static class PolarOrder implements Comparator<Point2D> {
private static class PolarOrder implements Comparator<Point2D> {
Point2D pt;

public PolarOrder(Point2D pt) {
Expand All @@ -65,7 +65,7 @@ else if (dy1 == 0 && dy2 == 0) {
}

// Put lower Y co-ordinates first, with a lower X value in the case of ties
static class PointOrder implements Comparator<Point2D> {
private static class PointOrder implements Comparator<Point2D> {
@Override
public int compare(Point2D q1, Point2D q2) {
if (q1.getY() < q2.getY()) return -1;
Expand All @@ -80,7 +80,7 @@ public int compare(Point2D q1, Point2D q2) {

// Check to see whether the points are ordered clockwise or counter-clockwise (0 indicates that
// they are collinear)
static int collinear(Point2D a, Point2D b, Point2D c) {
private static int collinear(Point2D a, Point2D b, Point2D c) {
double area =
(b.getX() - a.getX()) * (c.getY() - a.getY())
- (b.getY() - a.getY()) * (c.getX() - a.getX());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static boolean containsPoint(Point2D[] hull, Point2D p) {
// it is on the line, -1 if c is to the right of the line and +1 if it's
// to the left from the frame of reference of standing at point a
// and facing point b.
public static int collinear(Point2D a, Point2D b, Point2D c) {
private static int collinear(Point2D a, Point2D b, Point2D c) {
double ax = a.getX(), ay = a.getY();
double bx = b.getX(), by = b.getY();
double cx = c.getX(), cy = c.getY();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ConvexPolygonCutWithLineSegment {
private static final double EPS = 1e-9;

// Simple 2D point class.
static class Pt {
public static class Pt {
double x, y;

public Pt(double x, double y) {
Expand All @@ -31,7 +31,7 @@ public String toString() {
// Cuts a convex polygon by a specified line and returns one part
// of the polygon (swapping the endpoints p1 and p2 of the line
// will return the other part of the polygon).
static Pt[] cut(Pt[] poly, Pt p1, Pt p2) {
public static Pt[] cut(Pt[] poly, Pt p1, Pt p2) {
int n = poly.length;
List<Pt> res = new ArrayList<>();
for (int i = 0, j = n - 1; i < n; j = i++) {
Expand All @@ -44,7 +44,7 @@ static Pt[] cut(Pt[] poly, Pt p1, Pt p2) {
return res.toArray(new Pt[res.size()]);
}

static Pt intersect(
private static Pt intersect(
double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
double a1 = y2 - y1, a2 = y4 - y3, b1 = x1 - x2, b2 = x3 - x4;
double c1 = -(x1 * y2 - x2 * y1), c2 = -(x3 * y4 - x4 * y3);
Expand All @@ -53,7 +53,7 @@ static Pt intersect(
return new Pt(x, y);
}

static int orientation(double ax, double ay, double bx, double by, double cx, double cy) {
private static int orientation(double ax, double ay, double bx, double by, double cx, double cy) {
bx -= ax;
by -= ay;
cx -= ax;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

import static java.lang.Math.*;

public class CoplanarPointsTest {
public class CoplanarPoints {

private static final double EPS = 1e-7;

// A simple 3D vector class
static class Vector {
public static class Vector {
double x, y, z;

public Vector(double xx, double yy, double zz) {
Expand All @@ -24,19 +24,6 @@ public Vector(double xx, double yy, double zz) {
}
}

// Cross product of two vectors
static Vector cross(Vector v1, Vector v2) {
double v3x = v1.y * v2.z - v1.z * v2.y;
double v3y = v1.z * v2.x - v1.x * v2.z;
double v3z = v1.x * v2.y - v1.y * v2.x;
return new Vector(v3x, v3y, v3z);
}

// 3D vector dot product
static double dot(Vector v1, Vector v2) {
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
}

// Determine is four points (ax,ay,az), (bx,by,bz), (cx,cy,cz),
// (dx,dy,dz) all lie in the same plane in 3D space
public static boolean coplanar(
Expand Down Expand Up @@ -75,6 +62,19 @@ public static boolean coplanar(
return abs(dot(v3, v4)) < EPS;
}

// Cross product of two vectors
private static Vector cross(Vector v1, Vector v2) {
double v3x = v1.y * v2.z - v1.z * v2.y;
double v3y = v1.z * v2.x - v1.x * v2.z;
double v3z = v1.x * v2.y - v1.y * v2.x;
return new Vector(v3x, v3y, v3z);
}

// 3D vector dot product
private static double dot(Vector v1, Vector v2) {
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
}

// Examples
public static void main(String[] args) {

Expand Down
Loading