I would like to write a function to compare if the children keys of the current node are less/greater than
their parent and I don't know how to do that.
I would really like to read more about lenses but I haven't found any material on the web.
The problem is , I can't write n.key >= n.left.key && n.key < n.right.key in the compare_children function;
Stack overflow link
type avl_tree =
Node of avl_tree_node
| Leaf
and avl_tree_node = {
key : int;
balance : int;
left : avl_tree;
right : avl_tree;
}
type subtree_lens = {
get : avl_tree_node -> avl_tree;
set : avl_tree_node -> avl_tree -> avl_tree_node
}
let lens_right = {
get = (fun node -> node.right);
set = fun node t -> {node with right = t}
}
let lens_left = {
get = (fun node -> node.left);
set = fun node t -> {node with left = t}
}
let compare_children nd =
match nd with
| Leaf -> true
| Node n -> n.key >= n.left.key && n.key < n.right.key
let compare_children nd =
match nd with
| Leaf -> true
| Node n ->
match n.left, n.right with
| Leaf, Leaf -> ...
| Node left, Leaf -> ...
| Leaf, Node right -> ...
| Node left, Node right -> n.key >= left.key && n.key < right.key
--
You received this message because you are subscribed to the Google Groups "ocaml-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+...@googlegroups.com.
set = fun node t -> {node with right = t}
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+...@googlegroups.com.
Thanks Philippe ,
One more question though :
This line of code from lens_right function :it takes a node as an argument and then creates a brand new node with its right property being set to t ?set = fun node t -> {node with right = t}
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+unsubscribe@googlegroups.com.