Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add splaytree feature to tree #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Implementation of various data structures and algorithms in Go.
- [AVLTree](#avltree)
- [BTree](#btree)
- [BinaryHeap](#binaryheap)
- [SplayTree](#spalytree)
- [Functions](#functions)
- [Comparator](#comparator)
- [Iterator](#iterator)
Expand Down Expand Up @@ -846,6 +847,100 @@ func main() {
}
```

#### SplayTree

<b>A polymorphic splay tree implementation in Go(lang)</b>

splay is defined around the <code>SplayTree</code> interface:

type SplayTree interface {
SetRoot(n *Node)
GetRoot() *Node
Ord(key1, key2 interface{}) int
}

Your <code>SplayTree</code> implementation must contain a <code>root *Node</code> where

type Node struct {
key interface{}
value interface{}
parent *Node
left *Node
right *Node
}

<code>SetRoot</code> and <code>GetRoot</code> are the getter and setter for root, and <code>Ord</code> is a method for
comparing keys must return 0 if key1 < key2, 1 if key1 == key2, or 2 if
key1 > key2 (for whatever definitions of <, ==, and > apply to your key type).

Here's an example assuming we have the splay directory in the same directory as
our project (a very similar example is given in <i>example.go</i>):

package main

import (
"./splay"
"fmt"
"unicode/utf8"
)

type Tree struct {
root *splay.Node
}

func (T *Tree) SetRoot(n *splay.Node) {
T.root = n
}

func (T *Tree) GetRoot() *splay.Node {
return T.root
}

// This Splay Tree will use strings as keys and strings as values
// It returns 0, 1, or 2 based on the lexical comparison of the key strings
func (T *Tree) Ord(key1, key2 interface{}) int {
for i := 0; i < len(key1.(string)); {
c1, j := utf8.DecodeRuneInString(key1.(string)[i:])
c2, _ := utf8.DecodeRuneInString(key2.(string)[i:])
if c1 < c2 {
return 0
} else if c1 > c2 {
return 2
}
i += j
}
if len(key1.(string)) < len(key2.(string)) {
return 0
} else if len(key1.(string)) == len(key2.(string)) {
return 1
} else {
return 2
}
}

func main() {
T := new(Tree)

splay.Insert(T, "hello", "world")
splay.Insert(T, "foo", "bar")
splay.Insert(T, "what's your name", "buddy?")
splay.Insert(T, "Goku says", "Kamehameha")
err := splay.Insert(T, "foo", "foo")
fmt.Println(err)

v := splay.Find(T, "Goku says")
fmt.Println(v)

splay.Print(T)

splay.Delete(T, "foo")
err = splay.Delete(T, "foo")
fmt.Println(err)

splay.Print(T)
}


## Functions

Various helper functions used throughout the library.
Expand Down