在二叉树 Ocaml 中插入(左下)函数

问题描述 投票:0回答:0

给出二叉树的这些定义,以及它的拉链结构

type ('a, 'b) bin_tree =
  | Leaf of 'b
  | Node of 'a * ('a, 'b) bin_tree * ('a, 'b) bin_tree
              
type ('a, 'b) bin_context = 
  | Top
  | LContext of ('a * ('a, 'b) bin_context * ('a, 'b) bin_tree)
  | RContext of ('a * ('a, 'b) bin_context * ('a, 'b) bin_tree)

type ('a, 'b) z = BZ of ('a, 'b) bin_context * ('a, 'b) bin_tree
              

定义一个插入函数

insert (BZ(c, t) t1
是否合理,它通常会在
Node(v, l, r)
c
之间插入一个节点
t
,你会怎么做?我没有提供预定义的功能,因为坦率地说,我不知道该怎么做。

基本上我的问题是在我正在使用

Graphics
库的情况下,当我进行拆分时,我想插入。

像这样:

let coord1, coord2 =
  let coord = Wm.get_coord(content) in
  match coord with
  | Coord {px = x; py = y; sx = sx; sy = old_sy} ->
    let new_sy = old_sy / 2 in
    let coord1 = Wm.Coord {px = x; py = y; sx = sx; sy = new_sy} in
    let coord2 = Wm.Coord {px = x; py = y + new_sy; sx = sx; sy = new_sy} in
    coord1, coord2   
       in
       match context with
       | Top ->
       (Tree.next_leaf (Tree.focus_first_leaf 
       ((Tree.combine 
       (Wm.Split(Vertical, 0.5), Wm.get_coord(content)) 
       (Tree.return_leaf (Wm.get_win(content), coord1)) 
       (Tree.return_leaf (Wm.Win("W" ^ (Int.to_string (count)), Color.random ()), coord2)
       )))))
       (* |  LNContext(_, _, _) -> oz
       | RNContext(_, _, _) -> oz *)

其中

focus_first_leaf
得到一棵树并返回一个以最左边的叶子为焦点的拉链,而
combine
给出一个新节点。

我不知道在这些比赛中该怎么办

(* |  LNContext(_, _, _) -> oz
       | RNContext(_, _, _) -> oz *)
functional-programming ocaml binary-tree zipper
© www.soinside.com 2019 - 2024. All rights reserved.