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 9200331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:44:35+00:00 2026-06-17T22:44:35+00:00

Ok, I have written a binary search tree in OCaml. type ‘a bstree =

  • 0

Ok, I have written a binary search tree in OCaml.

type 'a bstree = 
    |Node of 'a * 'a bstree * 'a bstree
    |Leaf


let rec insert x = function
    |Leaf -> Node (x, Leaf, Leaf)
    |Node (y, left, right) as node -> 
        if x < y then
            Node (y, insert x left, right)
        else if x > y then
            Node (y, left, insert x right)
        else
            node

The above code was said to be good in The right way to use a data structure in OCaml

However, I found a problem. This insert will only work when building a bst from a list in one go, such as

let rec set_of_list = function
     [] > empty
   | x :: l > insert x (set_of_list l);;

So if we build a bst from a list continuously, no problem, we can get a complete bst which has all nodes from the list.

However, if I have a bst built previously and now I wish to insert a node, then the resulting bst won’t have complete nodes from the previous tree, am I right?

Then how should I write a bst in OCaml so that we create a new bst with all nodes from previous tree to keep the previous tree immutable? If every time I need to copy all nodes from old bst, will that impact the performance?


Edit:

So let’s say initially, a bst is created with one node t1 = (10, Leaf, Leaf).

then I do let t2 = insert 5 t1, then I get t2 = (10, (5, Leaf, Leaf), Leaf), right? inside t2, let’s give a variable c1 to the child node (5, Leaf, Leaf)

then I do let t5 = insert 12 t2, then I get t3 = (10, (5, Leaf, Leaf), (15, Leaf, Leaf)). let’s give a variable c2 to the child node (5, Leaf, Leaf)

So my question is whether c1 == c2? Are the two (5, Leaf, Leaf)s in t2 and t3 exactly the same?

  • 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-06-17T22:44:36+00:00Added an answer on June 17, 2026 at 10:44 pm

    Look at the accepted answer to the linked question.
    To be specific this line here:

    let tree_of_list l = List.fold_right insert l Leaf

    Work out the chain of what is happening. Take the list 1,2,3.

    First we have no tree and the result of insert 1 Leaf.

    call this T1

    Next is the tree generated by insert 2 T1

    call this T2

    Then the tree generated by insert 3 T2

    This is what is returned as the result of Tree_of_list.

    If we call the result T3 then somewhere else in code call insert 4 T3 there is no difference in the result returned from insert than in calling Tree_of_list with the list 1,2,3,4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I have written a binary search tree in OCaml. type 'a bstree =
I have written the following function to search for a value in a binary
I am writing a delete member function for a Binary Search Tree. I have
Have written a Binary Search Tree that stores data on ships, the key for
I have written a code for inserting to the binary tree an element generic's
I have rewritten the Binary Search Tree code found in the following links to
I am trying to implement the binary search in python and have written it
I have written the following code to check if a tree is a Binary
I have written a function that converts a decimal number to a binary number.
Suppose I have a function which I have written as a binary-operator (binop), how

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.