Commit 03d038be authored by bguillaum's avatar bguillaum

add function G_graph.graph_to_tree

git-svn-id: svn+ssh:// 7838e531-6607-4d57-9587-6c381814729c
parent e909ac2d
......@@ -228,6 +228,45 @@ module G_graph = struct
) Gid_map.empty
exception Not_a_tree
type tree = T of (Gid.t * tree list)
let rec tree_to_string = function
| T (gid, daughters) ->
sprintf "%s [%s]"
(Gid.to_string gid)
((String.concat) ";" ( tree_to_string daughters))
let graph_to_tree (g : t) : tree =
let rec build_sub_tree map forest gid =
if List.mem_assoc gid forest
then (map, forest)
let (new_map, daugthers, new_forest) = Massoc_gid.fold
(fun (acc_map, sub_trees, acc_forest) gid2 edge ->
if edge = G_edge.sub
(* ensure that gid2 is in forest *)
let (new_acc_map, new_acc_forest) = build_sub_tree acc_map acc_forest gid2 in
let sub = List.assoc gid2 new_acc_forest in
( new_acc_map,
List.remove_assoc gid2 new_acc_forest
else (acc_map, sub_trees, acc_forest)
) (map,[],forest) (G_node.get_next (Gid_map.find gid map)) in
(Gid_map.remove gid new_map, (gid, T (gid, List.rev daugthers))::new_forest) in
let rec loop (unused_map, forest) =
match (Gid_map.is_empty unused_map, forest) with
| (true, [(_,tree)]) -> tree
| (true, _) -> raise Not_a_tree
| _ ->
(* pick one unused node *)
let (gid,_) = Gid_map.choose unused_map in
loop (build_sub_tree unused_map forest gid) in
loop (, [])
let get_highest g = g.highest_index
let find node_id graph = Gid_map.find node_id
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment