What if I want to insert pair like (int,float) instead of int in btree?

package main

import (
	"github.com/google/btree"
	"fmt"
	"flag"
)

var btreeDegree = flag.Int("degree", 32, "B-Tree degree")

func main() {
	tr := btree.New(*btreeDegree)
	for i := btree.Int(0); i < 10; i++ {
		tr.ReplaceOrInsert(i)
	}

	fmt.Println("len:       ", tr.Len())
	fmt.Println("len:       ", tr.Len())
	fmt.Println("get3:      ", tr.Get(btree.Int(3)))
	fmt.Println("get100:    ", tr.Get(btree.Int(100)))
	fmt.Println("del4:      ", tr.Delete(btree.Int(4)))
	fmt.Println("del100:    ", tr.Delete(btree.Int(100)))
	fmt.Println("replace5:  ", tr.ReplaceOrInsert(btree.Int(5)))
	fmt.Println("replace100:", tr.ReplaceOrInsert(btree.Int(100)))
	fmt.Println("min:       ", tr.Min())
	fmt.Println("delmin:    ", tr.DeleteMin())
	fmt.Println("max:       ", tr.Max())
	fmt.Println("delmax:    ", tr.DeleteMax())
	fmt.Println("len:       ", tr.Len())
}

However, what if I want to insert pair instead of int?

For example, I want to insert (v1,v2,…)into the btree, and the btree is sorted by v2.

So basically I want to do CRUD on the btree(Create a node and then insert, Retrieve the node, Update the node by changing the v2,Delete the node by v1)

It will be better if it can generalize to arbitrary item and sort by certain element.

If I understand the API documentation correctly, btree stores data of type Item, which is an interface with one method:

Less(than Item) bool

So it seems you just need to create a data type that implements this method, and btree should then be able to process this type.

Disclaimer: I have not used btree yet, so I am only doing guesswork based on the docs.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.