BST Insert And Delete With Successor (Go)
Implement a Go binary search tree supporting insert and delete using in-order successor for two-child nodes.
Code
package main
type TreeNode struct {
val int
left *TreeNode
right *TreeNode
}
type BST struct {
root *TreeNode
}
func (t *BST) Insert(val int) {
if t.root == nil { t.root = &TreeNode{val: val}; return }
cur := t.root
for {
if val < cur.val {
if cur.left == nil { cur.left = &TreeNode{val: val}; return }
cur = cur.left
} else {
if cur.right == nil { cur.right = &TreeNode{val: val}; return }
cur = cur.right
}
}
}Report a problem with this exampleOpens a pre-filled issue on GitHub.