pongad
unread,Jan 2, 2012, 7:21:20 AM1/2/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-nuts
Hi All,
I do not see any thread on this topic, so I have taken the liberty
to start my own.
I have been playing with Go for a while, and I see it does not have
an "always sorted" data structure like binary tree. Here is my idea so
far:
The tree can be constructed with
tree := tree.New(fn),
where 'fn' is a function with signature
func(a, b interface{}) (int, os.Error).
Function 'fn' should return a integer that is positive if 'a' should
be sorted after 'b', negative if 'a' should be sorted before 'b', and
zero if 'a' is logically equal to 'b'. The error can be raised if
either 'a' or 'b' does not have the type wanted by the tree (ie, if we
want a tree of ints and 'a' is a string).
Function Add(a interface{}) adds an element to the tree
Add() does nothing if there exists an element 'x' already in the tree
where fn(x, a) == 0.
Function Remove(a interface{}) and Contains(a interface{}) removes and
checks if the tree contains the element 'a' respectively.
Function 'Len() int' returns the size of the tree.
Function 'Balance()' balances the tree.
Does anyone else like this idea? :D