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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:11:34+00:00 2026-05-31T23:11:34+00:00

As the title says, I have to trim a binary tree based on a

  • 0

As the title says, I have to trim a binary tree based on a given min and max value. Each node stores a value, and a left/right node. I may define private helper methods to solve this problem, but otherwise I may not call any other methods of the class nor create any data structures such as arrays, lists, etc.

An example would look like this:

                           overallRoot
                         _____[50]____________________
                        /                             \
          __________[38]                _______________[90]
         /              \              /
    _[14]                [42]      [54]_____
   /     \                                  \
[8]       [20]                               [72]
          \                             /    \
               [26]                     [61]      [83]

trim(52, 65);

should return:

overallRoot
[54]
    \
     [61]

My attempted solution has three methods:

public void trim(int min, int max) {
rootFinder(overallRoot, min, max);

}

First recursive method finds the new root perfectly.

private void rootFinder(IntTreeNode node, int min, int max) {
if (node == null)
    return;

if (overallRoot.data < min) {
    node = overallRoot = node.right;
    rootFinder(node, min, max);

}

else if (overallRoot.data > max) {
    node = overallRoot = node.left;
    rootFinder(node, min, max);
}
else
    cutter(overallRoot, min, max);
}

This second method should eliminate any further nodes not within the min/max, but it doesn’t work as I would hope.

private void cutter(IntTreeNode node, int min, int max) {
if (node == null)
    return;

if (node.data <= min) {
    node.left = null;
}
if (node.data >= max) {
    node.right = null;
}

if (node.data < min) {
    node = node.right;
}

if (node.data > max) {
    node = node.left;
}


cutter(node.left, min, max);
cutter(node.right, min, max);
}

This returns:

overallRoot
[54]_____
         \
          [72]
         /
     [61]

Any help is appreciated. Feel free to ask for further explanation as needed.

  • 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-31T23:11:35+00:00Added an answer on May 31, 2026 at 11:11 pm

    This assumes that a node x has the following values:

    1. left (pointer to the left child)
    2. right (pointer to the right child)
    3. parent (pointer to parent)

    You might want to make a method called CutBranch, which should simply remove a node an all it’s subtrees from your tree. Let T be your tree, and let T.root be a pointer to it’s root. It could then work like this:

    CutBranch(x,T) {
        y = T.root;
        while (y.left != x && y.right != x) {
            if (y < x) y = y.right;
            else       y = y.left;
            }
        if (y < x) y.right = Nil;
        else       y.left = Nil;
    }
    

    This assumes that your tree doesn’t include nodes with equal values of course, but it takes O(lg n) time. It doesn’t do any garbage collection however.

    now you can iterate through the nodes, and every time you reach a node smaller than your lower bound, you can call CutBranch on it’s left child, and then delete the node itself. If the node is larger than your upper bound, then you can CutBranch it’s right child and delete it.

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

Sidebar

Related Questions

As the title says, I have two ArrayLists. Strangely, setting a value on one
The title says it mostly. If I have an object in Python and want
The title says enough I think. I have a full quality BufferedImage and I
Pretty much what the title says really. We have some code that is .NET
Basically what the title says... I need to have an image that when clicked,
Ok, so as the title says, I have an HTML page that I fetch
OK, I don't think the title says it right... but here goes: I have
The title more or less says it all: I have a function which takes
As the title says I have a list of Django objects and I want
As the title says, I have a problem with binding to a change in

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.