Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7519277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:52:35+00:00 2026-05-30T01:52:35+00:00

How to define a tree + a pointer to its subtree in OCaml such

  • 0

How to define a tree + a pointer to its subtree in OCaml such that adding leaves in this subtree takes constant time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:52:36+00:00Added an answer on May 30, 2026 at 1:52 am

    If you want to use purely functional representation, zippers — suggested by nlucaroni — are indeed the good solution to represent a cursor deep down a data structure, that can be moved or used to update the structure.

    If you wish for a solution using in-place mutation, you can use mutable data through mutable record fields, or the references (ref) that are derived from it. For example:

    type 'a tree_cell = {mutable node : 'a tree}
    and 'a tree = Leaf of 'a | Branch of 'a tree_cell * 'a * 'a tree_cell
    

    If you hold an 'a tree_cell, you can mutate it (in constant time).

    let head {node = (Leaf x | Branch(_, x, _))} = x
    
    let duplicate cell =
      cell.node <- Branch (cell, head cell, {node = cell.node})
    

    Edit: in the comments of your question, you seem to indicate your interest in a solution for n-ary trees.

    The general n-ary case can be represented as

    type 'a tree_cell = {mutable node: 'a tree}
    and 'a tree = Branch of 'a * 'a tree_cell list
    

    while the zipper solution would look like (untested code)

    type 'a tree = Branch of 'a * 'a forest
    and 'a forest = 'a tree list
    
    (* the data between the current cursor and the root of the tree *)
    type 'a top_context = Top | Under of 'a * 'a tree * 'a top_context
    
    (* a cursor on the 'data' element of a tree *)
    type 'a data_cursor = top_context * 'a tree list
    
    (* plug some data in the hole and get a tree back *)
    val fill_data : 'a data_cursor -> 'a -> 'a tree
    
    (* a cursor on one of the children of a tree *)
    type 'a list_zipper = 'a list * 'a list
    type 'a children_cursor = top_context * 'a * 'a tree list_zipper
    
    (* plug some subtree in the hole and get a tree back *)
    val fill_children : 'a children_cursor -> 'a tree -> 'a tree
    
    (* carve a data hole at the root; also return what was in the hole *)
    val top_data : 'a tree -> 'a data_cursor * 'a
    
    (* fill a data hole and get a cursor for the first children in return
       -- if it exists *)
    val move_down : 'a data_cursor -> 'a -> ('a children_cursor * 'a tree) option
    (* fill the children hole and carve out the one on the left *)
    val move_left : 'a data_cursor -> 'a tree -> ('a data_cursor * 'a tree) option
    val move_right : 'a data_cursor -> 'a tree -> ('a data_cursor * 'a tree) option
    (* fill the children hole and get a cursor on the data *)
    val move_up : 'a children_cursor -> 'a tree -> 'a data_cursor * 'a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For our CMS we have a site manager that defines the site's tree structure
on more than one occasion I felt the need to define class methods that
Assume that my delete tries to rebalance the tree inorder (left to right). I'm
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I want to create a tree. I saw the following code #define NODEALLOC(struct treenode*)malloc(sizeof(struct
In Scheme, I can use define-struct to make a binary search tree, but how
Well, I am asked to do the next thing: To define a binary tree
I'm trying to define a tree structure in GORM. Here is my model: class
I am trying to correctly define a tree structure in GORM and having trouble.
I have a Ext.tree.Panel and define in it I have a store . I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.