Skip to content

Commit 4b93143

Browse files
authored
Merge pull request #101 from apple-han/master
BinarySearchTree delete wrong
2 parents fe9bb1b + 4195455 commit 4b93143

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +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.val = d.val
79-
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
8079
}
8180
}
8281
return root
@@ -94,14 +93,19 @@ func (t *btree) depth() int {
9493
return _calculate_depth(t.root, 0)
9594
}
9695

97-
/*
96+
9897
func main() {
9998
t := &btree{nil}
10099
inorder(t.root)
101-
t.root = insert(t.root, 10)
100+
t.root = insert(t.root, 30)
101+
102102
t.root = insert(t.root, 20)
103103
t.root = insert(t.root, 15)
104-
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)
105109
fmt.Print(t.depth(), "\n")
106110
inorder(t.root)
107111
fmt.Print("\n")
@@ -119,4 +123,4 @@ func main() {
119123
fmt.Print("\n")
120124
fmt.Print(t.depth(), "\n")
121125
}
122-
*/
126+

0 commit comments

Comments
 (0)