Skip to content

Commit

Permalink
glitch in new API
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 10, 2023
1 parent b1c81b9 commit a86c2f3
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions treap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit a86c2f3

Please sign in to comment.