Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 29, 2023
1 parent cbb3f1a commit affe039
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,24 +394,20 @@ func (t Tree[T]) Visit(start, stop T, visitFn func(item T) bool) {

// Clone, deep cloning of the tree structure.
func (t Tree[T]) Clone() Tree[T] {
if t.root != nil {
t.root = t.clone(t.root)
}
t.root = t.clone(t.root)
return t
}

// clone rec-descent
func (t *Tree[T]) clone(n *node[T]) *node[T] {
n = n.copyNode()

if n.left != nil {
n.left = t.clone(n.left)
}

if n.right != nil {
n.right = t.clone(n.right)
if n == nil {
return n
}
n = n.copyNode()

n.left = t.clone(n.left)
n.right = t.clone(n.right)
t.recalc(n)

return n
}

0 comments on commit affe039

Please sign in to comment.