Code Monkey home page Code Monkey logo

tree23's Introduction

Go Report Card

2,3-Tree

A Go library that implements a completely balanced 2,3-Tree that allows generic data as values in the tree. A 2,3-Tree self-balances itself when inserting or deleting elements and it is guaranteed, that all leaf nodes will be at a level at all times. The balancing itself runs in O(1) while ascending the tree. All nodes will be positioned at the leaf-level. Inserting/Deleting/Searching all take O(log n) time with the logarithm between base 2 and 3.

Additionally, all leaf nodes are linked to another in a sorted order (and circularly). This additionally allows accessing previous or next items (when we already have a leaf node) in O(1) without the need to further traverse the tree. An iterator is simply achieved by retrieving the smallest child (there is a function for that) and following the .Next() links.

Further, the tree allows accessing elements close to a given value (without knowing the exact leaf value!).

Documentation:

For detailed functionality and API, please have a look at the generated Go Docs: https://godoc.org/github.com/MauriceGit/tree23.

A fully functional example of creating a tree, inserting/deleting/searching:

import (
    "github.com/MauriceGit/tree23"
)

type Element struct {
    E int
}

func (e Element) Equal(e2 TreeElement) bool {
    return e.E == e2.(Element).E
}
func (e Element) ExtractValue() float64 {
    return float64(e.E)
}

func main() {
    tree := New()

    for i := 0; i < 100000; i++ {
        tree.Insert(Element{i})
    }

    // e1 will be the leaf node: 14
    e1, err := tree.FindFirstLargerLeaf(13.000001)
    // e2 will be the leaf node: 7
    e2, err := tree.Find(Element{7})
    // e3 will be the leaf node: 8
    e3, err := tree.Next(e2)
    // e4 will be the leaf node: 6
    e4, err := tree.Previous(e2)
    // smallest will be the leaf node: 0
    smallest, err := tree.GetSmallestLeaf()
    // largest will be the leaf node: 99999
    largest, err  := tree.GetLargestLeaf()

    for i := 0; i < 100000; i++ {
        tree.Delete(Element{i})
    }

    // tree is now empty.

    ...
}

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.