Skip to content

Commit

Permalink
Apply max height splay method
Browse files Browse the repository at this point in the history
To prevent the tree from becoming skewed, we applied a heuristic cal-
led the 'max height splay' method. When the size of the tree is n,
and its height exceeds 50*(log n), the deepest node is selected and
splayed to the root. To avoid excessive balancing when the tree has
few nodes, the balancing process is skipped if the number of nodes is
1000 or fewer.
Since the 'max height splay' method requires knowledge of the tree's
height, we added a height attribute to the nodes of the splay tree
and included code to check the height during testing.
  • Loading branch information
m4ushold committed Sep 1, 2024
1 parent 3f4f5d3 commit e6eb7fc
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 33 deletions.
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
98 changes: 86 additions & 12 deletions pkg/splay/splay.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Value interface {
type Node[V Value] struct {
value V
weight int
height int

left *Node[V]
right *Node[V]
Expand All @@ -45,7 +46,8 @@ 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
Expand All @@ -70,6 +72,20 @@ func (t *Node[V]) rightWeight() int {
return t.right.weight
}

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
}

// InitWeight sets initial weight of this node.
func (t *Node[V]) InitWeight() {
t.weight = t.value.Len()
Expand All @@ -92,13 +108,15 @@ 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]
nodeCount int
}

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

Expand All @@ -114,6 +132,7 @@ func (t *Tree[V]) Insert(node *Node[V]) *Node[V] {

// InsertAfter inserts the node after the given previous node.
func (t *Tree[V]) InsertAfter(prev *Node[V], node *Node[V]) *Node[V] {
t.BalancingTreeHeight()
t.Splay(prev)
t.root = node
node.right = prev.right
Expand All @@ -124,10 +143,29 @@ func (t *Tree[V]) InsertAfter(prev *Node[V], node *Node[V]) *Node[V] {
prev.parent = node
prev.right = nil

t.UpdateWeight(prev)
t.UpdateNode(prev)
t.UpdateNode(node)
t.nodeCount++
return node
}

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

return node
// MaxHeightSplay finds and splays a deapest node.
func (t *Tree[V]) MaxHeightSplay() {
node := t.root
for node.height > 1 {
if node.left != nil && node.left.height+1 == node.height {
node = node.left
} else {
node = node.right
}
}
t.Splay(node)
}

// Splay moves the given node to the root.
Expand Down Expand Up @@ -166,8 +204,26 @@ func (t *Tree[V]) Splay(node *Node[V]) {
}
}

// BalancingTreeHeight call MaxHeightSplay if n(node count) is greater than 1000 and 50*log n
func (t *Tree[V]) BalancingTreeHeight() {
if t.root != nil && t.nodeCount > 1000 {
var threshold uint64
threshold = 1
log := 1
for threshold < uint64(t.nodeCount) {
threshold *= 2
log++
}

if uint64(t.root.height) > uint64(50*log) {
t.MaxHeightSplay()
}
}
}

// IndexOf Find the index of the given node.
func (t *Tree[V]) IndexOf(node *Node[V]) int {
t.BalancingTreeHeight()
if node == nil || node != t.root && !node.hasLinks() {
return -1
}
Expand All @@ -187,6 +243,7 @@ func (t *Tree[V]) IndexOf(node *Node[V]) int {

// Find returns the Node and offset of the given index.
func (t *Tree[V]) Find(index int) (*Node[V], int, error) {
t.BalancingTreeHeight()
if t.root == nil {
return nil, 0, nil
}
Expand Down Expand Up @@ -228,9 +285,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,15 +323,29 @@ 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.height = 1

if node.left != nil && node.height < node.leftHeight()+1 {
node.height = node.leftHeight() + 1
}

if node.right != nil && 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.UpdateNode(node)
node = node.parent
}
}

// Delete deletes the given node from this Tree.
func (t *Tree[V]) Delete(node *Node[V]) {
t.BalancingTreeHeight()
t.Splay(node)

leftTree := NewTree(node.left)
Expand All @@ -300,8 +372,9 @@ func (t *Tree[V]) Delete(node *Node[V]) {

node.unlink()
if t.root != nil {
t.UpdateWeight(t.root)
t.UpdateNode(t.root)
}
t.nodeCount--
}

// DeleteRange separates the range between given 2 boundaries from this Tree.
Expand All @@ -311,6 +384,7 @@ func (t *Tree[V]) Delete(node *Node[V]) {
// but rightBoundary can be nil means range to delete includes the end of tree.
// Refer to the design document: ./design/range-deletion-in-slay-tree.md
func (t *Tree[V]) DeleteRange(leftBoundary, rightBoundary *Node[V]) {
t.BalancingTreeHeight()
if rightBoundary == nil {
t.Splay(leftBoundary)
t.cutOffRight(leftBoundary)
Expand Down Expand Up @@ -351,8 +425,8 @@ func (t *Tree[V]) rotateLeft(pivot *Node[V]) {
pivot.left = root
pivot.left.parent = pivot

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

func (t *Tree[V]) rotateRight(pivot *Node[V]) {
Expand All @@ -376,8 +450,8 @@ func (t *Tree[V]) rotateRight(pivot *Node[V]) {
pivot.right = root
pivot.right.parent = pivot

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

func (t *Tree[V]) rightmost() *Node[V] {
Expand Down
26 changes: 13 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[6,3,3]CCC[10,4,4]DDDD[15,5,5]EEEEE[19,4,6]FFFF[22,3,7]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,2]DDDD[0,0,1]EEEEE[0,0,3]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",
"[1,1,1]A[3,2,2]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 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

0 comments on commit e6eb7fc

Please sign in to comment.