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 MaxHeightSplay for the splay tree to improve its performance by reducing its skewness. #957

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,22 @@ func TestDocument(t *testing.T) {
root.SetNewArray("k1").AddInteger(1).AddInteger(2).AddInteger(3)
assert.Equal(t, 3, root.GetArray("k1").Len())
assert.Equal(t, `{"k1":[1,2,3]}`, root.Marshal())
assert.Equal(t, "[0,0]0[1,1]1[2,1]2[3,1]3", root.GetArray("k1").ToTestString())
assert.Equal(t, "[0,0,1]0[1,1,2]1[2,1,3]2[3,1,4]3", root.GetArray("k1").ToTestString())

root.GetArray("k1").Delete(1)
assert.Equal(t, `{"k1":[1,3]}`, root.Marshal())
assert.Equal(t, 2, root.GetArray("k1").Len())
assert.Equal(t, "[0,0]0[1,1]1[2,0]2[1,1]3", root.GetArray("k1").ToTestString())
assert.Equal(t, "[0,0,1]0[1,1,2]1[2,0,3]2[1,1,1]3", root.GetArray("k1").ToTestString())

root.GetArray("k1").InsertIntegerAfter(0, 2)
assert.Equal(t, `{"k1":[1,2,3]}`, root.Marshal())
assert.Equal(t, 3, root.GetArray("k1").Len())
assert.Equal(t, "[0,0]0[1,1]1[3,1]2[1,0]2[1,1]3", root.GetArray("k1").ToTestString())
assert.Equal(t, "[0,0,1]0[1,1,2]1[3,1,3]2[1,0,2]2[1,1,1]3", root.GetArray("k1").ToTestString())

root.GetArray("k1").InsertIntegerAfter(2, 4)
assert.Equal(t, `{"k1":[1,2,3,4]}`, root.Marshal())
assert.Equal(t, 4, root.GetArray("k1").Len())
assert.Equal(t, "[0,0]0[1,1]1[2,1]2[2,0]2[3,1]3[4,1]4", root.GetArray("k1").ToTestString())
assert.Equal(t, "[0,0,1]0[1,1,2]1[2,1,3]2[2,0,4]2[3,1,5]3[4,1,6]4", root.GetArray("k1").ToTestString())

for i := 0; i < root.GetArray("k1").Len(); i++ {
assert.Equal(
Expand Down
85 changes: 79 additions & 6 deletions pkg/splay/splay.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package splay

import (
"fmt"
"math"
"strings"
)

Expand All @@ -36,6 +37,7 @@ type Value interface {
type Node[V Value] struct {
value V
weight int
height int

left *Node[V]
right *Node[V]
Expand All @@ -45,12 +47,32 @@ type Node[V Value] struct {
// NewNode creates a new instance of Node.
func NewNode[V Value](value V) *Node[V] {
n := &Node[V]{
value: value,
value: value,
height: 1,
}
n.InitWeight()
return n
}

func (t *Node[V]) leftHeight() int {
if t.left == nil {
return 0
}
return t.left.height
}

func (t *Node[V]) rightHeight() int {
if t.right == nil {
return 0
}
return t.right.height
}

// InitHeight sets initial height of this node.
func (t *Node[V]) InitHeight() {
t.height = 1
}

// Value returns the value of this Node.
func (t *Node[V]) Value() V {
return t.value
Expand Down Expand Up @@ -92,13 +114,17 @@ func (t *Node[V]) hasLinks() bool {
// Tree is weighted binary search tree which is based on Splay tree.
// original paper on Splay Trees: https://www.cs.cmu.edu/~sleator/papers/self-adjusting.pdf
type Tree[V Value] struct {
root *Node[V]
root *Node[V]
splayCount int64
operationCount int64
}

// NewTree creates a new instance of Tree.
func NewTree[V Value](root *Node[V]) *Tree[V] {
return &Tree[V]{
root: root,
root: root,
splayCount: 0,
operationCount: 0,
}
}

Expand Down Expand Up @@ -126,12 +152,14 @@ func (t *Tree[V]) InsertAfter(prev *Node[V], node *Node[V]) *Node[V] {

t.UpdateWeight(prev)
t.UpdateWeight(node)
t.UpdateHeight(prev)
t.UpdateHeight(node)

return node
}

// Splay moves the given node to the root.
func (t *Tree[V]) Splay(node *Node[V]) {
// InternalSplay moves the given node to the root.
func (t *Tree[V]) InternalSplay(node *Node[V]) {
if node == nil {
return
}
Expand Down Expand Up @@ -166,6 +194,31 @@ func (t *Tree[V]) Splay(node *Node[V]) {
}
}

// Splay perform a MaxHeightSplay every sqrt(n) regular splays and do splay, to reduce the skewness of the splay tree
func (t *Tree[V]) Splay(node *Node[V]) {
t.operationCount++
t.splayCount++
if t.splayCount == int64(math.Sqrt(float64(t.operationCount))) {
t.MaxHeightSplay()
t.splayCount = 0
}
t.InternalSplay(node)
}

// MaxHeightSplay Find the deepest node and splay
func (t *Tree[V]) MaxHeightSplay() {
node := t.root
for node.left != nil || node.right != nil {
if node.left != nil && node.leftHeight()+1 == node.height {
node = node.left
} else {
node = node.right
}
}

t.InternalSplay(node)
}

// IndexOf Find the index of the given node.
func (t *Tree[V]) IndexOf(node *Node[V]) int {
if node == nil || node != t.root && !node.hasLinks() {
Expand Down Expand Up @@ -228,9 +281,10 @@ func (t *Tree[V]) ToTestString() string {

traverseInOrder(t.root, func(node *Node[V]) {
builder.WriteString(fmt.Sprintf(
"[%d,%d]%s",
"[%d,%d,%d]%s",
node.weight,
node.value.Len(),
node.height,
node.value.String(),
))
})
Expand Down Expand Up @@ -265,9 +319,23 @@ func (t *Tree[V]) UpdateWeight(node *Node[V]) {
}
}

// UpdateHeight recalculates the height of this node with the value and children.
func (t *Tree[V]) UpdateHeight(node *Node[V]) {
node.InitHeight()

if node.height < node.leftHeight()+1 {
node.height = node.leftHeight() + 1
}

if node.height < node.rightHeight()+1 {
node.height = node.rightHeight() + 1
}
}

func (t *Tree[V]) updateTreeWeight(node *Node[V]) {
for node != nil {
t.UpdateWeight(node)
t.UpdateHeight(node)
node = node.parent
}
}
Expand Down Expand Up @@ -301,6 +369,7 @@ func (t *Tree[V]) Delete(node *Node[V]) {
node.unlink()
if t.root != nil {
t.UpdateWeight(t.root)
t.UpdateHeight(t.root)
}
}

Expand Down Expand Up @@ -353,6 +422,8 @@ func (t *Tree[V]) rotateLeft(pivot *Node[V]) {

t.UpdateWeight(root)
t.UpdateWeight(pivot)
t.UpdateHeight(root)
t.UpdateHeight(pivot)
}

func (t *Tree[V]) rotateRight(pivot *Node[V]) {
Expand All @@ -378,6 +449,8 @@ func (t *Tree[V]) rotateRight(pivot *Node[V]) {

t.UpdateWeight(root)
t.UpdateWeight(pivot)
t.UpdateHeight(root)
t.UpdateHeight(pivot)
}

func (t *Tree[V]) rightmost() *Node[V] {
Expand Down
51 changes: 38 additions & 13 deletions pkg/splay/splay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ func TestSplayTree(t *testing.T) {
assert.Equal(t, 0, idx)

nodeA := tree.Insert(newSplayNode("A2"))
assert.Equal(t, "[2,2]A2", tree.ToTestString())
assert.Equal(t, "[2,2,1]A2", tree.ToTestString())
nodeB := tree.Insert(newSplayNode("B23"))
assert.Equal(t, "[2,2]A2[5,3]B23", tree.ToTestString())
assert.Equal(t, "[2,2,1]A2[5,3,2]B23", tree.ToTestString())
nodeC := tree.Insert(newSplayNode("C234"))
assert.Equal(t, "[2,2]A2[5,3]B23[9,4]C234", tree.ToTestString())
assert.Equal(t, "[2,2,1]A2[5,3,2]B23[9,4,3]C234", tree.ToTestString())
nodeD := tree.Insert(newSplayNode("D2345"))
assert.Equal(t, "[2,2]A2[5,3]B23[9,4]C234[14,5]D2345", tree.ToTestString())
assert.Equal(t, "[2,2,1]A2[5,3,2]B23[9,4,3]C234[14,5,4]D2345", tree.ToTestString())

tree.Splay(nodeB)
assert.Equal(t, "[2,2]A2[14,3]B23[9,4]C234[5,5]D2345", tree.ToTestString())
assert.Equal(t, "[2,2,1]A2[14,3,3]B23[9,4,2]C234[5,5,1]D2345", tree.ToTestString())

assert.Equal(t, 0, tree.IndexOf(nodeA))
assert.Equal(t, 2, tree.IndexOf(nodeB))
Expand All @@ -92,20 +92,20 @@ func TestSplayTree(t *testing.T) {
tree := splay.NewTree[*stringValue](nil)

nodeH := tree.Insert(newSplayNode("H"))
assert.Equal(t, "[1,1]H", tree.ToTestString())
assert.Equal(t, "[1,1,1]H", tree.ToTestString())
assert.Equal(t, 1, tree.Len())
nodeE := tree.Insert(newSplayNode("E"))
assert.Equal(t, "[1,1]H[2,1]E", tree.ToTestString())
assert.Equal(t, "[1,1,1]H[2,1,2]E", tree.ToTestString())
assert.Equal(t, 2, tree.Len())
nodeL := tree.Insert(newSplayNode("LL"))
assert.Equal(t, "[1,1]H[2,1]E[4,2]LL", tree.ToTestString())
assert.Equal(t, "[1,1,1]H[2,1,2]E[4,2,3]LL", tree.ToTestString())
assert.Equal(t, 4, tree.Len())
nodeO := tree.Insert(newSplayNode("O"))
assert.Equal(t, "[1,1]H[2,1]E[4,2]LL[5,1]O", tree.ToTestString())
assert.Equal(t, "[1,1,1]H[2,1,2]E[4,2,3]LL[5,1,4]O", tree.ToTestString())
assert.Equal(t, 5, tree.Len())

tree.Delete(nodeE)
assert.Equal(t, "[4,1]H[3,2]LL[1,1]O", tree.ToTestString())
assert.Equal(t, "[4,1,3]H[3,2,2]LL[1,1,1]O", tree.ToTestString())
assert.Equal(t, 4, tree.Len())

assert.Equal(t, tree.IndexOf(nodeH), 0)
Expand All @@ -121,7 +121,7 @@ func TestSplayTree(t *testing.T) {
tree.DeleteRange(nodes[6], nil)
assert.Equal(
t,
"[1,1]A[3,2]BB[6,3]CCC[10,4]DDDD[15,5]EEEEE[19,4]FFFF[22,3]GGG[0,0]HH[0,0]I",
"[1,1,1]A[3,2,2]BB[15,3,3]CCC[9,4,2]DDDD[5,5,1]EEEEE[19,4,4]FFFF[22,3,5]GGG[0,0,2]HH[0,0,1]I",
tree.ToTestString(),
)

Expand All @@ -131,7 +131,7 @@ func TestSplayTree(t *testing.T) {
tree.DeleteRange(nodes[2], nodes[7])
assert.Equal(
t,
"[1,1]A[3,2]BB[6,3]CCC[0,0]DDDD[0,0]EEEEE[0,0]FFFF[0,0]GGG[9,2]HH[1,1]I",
"[1,1,1]A[3,2,2]BB[6,3,4]CCC[0,0,1]DDDD[0,0,3]EEEEE[0,0,2]FFFF[0,0,1]GGG[9,2,5]HH[1,1,1]I",
tree.ToTestString(),
)

Expand All @@ -143,7 +143,7 @@ func TestSplayTree(t *testing.T) {
tree.DeleteRange(nodes[2], nodes[8])
assert.Equal(
t,
"[1,1]A[3,2]BB[6,3]CCC[0,0]DDDD[0,0]EEEEE[0,0]FFFF[0,0]GGG[0,0]HH[7,1]I",
"[3,1,2]A[2,2,1]BB[6,3,4]CCC[0,0,2]DDDD[0,0,1]EEEEE[0,0,3]FFFF[0,0,1]GGG[0,0,2]HH[7,1,5]I",
tree.ToTestString(),
)
})
Expand All @@ -155,6 +155,31 @@ func TestSplayTree(t *testing.T) {
tree.Delete(node)
assert.Equal(t, -1, tree.IndexOf(node))
})

t.Run("max height splay test", func(t *testing.T) {
tree := splay.NewTree[*stringValue](nil)

var testAnswer = []string{
"[1,1,1]A",
"[1,1,1]A[2,1,2]B",
"[1,1,1]A[2,1,2]B[3,1,3]C",
"[1,1,1]A[2,1,2]B[3,1,3]C[4,1,4]D",
"[1,1,1]A[2,1,2]B[3,1,3]C[4,1,4]D[5,1,5]E",
"[3,1,3]A[2,1,2]B[1,1,1]C[4,1,4]D[5,1,5]E[6,1,6]F",
"[3,1,3]A[2,1,2]B[1,1,1]C[4,1,4]D[5,1,5]E[6,1,6]F[7,1,7]G",
"[1,1,1]A[2,1,2]B[5,1,3]C[2,1,2]D[1,1,1]E[6,1,4]F[7,1,5]G[8,1,6]H",
"[1,1,1]A[2,1,2]B[5,1,3]C[2,1,2]D[1,1,1]E[6,1,4]F[7,1,5]G[8,1,6]H[9,1,7]I",
"[1,1,1]A[2,1,2]B[5,1,3]C[2,1,2]D[1,1,1]E[6,1,4]F[7,1,5]G[8,1,6]H[9,1,7]I[10,1,8]J",
"[9,1,7]A[4,1,4]B[3,1,3]C[2,1,2]D[1,1,1]E[6,1,5]F[1,1,1]G[8,1,6]H[1,1,1]I[10,1,8]J[11,1,9]K",
"[9,1,7]A[4,1,4]B[3,1,3]C[2,1,2]D[1,1,1]E[6,1,5]F[1,1,1]G[8,1,6]H[1,1,1]I[10,1,8]J[11,1,9]K[12,1,10]L",
"[9,1,7]A[4,1,4]B[3,1,3]C[2,1,2]D[1,1,1]E[6,1,5]F[1,1,1]G[8,1,6]H[1,1,1]I[10,1,8]J[11,1,9]K[12,1,10]L[13,1,11]M",
}

for i := 'A'; i <= 'M'; i++ {
tree.Insert(newSplayNode(string(i)))
assert.Equal(t, testAnswer[i-'A'], tree.ToTestString())
}
})
}

func makeSampleTree() (*splay.Tree[*stringValue], []*splay.Node[*stringValue]) {
Expand Down
8 changes: 4 additions & 4 deletions test/bench/document_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@ func BenchmarkDocument(b *testing.B) {
root.SetNewArray("k1").AddInteger(1).AddInteger(2).AddInteger(3)
assert.Equal(b, 3, root.GetArray("k1").Len())
assert.Equal(b, `{"k1":[1,2,3]}`, root.Marshal())
assert.Equal(b, "[0,0]0[1,1]1[2,1]2[3,1]3", root.GetArray("k1").ToTestString())
assert.Equal(b, "[0,0,1]0[1,1,2]1[2,1,3]2[3,1,4]3", root.GetArray("k1").ToTestString())

root.GetArray("k1").Delete(1)
assert.Equal(b, `{"k1":[1,3]}`, root.Marshal())
assert.Equal(b, 2, root.GetArray("k1").Len())
assert.Equal(b, "[0,0]0[1,1]1[2,0]2[1,1]3", root.GetArray("k1").ToTestString())
assert.Equal(b, "[0,0,1]0[1,1,2]1[2,0,3]2[1,1,1]3", root.GetArray("k1").ToTestString())

root.GetArray("k1").InsertIntegerAfter(0, 2)
assert.Equal(b, `{"k1":[1,2,3]}`, root.Marshal())
assert.Equal(b, 3, root.GetArray("k1").Len())
assert.Equal(b, "[0,0]0[1,1]1[3,1]2[1,0]2[1,1]3", root.GetArray("k1").ToTestString())
assert.Equal(b, "[0,0,1]0[1,1,2]1[3,1,3]2[1,0,2]2[1,1,1]3", root.GetArray("k1").ToTestString())

root.GetArray("k1").InsertIntegerAfter(2, 4)
assert.Equal(b, `{"k1":[1,2,3,4]}`, root.Marshal())
assert.Equal(b, 4, root.GetArray("k1").Len())
assert.Equal(b, "[0,0]0[1,1]1[2,1]2[2,0]2[3,1]3[4,1]4", root.GetArray("k1").ToTestString())
assert.Equal(b, "[0,0,1]0[1,1,2]1[2,1,3]2[2,0,4]2[3,1,5]3[4,1,6]4", root.GetArray("k1").ToTestString())

for i := 0; i < root.GetArray("k1").Len(); i++ {
assert.Equal(
Expand Down
Loading