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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@ballsxan/credit-balanced-bst

v0.1.1

Published

A binary search tree implementation that maintains balance based on credit values rather than height, enabling efficient credit-based search operations.

Readme

Credit Balanced Binary Search Tree

A binary search tree implementation that maintains balance based on credit values rather than height, enabling efficient credit-based search operations.

📋 Overview

This data structure combines the properties of a traditional BST (ordered by key) with a credit-based balancing mechanism. It's particularly useful for scenarios where you need to:

  • Find nodes based on cumulative credit sums in O(log n) time
  • Efficiently maintain and modify credit values
  • Perform prefix-based key operations

🏗️ Structure

CreditNode Class

class CreditNode {
    constructor(key, data, credit) {
        this.key = key;          // Sorting key
        this.data = data;        // Associated data
        this.creditSum = credit; // Sum of credits in subtree
        this.credit = credit;    // Node's own credit
        this.left = null;        // Left child
        this.right = null;       // Right child
    }
}

🚀 Features

  • Dual Balancing: Ordered by keys, balanced by credits
  • Efficient Operations: O(log n) for insert, delete, and credit-based search
  • Credit Management: Maintains cumulative sums for efficient range queries
  • Prefix Search: Find nodes by key prefixes

📖 API Reference

Constructor

const tree = new CreditBalancedBST();

Methods

insert(key, data, credit)

Inserts a new node into the tree.

delete(key)

Removes a node with the specified key.

findNodeByCredit(creditSum)

Finds the node where the cumulative credit of preceding nodes equals the specified sum.

findNodesByKey(key)

Returns all nodes with the specified key.

findNodesByPrefix(prefix = "")

Finds all nodes whose keys start with the given prefix.

updateCreditByKey(key, newCredit)

Updates the credit value for nodes with the specified key.

getTotalCredit()

Returns the sum of all credits in the tree.

isEmpty()

Checks if the tree is empty.

💡 Usage Examples

const tree = new CreditBalancedBST();

// Insert nodes with credits
tree.insert("user1", { name: "Alice" }, 10);
tree.insert("user2", { name: "Bob" }, 20);
tree.insert("user3", { name: "Charlie" }, 15);

// Find node by cumulative credit
const node = tree.findNodeByCredit(25); // Finds node where preceding credits sum to 25

// Update credits
tree.updateCreditByKey("user2", 25);

// Search by prefix
const users = tree.findNodesByPrefix("user");

// Get total credit
const total = tree.getTotalCredit(); // Returns 50

⚙️ Balancing Mechanism

The tree uses a credit-based balancing strategy that considers four possible rotations:

  • LL: Single right rotation
  • LR: Left-right double rotation
  • RR: Single left rotation
  • RL: Right-left double rotation

Each rotation is evaluated based on a cost function that minimizes credit imbalance between subtrees.

🛠️ Implementation Details

  • Credit Sum Maintenance: Each node maintains the sum of credits in its subtree
  • Recursive Operations: Insertion, deletion, and updates use recursive traversal
  • Error Checking: Includes validation for credit sum consistency
  • Memory Efficient: Only stores necessary metadata for balancing

📊 Performance

| Operation | Time Complexity | |-----------|-----------------| | Insert | O(log n) | | Delete | O(log n) | | Credit Search | O(log n) | | Key Search | O(log n) | | Prefix Search | O(log n + m) | | Credit Update | O(log n) |

🎯 Use Cases

  • Weighted Random Selection: Use credits as weights for probabilistic selection
  • Resource Allocation: Manage resources with credit-based priorities
  • Load Balancing: Distribute load based on capacity credits
  • Priority Queues: Implement efficient priority-based data structures

📝 Notes

  • Keys are used for ordering and searching
  • Credits are used for balancing and cumulative operations
  • The tree automatically rebalances after insertions, deletions, and credit updates
  • All credit modifications should use updateCreditByKey() to maintain balance

🔧 Development

The implementation uses an IIFE (Immediately Invoked Function Expression) to create a closure around private helper functions while exposing only the public API.