Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@datastructures-js/avl-tree

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

@datastructures-js/avl-tree

avl tree implementation in javascript

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by150%
Maintainers
1
Weekly downloads
 
Created
Source

@datastrucures-js/avl-tree

build:? npm npm npm

node's data type: string, number.

AVL Tree

Usage

const avlTree = require('@datastructures-js/avl-tree');
const avl = avlTree();

API

.node(value, parent, left, right)

creates a binary tree node with height property.

  • .setValue(value) sets the node's value.
  • .getValue() gets the node's value.
  • .setParent(parent) sets the parent node.
  • .getParent() gets the parent node.
  • .setLeft(left) sets the node's left child.
  • .getLeft() gets the node's left child.
  • .setRight(right) sets the node's right child.
  • .getRight() gets the node's right child.
  • .setHeight(height) sets the node's height.
  • .getHeight() gets the node's height.
const n = avl.node('test');
console.log(n.getValue()); // test
console.log(n.getParent()); // null
console.log(n.getLeft()); // null
console.log(n.getRight()); // null
console.log(n.getHeight()); // 1

.insert(value)

inserts a value into the tree and maintains the tree balance by rotating the imbalanced node.

avl.insert(20);
avl.insert(30);
avl.insert(40);
avl.insert(50);
avl.insert(60);
avl.insert(70);
avl.insert(80);

.root()

gets the root node

console.log(avl.root().getValue()); // 50

.min()

finds the min value node (most left).

console.log(avl.min().getValue()); // 20

.max()

finds the min value node (most right).

console.log(avl.max().getValue()); // 80

.count()

gets nodes count.

console.log(avl.count()); // 7

.find(value)

finds the value's node or returns null if not found.

let n = avl.find(30);
console.log(n.getValue()); // 30
console.log(n.getRight().getValue()); // 40
console.log(n.getLeft().getValue()); // 20
console.log(n.getHeight()); // 2

.traverseInOrder(cb)

// in-order traverse (left-parent-right)
avl.traverseInOrder(node => console.log(node.getValue()));

// 20
// 30
// 40
// 50
// 60
// 70
// 80

.traversePreOrder(cb)

// pre-order traverse (parent-left-right)
avl.traversePreOrder(node => console.log(node.getValue()));

// 50
// 30
// 20
// 40
// 70
// 60
// 80

.traversePostOrder(cb)

// post-order traverse (left-right-parent)
avl.traverse(node => console.log(node.getValue()));

// 20
// 40
// 30
// 60
// 80
// 70
// 50

.traverse(cb, order)

traverse the tree in the defined order and apply a callback on each node.

order values: inOrder, preOrder OR postOrder. default is inOrder

avl.traverse(node => console.log(node.getValue())); // in-order

// 20
// 30
// 40
// 50
// 60
// 70
// 80

avl.traverse(node => console.log(node.getValue()), 'preOrder');

// 50
// 30
// 20
// 40
// 70
// 60
// 80

.remove(value)

removes a value's node (if exists) from the tree and maintains the tree balance by rotating the imbalanced node.

console.log(bst.find(30).getValue()); // 30
bst.remove(30);
console.log(bst.find(30)); // null

.clear()

clears the tree.

avl.clear();
console.log(avl.count()); // 0

Build

grunt build

License

The MIT License. Full License is here

Keywords

FAQs

Package last updated on 16 Jul 2018

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