Skip to content

pvva/splaytree-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repository contains splay tree implementation.

This implementation does not allow duplicate elements in the tree, so it may be used as set data type.

Usage.

    type ComparableString string

    func (sv ComparableString) CompareTo(i interface{}) int {
        if ts, ok := i.(ComparableString); ok {
            return strings.Compare(string(sv), string(ts))
        }

        return -1
    }

    ...

    tree := splaytree.NewSplayTree()
    tree.Insert(ComparableString("A"))
    tree.Insert(ComparableString("B"))
    tree.Insert(ComparableString("C"))
    ...

    if tree.Has(ComparableString("C")) {
        ...
    }
    ...

    if tree.Remove(ComparableString("B")) {
        // B was removed
        ...
    }

    tree.Traverse(func(c splaytree.Comparable, level int) bool {
        // level shows how deep in the tree the value is, root has value 0

        return canContinueTraverse // false to stop traverse
    })

About

Splay tree implementation in Go

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages