Git Product home page Git Product logo

t-structs's Introduction

T-Structs

t-structs is a TypeScript library consisting of standard data structures provided through a simple API.

Getting Started

Installation

npm install t-structs

Usage

Example 1 - Using a stack of numbers.

import { Stack } from "t-structs";

/** Initialize a new empty stack. */
const stack = new Stack<number>();

/** Stack is empty */
stack.size; // returns 0.
stack.isEmpty; // returns true.

/** Add elements using push method. */
stack.push(25);

/** push method can accept one or more arguments. */
stack.push(10, 11);

/** pushAll method can be used to push multiple elements at once using a list of values. */
stack.pushAll([5, 6, 7]);

/** pop method removes a value from the top of the stack and returns it. */
stack.pop(); // returns 7

/** peek method returns the value at the top of the stack without removing it from the stack. */
stack.peek(); // returns 6

/** toArray method returns a list of all the values in the stack. */
stack.toArray(); // returns [6, 5, 11, 10, 25]

Example 2 - Using a Binary Search tree with a complex custom data type.

import { BinarySearchTree } from "t-structs";
import type { CompareFunc, EqualsFunc } from "t-structs";

type Vehicle = {
  brand: string;
  cost: number;
};

const vehicles: Vehicle[] = [
  {
    brand: "Tata",
    cost: 300,
  },
  {
    brand: "Tesla",
    cost: 800,
  },
  {
    brand: "Mercedes",
    cost: 550,
  },
  {
    brand: "BMW",
    cost: 620,
  },
];

/**
 * Comparison logic for our custom Vehicle type.
 * @param {Vehicle} a
 * @param {Vehicle} b
 * @returns {1 | -1 | 0}
 */
const compare: CompareFunc<Vehicle> = function (
  a: Vehicle,
  b: Vehicle
): 1 | -1 | 0 {
  if (+a.cost > +b.cost) return 1;
  else if (+a.cost < +b.cost) return -1;
  else return 0;
};

/**
 * Equality logic for our custom Vehicle type.
 * @param {Vehicle} a
 * @param {Vehicle} b
 * @returns {boolean}
 */
const equals: EqualsFunc<Vehicle> = function (a: Vehicle, b: Vehicle): boolean {
  return a.brand === b.brand && +a.cost === +b.cost;
};

/** Initialize an empty binarySearchtree using the custom equality and compare functions. */
const binarySearchTree = new BinarySearchTree<Vehicle>([], compare, equals);

/** Initially the tree is empty. */
binarySearchTree.isEmpty; // returns true.
binarySearchTree.height; // returns 0.

/** Insert an element into the tree using insert method. */
binarySearchTree.insert({
  brand: "Porsche",
  cost: 750,
});

/** Insert method is variadic and can accept one or more arguments. */
binarySearchTree.insert(
  {
    brand: "Jeep",
    cost: 600,
  },
  {
    brand: "Toyota",
    cost: 400,
  }
);

/** Insert all method can be used to add multiple elements by passing a list as an argument. */
binarySearchTree.insertAll(vehicles);

/** has method can be used to check if a value exists in the tree. */
binarySearchTree.has({
  brand: "Mercedes",
  cost: 550,
}); // returns true.

binarySearchTree.has({
  brand: "Lamborghini",
  cost: "999",
}); // returns false.

/** remove method can be used to remove a node from the tree. */
binarySearchTree.remove({
  brand: "Mercedes",
  cost: 550,
}); // returns { brand: "Mercedes", cost: 550 } (the removed node).

/** traverse method can be used to traverse the tree. The default traversal is in_order traversal */
console.log(binarySearchTree.traverse()); /**
    Same as binarySearchTree.traverse("IN_ORDER").
    returns [
              { brand: 'Tata', cost: 300 },
              { brand: 'Toyota', cost: 400 },
              { brand: 'Jeep', cost: 600 },
              { brand: 'BMW', cost: 620 },
              { brand: 'Porsche', cost: 750 },
              { brand: 'Tesla', cost: 800 }
            ] (the in-order traversal)
*/

API Reference

Visit the t-structs reference documentation page for a more comprehensive coverage of the API.

License

t-structs is offered under the MIT License.

Note

Please note that although the library has been effectively tested, the author does not provide any correctness or performance guarantees and assurances. Feel free to incorporate and use this library in any way by your own volition.

t-structs's People

Contributors

rahulrao0209 avatar

Watchers

James Cloos 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.