From a86c2f3072eb9c18456ed15dad26285c2b815bca Mon Sep 17 00:00:00 2001 From: Karl Gaissmaier Date: Tue, 10 Jan 2023 09:15:57 +0100 Subject: [PATCH] glitch in new API --- treap.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/treap.go b/treap.go index a52cd17..b4f282b 100644 --- a/treap.go +++ b/treap.go @@ -70,18 +70,21 @@ func (n *node[T]) copyNode() *node[T] { return &m } -// Insert elements into the tree, if an element is a duplicate, it replaces the previous element. +// Insert elements into the tree, returns the new Tree. +// If an element is a duplicate, it replaces the previous element. func (t Tree[T]) Insert(items ...T) Tree[T] { + n := t.root + // something to preserve? immutable := true - if t.root == nil { + if n == nil { immutable = false } for i := range items { - t.root = t.root.insert(makeNode(items[i]), immutable) + n = n.insert(makeNode(items[i]), immutable) } - return t + return Tree[T]{root: n} } // insert into tree, changing nodes are copied, new treap is returned, old treap is modified if immutable is false. @@ -155,10 +158,13 @@ func (n *node[T]) insert(b *node[T], immutable bool) *node[T] { // Delete removes an item if it exists, returns the new tree and true, false if not found. func (t Tree[T]) Delete(item T) (Tree[T], bool) { + n := t.root + immutable := true - l, m, r := t.root.split(item, immutable) - t.root = join(l, r, immutable) + l, m, r := n.split(item, immutable) + n = join(l, r, immutable) + t.root = n if m == nil { return t, false }