mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
types: avoid using copy node for rbtree
This commit is contained in:
parent
c3356fa694
commit
0c7710c91b
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user