Browse Source

Decrement the Tree size on Remove().

Oops, forgot to do this.
Yawning Angel 1 year ago
parent
commit
e6f0b0ee02
2 changed files with 4 additions and 1 deletions
  1. 1 0
      avl.go
  2. 3 1
      avl_test.go

+ 1 - 0
avl.go

@@ -233,6 +233,7 @@ func (t *Tree) Remove(node *Node) {
 	var parent *Node
 	var leftDeleted bool
 
+	t.size--
 	if node.left != nil && node.right != nil {
 		parent, leftDeleted = t.swapWithSuccessor(node)
 	} else {

+ 3 - 1
avl_test.go

@@ -85,13 +85,15 @@ func TestAVLTree(t *testing.T) {
 	require.Equal(nrEntries, visited, "Iterator: Backward: Visited")
 
 	// Test removal.
-	for _, idx := range rand.Perm(nrEntries) {
+	for i, idx := range rand.Perm(nrEntries) {
 		v := inOrder[idx]
 		n := tree.Find(v)
 		require.Equal(v, n.Value, "Find(): %v (Pre-remove)", v)
 		tree.Remove(n)
+		require.Equal(nrEntries-(i+1), tree.Len(), "Len(): %v (Post-remove)", v)
 
 		n = tree.Find(v)
 		require.Nil(n, "Find(): %v (Post-remove)", v)
 	}
+	require.Equal(0, tree.Len(), "Len(): After removal")
 }