npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@datastructures/avl-tree

v0.0.2

Published

AVL Tree Data Structure in TypeScript

Downloads

3

Readme

@datastructures/avl-tree 🦄⛓

The AVL Tree is a Binary Search Tree that self balances when the right or left side becomes > 1 deeper than the other The self-balancing ensures that time complexity of the search method stays close to O(NlogN) Each node is a seperate object; an instance of AvlTreeNode AvlTree currently only handles numbers, but could be converted to a Generic to handle strings as well


Install


npm i @datastructures/avl-tree -D

# or

yarn add @datastructures/avl-tree -D

Usage

New AVL Trees can be created by importing the AvlTree class and setting them up similarly to the example below.

import { AvlTree } from '@datastructures/avl-tree'

const tree = new AvlTree()
tree.add(2)
tree.add(1)

console.log(tree.count()) // 2
console.log(tree.search(2)) // 2
console.log(tree.search(3)) // null

tree.inOrderTraversal(node => console.log('This node has value:', node.value))
/*
This node has value: 1
This node has value: 2
*/

API

Listed below is the AVL Tree API.

Methods

add(value): adds a AvlTreeNode with a value {number} to the tree. Rebalancing is performed automatically after the new AvlTreeNode is added.

ex: tree.add(1)

remove(value): removes a AvlTreeNode by value from the tree. Rebalancing is performed automatically after the AvlTreeNode is removed.

ex: tree.remove(1)

search(value): searches for an existing AvlTreeNode by value. If one is found, the value is returned. The tree is not modified.

ex: if (tree.search(value)) { /* do something about it */ }

preOrderTraversalRecursive(fn): envokes a callback function (fn) on each node within a tree, using "preOrder traversal" order

ex: tree.preOrderTraversalRecursive(node => console.log('This node has value:', node.value))

postOrderTraversalRecursive(fn): envokes a callback function (fn) on each node within a tree, using "postOrder traversal" order

ex: tree.postOrderTraversalRecursive(node => console.log('This node has value:', node.value))

inOrderTraversalRecursive(fn): envokes a callback function (fn) on each node within a tree, using "inOrder traversal" order

ex: tree.inOrderTraversalRecursive(node => console.log('This node has value:', node.value))

inOrderTraversal(fn): envokes a callback function (fn) on each node within a tree, using "inOrder traversal" order. This method is iterative rather than recursive, so is safer for production scenarios.

ex: tree.inOrderTraversal(node => console.log('This node has value:', node.value))

count: returns number of AvlTreeNodes currently in the tree

ex: const count = tree.count()

clear: removes all AvlTreeNodes from tree and allows them to be garbage collected

ex: tree.clear()


Resources

The list below provides links to other helpful tools for understanding the Linked List data structure.

Code Examples


View other Data Structures.