Coder Social home page Coder Social logo

rbtz's Introduction

Red-black tree in Zig

The idea is that all nodes are stored adjacent to each other in linear memory with the root node always at index 0. This should allow easy serialization.

Usage

// This function should be used to compare nodes
fn comp(lhs: *const u32, rhs: *const u32) Cmp {
    if (lhs.* == rhs.*) {
        return .Equal;
    } else if (lhs.* < rhs.*) {
        return .Smaller;
    } else {
        return .Greater;
    }
}

pub fn main() void {
    const K = u32; // Type of the key
    const V = u32; // Type of the value
    const MAX_NODES = 10; // Tree is fixed to 10 nodes
    
    // Create a new tree type with u32 keys and u32 values 
    const U32Tree = RBTree(K, V, MAX_NODES, comp);
    
    // New tree instance 
    var tree = U32Tree{}; 

    // Insert a element with key = 10 and value = 100
    tree.insert(10, 100);

    // Fetch a node index
    const i = tree.get(10);

    // Get the key of the node at index i
    _ = tree.get_key(i);

    // Get the value of the node at index i
    _ = tree.get_value(i);

    // Delete the node with key = 10
    tree.delete(10);
}

rbtz's People

Contributors

r4gus avatar

Watchers

 avatar  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.