zoukankan      html  css  js  c++  java
  • OCaml+Graphviz 初练

    用OCaml做解析

    bst.ml:

    Code
    type 'a bst=Leaf|Node of 'a * 'a bst * 'a bst;;

    let rec insert t x=match t with
        
    |Leaf->Node (x,Leaf,Leaf)
        
    |Node (y,l,r) when x<>y->if x<y then
            Node (y,insert l x,r)
            
    else Node (y,l,insert r x)
        
    |_->t;;

    let rec build l=match l with
        
    |[]->Leaf
        
    |h::t->insert (build t) h;;

    let rec inorder t=match t with
        
    |Leaf->[]
        
    |Node (x,l,r)->inorder l @ [x] @ inorder r;;
    (* -------------------------------------------------- *)
    let rec print_list l=match l with
        
    |[]->()
        
    |[x]->Printf.printf "%d\n" x
        
    |h::t->Printf.printf "%d " h;print_list t;;

    let rec aux_dot_output t=match t with
        
    |Leaf->()
        
    |Node (x,l,r)->
            Printf.printf 
    "%d [label=\"{<x> %d|{<l>|<r>}} \"];\n" x x;
            Printf.printf 
    "%d:l->%s;\n%d:r->%s;\n"
            x
            (
    match l with
                
    |Leaf->"Leaf"
                
    |Node (y,_,_)->string_of_int y^":x")
            x
            (
    match r with
                
    |Leaf->"Leaf"
                
    |Node (y,_,_)->string_of_int y^":x");
        aux_dot_output l;
        aux_dot_output r;;

    let dot_output t=Printf.printf "digraph G{\nnode [shape=record];\n";
                aux_dot_output t;
                Printf.printf 
    "}\n";;

    这个是主程序:main.ml

    Code
    open Bst;;

    let l=[4;1;0;8;9;7;2;5;3;6in
    let t=build l in
    (*print_list (inorder t);*)
    dot_output t;;

    输出的图如下:点击看大图

    image

  • 相关阅读:
    巴厘岛的雕塑(sculptures)
    BZOJ4361: isn
    BZOJ2131: 免费的馅饼
    BZOJ4240: 有趣的家庭菜园
    BZOJ5484: [Usaco2018 Dec]Sort It Out
    BZOJ 2151: 种树
    HDU 1285 确定比赛名次(拓扑排序+优先队列)
    申请中文域名并跳转到个人网站(多种方法的尝试)
    Java binarysearch方法
    eclipse2019-12设置中文
  • 原文地址:https://www.cnblogs.com/euphoria/p/1492406.html
Copyright © 2011-2022 走看看