У меня возникли проблемы с реализацией этого алгоритма в OCaml, так как я должен печатать круглые скобки между функциями.
Алгоритм выглядит следующим образом:
BEGIN
WRITE ( "(" )
IF (NOT EMPTY tree) THEN
IF (NOT EMPTY (left_leaf tree)) OR (NOT EMPTY (right_leaf tree)) THEN BEGIN
WRITE (" ", root tree, " ")
preorder (left_leaf tree)
WRITE (" ")
preorder (right_leaf tree)
END ELSE
WRITE (" ", root tree, " ")
WRITE ( ")" ); {this has to be always executed}
END
Это моя неудачная попытка в OCaml:
let rec preorderParenthesed tree =
print_string "(" in
if not (isEmptyTree tree) then (
if not (isEmptyTree(leftLeaf tree)) || not (isEmptyTree(rightLeaf tree)) then begin
print_string " ";
print_string (string_of_root (root tree));
print_string " ";
preorderParenthesed (leftLeaf tree);
print_string " ";
preorderParenthesed (rightLeaf tree);
end else
print_string " ";
print_string (string_of_root (root tree));
print_string " ";
)
else if true then print_string ")\n";;
Любая помощь будет оценена
type bst =
Empty
| Node of (key * bst * bst);;