Giter Site home page Giter Site logo

algorithms-and-data-structures's Introduction

Common Algorithms & Data Structures in TypeScript

TypeScript

Writing Readable Code

The Internet is full of algorithms implemented in efficient ways, but written horribly in terms of separation of concerns, variable naming, etc.

This repo is my attempt to write both efficient and readable algorithms. Please let me know your thoughts.

Get started

$ yarn
$ yarn test

Use this to run the unit tests. It also watches for any changes in the current directory. So feel free to hack the code, save the file and you'll see right away if the tests pass. Exit with Ctrl+C.

Algorithms

The Min-Heap data structure is useful for inserting values in O(log N) time and getting the minimum in O(1) time. Removing the minimum is done in O(log N) time and the space complexity is, as you expect, O(N).

The data structure uses a Binary Tree in which each node's value is less than its childrens'.
Inserting a new value needs to be made such that it respects the above condition. Let's say we create a new leaf node with a given value. If it doesn't respect the condition (i.e. its value is greater than its parent's) then we swap it with its parent. We repeat the process until its value is greater than its parent's. At this point the Min-Heap is restored and the insertion process is completed.

For a Balanced Binary Tree insertion takes O(log N) time. But how can we insure we have a Balanced Tree?
We can use a Complete Binary Tree, were we always insert into and remove from the last level.

A convenient way to implement a Complete Binary Tree is using an array. Although maybe not intuitive at the first sight, there's an easy to way to calculate a node's parent and children based on its position in the array. See Complete Binary Tree in Array for more details.

Problems

algorithms-and-data-structures's People

Contributors

finnbuga avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

ultraasi-atanas

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.