Excluir um elemento de uma árvore de pesquisa binária em F #
Estou tentando escrever um método para excluir um elemento de um BST. Até agora, é isso que eu tenho. Não tenho certeza se estou no caminho certo ou se há uma maneira melhor de fazê-lo usando a correspondência de padrões para corresponder aos diferentes casos de exclusão, ou seja: sem filhos, 1 filho, 2 filhos.
type 'a bst = NL | BinTree of 'a * 'a bst * 'a bst;;
let rec smallest = function
| NL -> failwith "tree is empty"
| BinTree(m, lst, rst) -> if lst = NL then BinTree(m, lst, rst)
else smallest lst;;
let rec smallest2 = function
| NL -> failwith "tree is empty"
| BinTree(m, lst, rst) -> if lst = NL then m
else smallest2 lst;;
let rec rem2 = function
| NL -> NL
| BinTree(m, NL, NL) -> NL
| BinTree(m, NL, rst) -> rst
| BinTree(m, lst, NL) -> lst
| BinTree(m, lst, rst) -> BinTree(smallest2 rst, lst, rst);;
let rec rem x = function
|NL -> failwith "Node doesn't exit"
|BinTree(m, lst, rst) -> if m = x then rem2 (BinTree(m, lst, rst))
elif m < x then BinTree(m, lst, rem x rst)
else BinTree(m, rem x lst, rst);;
Os casos de nenhum filho e um único filho funcionam perfeitamente, mas quando o nó a ser excluído tem 2 filhos, não consigo descobrir como implementar esse caso. Quero substituir o valor desse nó pelo menor item na subárvore direita e remover o menor item na subárvore direita.