types: avoid using copy node for rbtree

This commit is contained in:
c9s 2022-01-12 14:45:05 +08:00
parent c3356fa694
commit 0c7710c91b
2 changed files with 8 additions and 24 deletions

View File

@ -186,11 +186,11 @@ func (tree *RBTree) Insert(key, val fixedpoint.Value) {
var y = neel
var x = tree.Root
var node = &RBNode{
key: key,
value: val,
color: Red,
left: neel,
right: neel,
key: key,
value: val,
color: Red,
left: neel,
right: neel,
parent: neel,
}
@ -435,17 +435,6 @@ func (tree *RBTree) PostorderOf(current *RBNode, cb func(n *RBNode) bool) {
}
}
func (tree *RBTree) copyNode(node *RBNode) *RBNode {
if node == neel {
return neel
}
newNode := *node
newNode.left = tree.copyNode(node.left)
newNode.right = tree.copyNode(node.right)
return &newNode
}
func (tree *RBTree) CopyInorderReverse(limit int) *RBTree {
cnt := 0
newTree := NewRBTree()
@ -483,9 +472,3 @@ func (tree *RBTree) Print() {
return true
})
}
func (tree *RBTree) Copy() *RBTree {
newTree := NewRBTree()
newTree.Root = tree.copyNode(tree.Root)
return newTree
}

View File

@ -4,8 +4,9 @@ import (
"math/rand"
"testing"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
func TestRBTree_InsertAndDelete(t *testing.T) {
@ -94,7 +95,7 @@ func TestTree_Copy(t *testing.T) {
tree.Insert(fixedpoint.NewFromFloat(4000.0), fixedpoint.NewFromFloat(2.0))
tree.Insert(fixedpoint.NewFromFloat(2000.0), fixedpoint.NewFromFloat(3.0))
newTree := tree.Copy()
newTree := tree.CopyInorder(0)
node1 := newTree.Search(fixedpoint.NewFromFloat(2000.0))
assert.NotNil(t, node1)
assert.Equal(t, fixedpoint.NewFromFloat(2000.0), node1.key)