Giter Club home page Giter Club logo

js-avl-tree's Introduction

js-avl-tree

Build Status Coverage Status

A JavaScript implementation of the AVL tree data structure.

Features

  • 100% test coverage
  • Supports all common tree operations

Install

npm install --save @tyriar/avl-tree

Usage

// Import npm module
var AvlTree = require('@tyriar/avl-tree');

// Construct AvlTree
var tree = new AvlTree();

// Insert keys
tree.insert(1);
tree.insert(2);
tree.insert(3);
tree.insert(4);
tree.insert(5);
console.log('size: ' + tree.size());
console.log('contains 2: ' + tree.contains(2));
console.log('contains 7: ' + tree.contains(7));
// > size: 5
// > contains 2: true
// > contains 7: false

// Delete a key
tree.delete(2);
console.log('size: ' + tree.size());
console.log('contains 2: ' + tree.contains(2));
// > size: 4
// > contains 2: false

// Construct custom compare AvlTree
tree = new AvlTree(function (a, b) {
  return a.localeCompare(b);
});
tree.insert('a');
tree.insert('A');
tree.insert('b');
tree.insert('B');

// Delete the minimum key
var minKey = tree.findMinimum();
tree.delete(minKey);
console.log('minKey: ' + minKey);
console.log('new minKey: ' + tree.findMinimum());
// > min key: 'a'
// > new min key: 'A'

Operation time complexity

Operation Complexity
contains O(log n)
delete O(log n)
findMaximum O(log n)
findMinimum O(log n)
get O(log n)
insert O(log n)
isEmpty ฮ˜(1)
size ฮ˜(1)

API

AvlTree

Creates a new AVL Tree.

Parameters

  • customCompare function An optional custom compare function.

contains

Gets whether a node with a specific key is within the tree.

Parameters

  • key Object The key being searched for.

Returns boolean Whether a node with the key exists.

delete

Deletes a node with a specific key from the tree.

Parameters

  • key Object The key being deleted.

findMaximum

Returns Object The maximum key in the tree.

findMinimum

Returns Object The minimum key in the tree.

get

Gets the value of a node within the tree with a specific key.

Parameters

  • key Object The key being searched for.

Returns Object The value of the node or null if it doesn't exist.

insert

Inserts a new node with a specific key into the tree.

Parameters

  • key Object The key being inserted.
  • value Object The value being inserted.

isEmpty

Returns boolean Whether the tree is empty.

size

Returns number The size of the tree.

js-avl-tree's People

Contributors

tvnhan avatar tyriar avatar

Watchers

 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.