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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:56:25+00:00 2026-05-25T18:56:25+00:00

Okay. I have a binary tree, and this is what I want to do

  • 0

Okay. I have a binary tree, and this is what I want to do with it:

For each node in original tree:
If it’s not a leaf, replace it with a leaf node.
Do a calculation on the original tree updated with the removed branch.
Revert the node back to how it was (so now the tree is the same as at the beginning).

The problem is this: I am traversing the tree using a stack. If I change the stack.pop() node to a leaf, this does NOT remove any branches in the original tree. It’s the same reasoning behind why you can do:

int x=1
int y=x
y++

And x still equals 1. There’s a technical term for this but I forgot it.

So how can I edit the nodes in an original tree and still traverse it?

This is basically what I’m doing to traverse the tree right now:

public void iterativePreorder(Node root) {
        Stack nodes = new Stack();
        nodes.push(root);

        Node currentNode;

        while (!nodes.isEmpty()) {
                currentNode = nodes.pop();
                Node right = currentNode.right();
                if (right != null) {
                        nodes.push(right);
                }
                Node left = currentNode.left();
                if (left != null) {
                        nodes.push(left);      
                }
                //This is where you do operations on the currentNode
        }
}
  • 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-25T18:56:26+00:00Added an answer on May 25, 2026 at 6:56 pm

    From what I can tell from your question, for every Node you want to calculate something about the tree as if that node was a leaf.

    To do this there is no reason to actually make that node a leaf and then reattach it. Instead, your logic can simply remember which node to treat as a leaf for each computation.

    Traverse the tree, and for each Node, let’s call it outerCurrentNode, once again traverse the tree doing your calculation – but now for each Node, let’s call it innerCurrentNode, test to see if outerCurrentNode == innerCurrentNode. If the test returns true, treat that innerCurrentNode as if it’s a leaf, ignoring its children.

    EDIT: Here’s a mock up of what I’m suggesting (untested):

    //entry point - called from directing code
    public void iterativePreorder(Node root) {
       iterativePreorderKernel(root, root);
    }
    
    //recursive method - keeps track of root in addition to current Node
    private void iterativePreorderKernel(Node root, Node current) {
    
        if (current.left() != null) {
            iterativePreorderKernel(root, current.left());
        }
        if (current.right() != null) {
            iterativePreorderKernel(root, current.right());
        }
    
        //for each Node in the tree, do calculations on the entire tree, pretending
        //the current Node is a leaf
        doCalculation(root, current);
    }
    
    //calculation method (also recursive) - takes a current Node, plus
    //the Node to treat as a leaf
    public void doCalculation(Node innerCurrent, Node pretendLeaf) {
    
       //do calculation with inner current node
    
       if (innerCurrent != pretendLeaf) {
          if (innerCurrent.left() != null) {
             doCalculation(innerCurrent.left(), pretendLeaf);
          }
          if (innerCurrent.right() != null) {
             doCalculation(innerCurrent.right(), pretendLeaf);
          }
       }
    }
    

    I’m using recursion instead of a Stack, but either will work. iterativePreorder() does a traversal, calling doCalculation() for each Node, passing it in along with the root (to keep track of the entire tree). That method then does its own traversal, doing your calculation, but stopping short when it reaches the specially marked Node.

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

Sidebar

Related Questions

Okay I have updated my code a little, but I am still not exactly
Okay so I have a Binary Search Tree built using only C structs and
Okay i have this problem with every page i make. im not sure what
Okay I have this RewriteRule which is supposed to redirect any request for the
Okay I have updated my code quite a bit. I am getting a new
okay have a this list: object[] test; test[0]=null; .... test[8700]=null; test[8701]= object[] .... test[9431]=
Okay I have three tables that all communicate with each other. ForumTopic t ForumMessage
I have written the following code to check if a tree is a Binary
Okay I have the below code performing something I don't want it to do.
okay so i have a div and a span right next to each other

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.