@@ -66,17 +66,16 @@ func bst_delete(root *node, val int) *node {
66
66
root .right = bst_delete (root .right , val )
67
67
} else {
68
68
// this is the node to delete
69
-
70
69
// node with one child
71
70
if root .left == nil {
72
- root = root .right
71
+ return root .right
73
72
} else if root .right == nil {
74
- root = root .left
73
+ return root .left
75
74
} 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
80
79
}
81
80
}
82
81
return root
@@ -94,14 +93,19 @@ func (t *btree) depth() int {
94
93
return _calculate_depth (t .root , 0 )
95
94
}
96
95
97
- /*
96
+
98
97
func main () {
99
98
t := & btree {nil }
100
99
inorder (t .root )
101
- t.root = insert(t.root, 10)
100
+ t .root = insert (t .root , 30 )
101
+
102
102
t .root = insert (t .root , 20 )
103
103
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 )
105
109
fmt .Print (t .depth (), "\n " )
106
110
inorder (t .root )
107
111
fmt .Print ("\n " )
@@ -119,4 +123,4 @@ func main() {
119
123
fmt .Print ("\n " )
120
124
fmt .Print (t .depth (), "\n " )
121
125
}
122
- */
126
+
0 commit comments