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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:00:12+00:00 2026-05-13T21:00:12+00:00

i have a problem calling multiple instance of a class that i have coded

  • 0

i have a problem calling multiple instance of a class that i have coded (Tree, TreeNode)
in the main method, the system would give the output c d j c d j even though both trees are obviously different trees.
if i were to separate both postOrder() calls(each called after the tree has been pushed in to the stack)

    Stack<Tree> alphaStack = new Stack<Tree>();
    TreeNode a = new TreeNode('i');
    Tree tree = new Tree(a);
    TreeNode newleft = new TreeNode('a');
    TreeNode newright = new TreeNode('b');
    tree.setLeft(a, newleft);
    tree.setRight(a, newright);
    alphaStack.push(tree);
    Tree.postOrder(alphaStack.pop().getRoot());

    TreeNode b = new TreeNode('j');
    Tree newtree = new Tree(b);
    TreeNode left = new TreeNode('c');
    TreeNode right = new TreeNode('d');
    newtree.setLeft(b, left);
    newtree.setRight(b, right);
    alphaStack.push(newtree);

    Tree.postOrder(alphaStack.pop().getRoot());

the output would be a b i c d j.

Does this mean that my class is not being duplicated but instead being reused when i make new Trees?

Below is the code:

import java.util.Stack;

public class mast_score {

public static void main(String[] args){
    Stack<Tree> alphaStack = new Stack<Tree>();
    TreeNode a = new TreeNode('i');
    Tree tree = new Tree(a);
    TreeNode newleft = new TreeNode('a');
    TreeNode newright = new TreeNode('b');
    tree.setLeft(a, newleft);
    tree.setRight(a, newright);
    alphaStack.push(tree);

    TreeNode b = new TreeNode('j');
    Tree newtree = new Tree(b);
    TreeNode left = new TreeNode('c');
    TreeNode right = new TreeNode('d');
    newtree.setLeft(b, left);
    newtree.setRight(b, right);
    alphaStack.push(newtree);

    Tree.postOrder(alphaStack.pop().getRoot());
    Tree.postOrder(alphaStack.pop().getRoot());

} }

code for TreeNode

public class TreeNode{
Object item;

TreeNode parent;
TreeNode left;
TreeNode right;

public TreeNode (Object item) {
    this.item = item;
    parent = null;
    left = null;
    right = null;
}

public TreeNode getParent(TreeNode current) throws ItemNotFoundException
{
    if(current == null) throw new ItemNotFoundException("No parent");
    if(current.parent == null) throw new ItemNotFoundException("This
    is the root");
    else return current.parent;
}
public TreeNode getLeft(TreeNode current) throws ItemNotFoundException
{
    if(current == null) throw new ItemNotFoundException("No left or
    right child");
    if(current.left == null) throw new ItemNotFoundException("No left
    child");
    else return current.left;
}

public TreeNode getRight(TreeNode current) throws ItemNotFoundException
{
    if(current == null) throw new ItemNotFoundException("No left or
    right child");
    if(current.right == null) throw new ItemNotFoundException("No
    right child");
    else return current.right;
}

public Object getElement() throws ItemNotFoundException {
    if(this.item == null) throw new ItemNotFoundException("No such
    node");
    else return this.item;
} }

code for Tree class

import java.util.*;

public class Tree {

static TreeNode root;
int size;

public Tree() {
    root = null;
}

public Tree(TreeNode root) {
    Tree.root = root;
}

public TreeNode getRoot() {
    return this.root;
}

public int getLvl(TreeNode node) {
    return node.lvlCount;
}

public void setLeft(TreeNode node, TreeNode left) {
    node.left = left;
}

public void setRight(TreeNode node, TreeNode right) {
    node.right = right;
}
public static void postOrder(TreeNode root) {
    if (root != null) {

        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.item + " ");

    } else {
        return;
    }

}

public static int getSize(TreeNode root) {
    if (root != null) {
        return 1 + getSize(root.left) +
    getSize(root.right);
    } else {
        return 0;
    }
}

public static boolean isEmpty(Tree Tree) {
    return Tree.root == null;
} }
  • 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-13T21:00:12+00:00Added an answer on May 13, 2026 at 9:00 pm

    Your problem is here, in the Tree class:

    static TreeNode root;
    

    You should remove the word static, and replace Tree.root with this.root.

    Adding the keyword static causes the variable root to be shared between all instances of Tree in your program, which is not what you want.

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

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No. You can prevent the common user from bothering, but… May 14, 2026 at 6:27 pm
  • Editorial Team
    Editorial Team added an answer It sounds like you are asking three different questions: Can… May 14, 2026 at 6:27 pm
  • Editorial Team
    Editorial Team added an answer First: The blog entry you cite is just wrong. Here… May 14, 2026 at 6:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.