Skip to content

Commit 4195455

Browse files
committed
BinarySearchTree delete wrong
1 parent 9896f57 commit 4195455

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

data-structures/binary-tree/binary-search-tree.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ func bst_delete(root *node, val int) *node {
6666
root.right = bst_delete(root.right, val)
6767
} else {
6868
// this is the node to delete
69-
7069
// node with one child
7170
if root.left == nil {
72-
root = root.right
71+
return root.right
7372
} else if root.right == nil {
74-
root = root.left
73+
return root.left
7574
} else {
76-
// node with two children
77-
d := inorderSuccessor(root)
78-
root.right = bst_delete(root.right, d.val)
75+
n := root.right
76+
d := inorderSuccessor(n)
77+
d.left = root.left
78+
return root.right
7979
}
8080
}
8181
return root
@@ -93,14 +93,19 @@ func (t *btree) depth() int {
9393
return _calculate_depth(t.root, 0)
9494
}
9595

96-
/*
96+
9797
func main() {
9898
t := &btree{nil}
9999
inorder(t.root)
100-
t.root = insert(t.root, 10)
100+
t.root = insert(t.root, 30)
101+
101102
t.root = insert(t.root, 20)
102103
t.root = insert(t.root, 15)
103-
t.root = insert(t.root, 30)
104+
t.root = insert(t.root, 10)
105+
t.root = insert(t.root, 12)
106+
t.root = insert(t.root, 9)
107+
t.root = insert(t.root, 11)
108+
t.root = insert(t.root, 17)
104109
fmt.Print(t.depth(), "\n")
105110
inorder(t.root)
106111
fmt.Print("\n")
@@ -118,4 +123,4 @@ func main() {
118123
fmt.Print("\n")
119124
fmt.Print(t.depth(), "\n")
120125
}
121-
*/
126+

0 commit comments

Comments
 (0)