@@ -66,16 +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 .right = bst_delete (root .right , d .val )
75
+ n := root .right
76
+ d := inorderSuccessor (n )
77
+ d .left = root .left
78
+ return root .right
79
79
}
80
80
}
81
81
return root
@@ -93,14 +93,19 @@ func (t *btree) depth() int {
93
93
return _calculate_depth (t .root , 0 )
94
94
}
95
95
96
- /*
96
+
97
97
func main () {
98
98
t := & btree {nil }
99
99
inorder (t .root )
100
- t.root = insert(t.root, 10)
100
+ t .root = insert (t .root , 30 )
101
+
101
102
t .root = insert (t .root , 20 )
102
103
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 )
104
109
fmt .Print (t .depth (), "\n " )
105
110
inorder (t .root )
106
111
fmt .Print ("\n " )
@@ -118,4 +123,4 @@ func main() {
118
123
fmt .Print ("\n " )
119
124
fmt .Print (t .depth (), "\n " )
120
125
}
121
- */
126
+
0 commit comments