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

  • Home
  • SEARCH
  • 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 9195721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:41:26+00:00 2026-06-17T21:41:26+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

I guess the above code does not have problems.

When using it, I write

let root = insert 4 Leaf

let root = insert 5 root

…

Is this the correct way to use/insert to the tree?

I mean, I guess I shouldn’t declare the root and every time I again change the variable root’s value, right?

If so, how can I always keep a root and can insert a value into the tree at any 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-06-17T21:41:27+00:00Added an answer on June 17, 2026 at 9:41 pm

    This looks like good functional code for inserting into a tree. It doesn’t mutate the tree during insertion, but instead it creates a new tree containing the value. The basic idea of immutable data is that you don’t “keep” things. You calculate values and pass them along to new functions. For example, here’s a function that creates a tree from a list:

    let tree_of_list l = List.fold_right insert l Leaf
    

    It works by passing the current tree along to each new call to insert.

    It’s worth learning to think this way, as many of the benefits of FP derive from the use of immutable data. However, OCaml is a mixed-paradigm language. If you want to, you can use a reference (or mutable record field) to “keep” a tree as it changes value, just as in ordinary imperative programming.

    Edit:

    You might think the following session shows a modification of a variable x:

    # let x = 2;;
    val x : int = 2
    # let x = 3;;
    val x : int = 3
    # 
    

    However, the way to look at this is that these are two different values that happen to both be named x. Because the names are the same, the old value of x is hidden. But if you had another way to access the old value, it would still be there. Maybe the following will show how things work:

    # let x = 2;;
    val x : int = 2
    # let f () = x + 5;;
    val f : unit -> int = <fun>
    # f ();;
    - : int = 7
    # let x = 8;;
    val x : int = 8
    # f ();;
    - : int = 7
    # 
    

    Creating a new thing named x with the value 8 doesn’t affect what f does. It’s still using the same old x that existed when it was defined.

    Edit 2:

    Removing a value from a tree immutably is analogous to adding a value. I.e., you don’t actually modify an existing tree. You create a new tree without the value that you don’t want. Just as inserting doesn’t copy the whole tree (it re-uses large parts of the previous tree), so deleting won’t copy the whole tree either. Any parts of the tree that aren’t changed can be re-used in the new tree.

    Edit 3

    Here’s some code to remove a value from a tree. It uses a helper function that adjoins two trees that are known to be disjoint (furthermore all values in a are less than all values in b):

    let rec adjoin a b =
        match a, b with
        | Leaf, _ -> b
        | _, Leaf -> a
        | Node (v, al, ar), _ -> Node (v, al, adjoin ar b)
    
    let rec delete x = function
        | Leaf -> Leaf
        | Node (v, l, r) ->
            if x = v then adjoin l r
            else if x < v then Node (v, delete x l, r)
            else Node (v, l, delete x r)
    

    (Hope I didn’t just spoil your homework!)

    • 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.