Я пытаюсь вставить дерево в отсортированном списке дерева со следующим кодом:не в состоянии вставить дерево в отсортированном TreeList
exception NilTree;;
type hTree = Nil | Node of char * int * hTree * hTree;;
let rec printTree t = match t with
| Nil ->()
| Node(c, w, l, r) -> print_char c ; print_int w ; printTree l ; printTree r;;
let rec printTreeList l = match l with
| [] ->()
| h::t -> print_newline (printTree h) ; printTreeList t;;
let insertree l tree =
let rec insertree' l tree prev = match tree with
| Nil -> raise NilTree
| Node(c, w, left, right) -> match l with
| [] -> [email protected][tree]
| h::t -> match h with
| Nil -> raise NilTree
| Node(c', w', left', right') ->
if w <= w' then
[email protected][tree]@[h]@t
else
insertree' t tree [email protected][h]
in insertree' l tree [];;
let tree1 = insertree [Node('a', 3, Nil, Nil)] (Node('b', 5, Nil, Nil));;
printTreeList tree1;;
деревья имеют четыре поля, и дерево с меньшим междунар должно быть перед другими, , но вот что я получаю:
b5
a3
В printTree, не хотите ли вы сначала напечатать левое поддеревье, затем текущий узел, а затем правое поддерево? –