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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:19:52+00:00 2026-05-31T00:19:52+00:00

The type is defined as data BST = MakeNode BST String BST | Empty

  • 0

The type is defined as

data BST = MakeNode BST String BST
          | Empty

I’m trying to add a new leaf to the tree, but I don’t really understand how to do it with recursion.

the function is set up like this

add :: String -> BST -> BST
  • 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-31T00:19:53+00:00Added an answer on May 31, 2026 at 12:19 am

    The advantage of using binary trees is that you only need to look at the “current part” of the tree to know where to insert the node.

    So, let’s define the add function:

    add :: String -> BST -> BST
    

    If you insert something into an empty tree (Case #1), you just create a leaf directly:

    add s Empty = MakeNode Empty s Empty
    

    If you want to insert something into a node (Case #2), you have to decide which sub-node to insert the value in. You use comparisons to do this test:

    add s t@(MakeNode l p r) -- left, pivot, right
      | s > p     = Node l p (add s r) -- Insert into right subtree
      | s < p     = Node (add s l) p r -- Insert into left subtree
      | otherwise = t -- The tree already contains the value, so just return it
    

    Note that this will not rebalance the binary tree. Binary tree rebalancing algorithms can be very complicated and will require a lot of code. So, if you insert a sorted list into the binary tree (e.g. ["a", "b", "c", "d"]), it will become very unbalanced, but such cases are very uncommon in practice.

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

Sidebar

Related Questions

Basically I have defined a Tree data type which is defined as follows: data
I've got a Clr user defined type that takes a string of values sepertated
I have a complex data structure (user-defined type) on which a large number of
I have defined data type for Binary numbers as follows data Bin = Nil
Consider this simple example: template <class Type> class smartref { public: smartref() : data(new
I have created a user defined type to contain some data that I will
I defined a data type: Data Card = Card Int deriving (Show, Eq) (I
Is it possible to define an array of text fields (or any data type)
If I have a type defined as a set of an enumerated type, it's
I have a complex type defined which doesn't currently contain any minOccurs restrictions. When

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.