New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

quick-avl

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

quick-avl

AVL tree: a self-balancing binary search tree

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

quick-avl

A Typescript implementation of an AVL tree, which is a self-balancing binary search tree.

Implements the Map interface.

Named after the inventors Adelson-Velsky and Landis, an AVL tree enforces an invariant where the heights of the subtrees of a node differ by at most one. Rebalancing is performed after each insert or remove operation, if that operation made the tree imbalanced.

Installation

npm install quick-avl

Performance

The main reason of using an AVL tree is performance. Because of its self-balancing property, worst case lookup is O(log(n)), compared to the plain binary search trees where this is O(n).

OperationAverage time complexityWorst case complexity
findO(log n)O(log n)
insertO(log n)O(log n)
removeO(log n)O(log n)
traversalO(n)O(n)

Usage

This AVL tree implementation acts like a map to store key-value pairs in.

import { AvlTree } from 'quick-avl';

const users = new AvlTree<number, string>();

users.set(100, 'Bob').set(200, 'Carol').set(0, 'Alice');

users.get(100); // --> 'Bob'
users.has(100); // --> true

users.delete(200); // --> true

users.valueList(); // --> ['Alice', 'Carol']

for (const [key, value] of users) {
  console.log(`Key: ${key}, value: ${value}`);
}

Why another AVL library

While there are some excellent AVL libraries available within NPM, these libraries swap out tree node values while performing tree balancing. I required an AVL library that does not replace keys or values within a node. That way, a reference to a tree node will always keep the same value.

Keywords

FAQs

Package last updated on 14 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc