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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:33:04+00:00 2026-05-19T17:33:04+00:00

I’ve scoured the internet for help on this problem but I need help. This

  • 0

I’ve scoured the internet for help on this problem but I need help. This isn’t exactly an ordinary insertion problem for a binary tree, since we don’t get to work directly with the tree structure itself. My prof wrote that himself and has given us functions we can use to write functions pertaining to binary trees. As such, I can’t use nodes and pointers and things. Also this is in C++.

Anyway, so here is the description of the recursive function I have to write ( along with my starting attempt at working with the problem). Notice it returns a new tree entirely, it does not actually add something to the existing tree.

tree_t insert_tree(int elt, tree_t tree)
{
    /* 
    // REQUIRES; tree is a sorted binary tree
    // EFFECTS: returns a new tree with elt inserted at a leaf such that 
    //          the resulting tree is also a sorted binary tree.
    //
    //          for example, inserting 1 into the tree:
    //
    //                           4
    //                         /   \
    //                        /      \
    //                       2        5
    //                      / \      / \
    //                         3 
    //                        / \
    //
    // would yield
    //                           4
    //                         /   \
    //                        /      \
    //                       2        5
    //                      / \      / \
    //                     1   3 
    //                    / \ / \
    // 
    // Hint: an in-order traversal of a sorted binary tree is always a
    //       sorted list, and there is only one unique location for
    //       any element to be inserted.
    */

if (elt < elt(tree_left(tree)){
    return insert_tree(tree_left(left));
} else {
    return insert_tree(tree_right(right));
}
}

And here are the functions we can use:

extern bool tree_isEmpty(tree_t tree);
    // EFFECTS: returns true if tree is empty, false otherwise

extern tree_t tree_make();
    // EFFECTS: creates an empty tree.

extern tree_t tree_make(int elt, tree_t left, tree_t right);
    // EFFECTS: creates a new tree, with elt as it's element, left as
    //          its left subtree, and right as its right subtree

extern int tree_elt(tree_t tree);
    // REQUIRES: tree is not empty
    // EFFECTS: returns the element at the top of tree.

extern tree_t tree_left(tree_t tree);
    // REQUIRES: tree is not empty
    // EFFECTS: returns the left subtree of tree

extern tree_t tree_right(tree_t tree);
    // REQUIRES: tree is not empty
    // EFFECTS: returns the right subtree of tree

extern void tree_print(tree_t tree);
    // MODIFIES: cout
    // EFFECTS: prints tree to cout.
  • 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-19T17:33:05+00:00Added an answer on May 19, 2026 at 5:33 pm

    Inserting into a zero-element tree is easy:

    return tree_make(elt, tree_make(), tree_make());
    

    Inserting into a one-element tree is also easy:

    tree_t new_node = tree_make(elt, tree_make(), tree_make());
    if(elt < tree_elt(tree))
        return tree_make(tree_elt(tree), new_node, tree_right(tree));
    else
        return tree_make(tree_elt(tree), tree_left(tree), new_node);
    

    In general, to insert a new element, you’ll need to recreate all of its parents in this manner.


    Part 2: Recursion

    We have our base case (a zero-element tree). And we know how to attach a new subtree to the root of our existing tree.

    So how to get the new subtree? Well, how about we just insert the element into the current subtree?

    The following code will always attach the new element at the far left of the tree, but that should be trivial to correct once you understand it:

    tree_t tree_insert(int elt, tree_t tree)
    {
        if(tree_empty(tree)) //base case
            return tree_make(elt, tree_make(), tree_make());
        else
            return tree_make( // make a new node
                tree_elt(tree) // with the same value as the current one
                tree_insert(elt, tree_left(tree)) //insert into the left subtree
                tree_right(tree) // keep the right subtree the same
                );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.