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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:12:29+00:00 2026-06-14T16:12:29+00:00

Hi I have been searching for some good information about how I can make

  • 0

Hi I have been searching for some good information about how I can make a sorted binary tree datatype in F# but i cant find anything understandable. I have checked the example on msdn-page but i dont get it.

  • 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-14T16:12:31+00:00Added an answer on June 14, 2026 at 4:12 pm

    If you’re looking for an existing implementation, then the FSharpX library has quite a few data structures (including some trees) available. Although I do not think it has a straightforward implementation of a sorted binary tree. You can also access all the .NET collections from F#, but I don’t think .NET has a binary tree either. It has sorted list, which might be just fine for you, depending on what you’re trying to do.

    If you want to implement your own, then you need to start by defining a data type to represent the tree:

    type SortedTree<'T when 'T : comparison> =
      | Node of SortedTree<'T> * 'T * SortedTree<'T>
      | Leaf of 'T
    

    The Node element means that all values on the left are smaller than the value stored in the node and all greater (or equal) values are on the right. I added a constraint 'T : comparison which means that you can only create tree of comparable elements (this is needed in the implementation to sort the tree).

    Implementing all the common tree operations is going to be quite a lot of work, but a simple attempt at insertion (that does not keep the tree ballanced in any way) looks like this:

    let rec insert element tree = 
      match tree with
      | Leaf v when element < v -> Node(Leaf element, v, Leaf v)
      | Leaf v -> Node(Leaf v, element, Leaf element)
      | Node(left, key, right) when element < key -> Node(insert element left, key, right)
      | Node(left, key, right) -> Node(left, key, insert element right)
    

    The pattern matching handles 4 interesting cases: When the tree is Leaf, you need to build a new node with the new element either on the left or on the right. When the tree is Node, then you want to insert to the left or right, depending on the value of the key.

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

Sidebar

Related Questions

I need some help, i have been searching around and cant find the correct
Good morning. I have been searching for some code either a formula or VBA
Have been searching how to convert a dictionary to a string. But the results
I have been searching several days for an example of how to make a
I have been searching for quite a bit now but I have never been
I have been searching for a few hours and cant seem to find answer
I have been searching for this solution for some time have not found any
I have been having some trouble finding a good way to output just the
Good day, I have been searching for a way to format output using writeline/write
Ok, so I have been searching for quite some time on this. I have

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.