Coder Social home page Coder Social logo

bstree's Introduction

btree

GoDoc

An efficient and simple Binary Search Tree implementation in Go.

Installing

To start using btree, install Go and run go get:

$ go get -u github.com/biningo/bstree

Usage

package main

import "log"

/**
*@Author icepan
*@Date 2020/12/2 下午4:04
*@Describe
**/
import "github.com/biningo/bstree"

type Item struct {
	Key int
	Val string
}

func byKey(a, b interface{}) int {
	ia, ib := a.(Item), b.(Item)
	return ia.Key - ib.Key
}

func main() {
	tree := bstree.NewBSTree(byKey)
	tree.Set(Item{5, "five"})
	tree.Set(Item{3, "three"})
	tree.Set(Item{7, "seven"})
	tree.Set(Item{4, "four"})
	tree.Set(Item{6, "six"})
	tree.Set(Item{10, "ten"})

	arrKey := []int{}
	arrVal := []string{}
	tree.Scan(func(item interface{}) bool {
		i := item.(Item)
		arrKey = append(arrKey, i.Key)
		arrVal = append(arrVal, i.Val)
		return true
	})

	log.Println(arrKey) // [3,4,5,6,7,10]
	log.Println(arrVal)

	f := tree.Del(Item{Key: 10})
	log.Println(f) //true
    
	if v, f := tree.Get(Item{Key: 3}); f == true {
		item := v.(Item)
		log.Println(item.Val) //"three"
	}
    
	tree.Scan(func(item interface{}) bool {
		i := item.(Item)
		log.Println(i.Key, i.Val)
		return true
	})
    // 1 one
    // 2 two
    // ....
    

	item := tree.Max().(Item)
	log.Println(item.Val) // "seven"
	item = tree.Min().(Item)
	log.Println(item.Val) // "three"

    
	arrKey = []int{}
	tree.Range(Item{Key: 4}, Item{Key: 6}, func(item interface{}) bool {
		i := item.(Item)
		arrKey = append(arrKey, i.Key)
		return true
	})
	log.Println(arrKey) // [4,5,6]



	//by key
	tree2:=bstree.NewBSTree(func(a, b interface{}) int {
		key,ok:=a.(int)
		item:=b.(Item)
		if !ok{
			key2 := a.(Item)
			return key2.Key-item.Key
		}
		return key-item.Key
	})


	tree2.Set(Item{5, "five"})
	tree2.Set(Item{3, "three"})
	tree2.Set(Item{7, "seven"})
	tree2.Set(Item{4, "four"})
	tree2.Set(Item{6, "six"})

	tree2.Range(4,6, func(item interface{}) bool {
		i:=item.(Item)
		log.Println(i.Key,i.Val)
		return true
	})
    
    tree2.Get(6)
    
	m:=tree2.Max().(Item)
	log.Println(m.Key)
}

Operations

Basic

Len()                   # return the number of items in the bstree
Set(item)               # insert or replace an existing item
Get(item)               # get an existing item
Del(item)            # delete an item

Iteration

Scan(iter)     #Scan the tree by order
Range(start,end,item) #scan the tree within the range [start,end]

Queues

Min()                   # return the first item in the bstree
Max()                   # return the last item in the bstree
TODO:PopMin()                # remove and return the first item in the TODO:PopMax()                # remove and return the last item in the bstree

Benchmarks

TODO

License

Source code is available under the MIT License.

bstree's People

Contributors

biningo avatar

Stargazers

 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.