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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:24:07+00:00 2026-05-14T04:24:07+00:00

I have problems building a binary tree from the bottom up. THe input of

  • 0

I have problems building a binary tree from the bottom up.
THe input of the tree would be internal nodes of the trees with the children of this node being the leaves of the eventual tree.

So initially if the tree is empty the root would be the first internal node.

Afterwards, The next internal node to be added would be the new root(NR), with the old root(OR) being one of the child of NR. And so on.

The problem i have is that whenever i add a NR, the children of the OR seems to be lost when i do a inOrder traversal. This is proven to be the case when i do a getSize() call which returns the same number of nodes before and after addNode(Tree,Node)

Any help with resolving this problem is appreciated

edited with the inclusion of node class code.
both tree and node classes have the addChild methods because i’m not very sure where to put them for it to be appropriated. any comments on this would be appreciated too.

The code is as follows:


import java.util.*;

public class Tree {

Node root;
int size;

public Tree() {
    root = null;
}

public Tree(Node root) {
    this.root = root;
}

public static void setChild(Node parent, Node child, double weight) throws ItemNotFoundException {
    if (parent.child1 != null && parent.child2 != null) {
        throw new ItemNotFoundException("This Node already has 2 children");
    } else if (parent.child1 != null) {
        parent.child2 = child;
        child.parent = parent;
        parent.c2Weight = weight;
    } else {
        parent.child1 = child;
        child.parent = parent;
        parent.c1Weight = weight;
    }
}

public static void setChild1(Node parent, Node child) {
    parent.child1 = child;
    child.parent = parent;
}

public static void setChild2(Node parent, Node child) {
    parent.child2 = child;
    child.parent = parent;
}

public static Tree addNode(Tree tree, Node node) throws ItemNotFoundException {
    Tree tree1;
    if (tree.root == null) {
        tree.root = node;
    } else if (tree.root.getSeq().equals(node.getChild1().getSeq()) ||
            tree.root.getSeq().equals(node.getChild2().getSeq())) {


        Node oldRoot = tree.root;
        oldRoot.setParent(node);


        tree.root = node;


    } else { //form a disjoint tree and merge the 2 trees
        tree1 = new Tree(node);
        tree = mergeTree(tree, tree1);
    }
    System.out.print("addNode2 = ");
    if(tree.root != null ) {
        Tree.inOrder(tree.root);
    }
    System.out.println();
    return tree;

}


public static Tree mergeTree(Tree tree, Tree tree1) {
    String root = "root";
    Node node = new Node(root);
    tree.root.setParent(node);
    tree1.root.setParent(node);
    tree.root = node;
    return tree;
}

public static int getSize(Node root) {
    if (root != null) {
        return 1 + getSize(root.child1) + getSize(root.child2);
    } else {
        return 0;
    }

}

public static boolean isEmpty(Tree Tree) {
    return Tree.root == null;
}

public static void inOrder(Node root) {
    if (root != null) {
        inOrder(root.child1);
        System.out.print(root.sequence + "  ");
        inOrder(root.child2);

    }
}

}

public class Node {

Node child1;
Node child2;
Node parent;
double c1Weight;
double c2Weight;
String sequence;
boolean isInternal;

public Node(String seq) {
    sequence = seq;
    child1 = null;
    c1Weight = 0;
    child2 = null;
    c2Weight = 0;
    parent = null;
    isInternal = false;
}

public boolean hasChild() {
    if (this.child1 == null && this.child2 == null) {
        this.isInternal = false;
        return isInternal;
    } else {
        this.isInternal = true;
        return isInternal;
    }
}

public String getSeq() throws ItemNotFoundException {
    if (this.sequence == null) {
        throw new ItemNotFoundException("No such node");
    } else {
        return this.sequence;
    }
}

public void setChild(Node child, double weight) throws ItemNotFoundException {
    if (this.child1 != null && this.child2 != null) {
        throw new ItemNotFoundException("This Node already has 2 children");
    } else if (this.child1 != null) {
        this.child2 = child;
        this.c2Weight = weight;
    } else {
        this.child1 = child;
        this.c1Weight = weight;
    }
}
public static void setChild1(Node parent, Node child) {
    parent.child1 = child;
    child.parent = parent;
}

public static void setChild2(Node parent, Node child) {
    parent.child2 = child;
    child.parent = parent;
}

public void setParent(Node parent){
    this.parent = parent;
}

public Node getParent() throws ItemNotFoundException {
    if (this.parent == null) {
        throw new ItemNotFoundException("This Node has no parent");
    } else {
        return this.parent;
    }
}

public Node getChild1() throws ItemNotFoundException {
    if (this.child1 == null) {
        throw new ItemNotFoundException("There is no child1");
    } else {
        return this.child1;
    }
}

public Node getChild2() throws ItemNotFoundException {
    if (this.child2 == null) {
        throw new ItemNotFoundException("There is no child2");
    } else {
        return this.child2;
    }
}

}

  • 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-14T04:24:08+00:00Added an answer on May 14, 2026 at 4:24 am

    Looking at just the code attached, one can not find an explicit setChild being called, only setParent. If you also attach the code for setParent, then we can confirm if that method also creates the reverse links from the parent to the child.

    My guess is that setParent does NOT setChild, in which case the links to the children were not lost: they were never made in the first place.

    //after edit:

    Okay, it’s confirmed now that setParent does not setChild, so you never made those links in the first place. You should augment all calls of the form:

    someNode.setParent(otherNode);
    

    to also:

    otherNode.setChild(someNode);
    

    Other remarks:

    • setChild can be less repetitive by calling setChild1 and setChild2.
    • The logic of which link is free to use in setChild is correct, but confusing, i.e. “if that other one isn’t free, then use this one”, instead of the more straightforward “if this one is free, just use it”.
    • Do make a habit of declaring and using local variables in the smallest scope possible, e.g. tree1 in addNode can be declared inside the one block where it’s used. In fact, it can even be written out of the code altogether, e.g. tree = merge(tree, new Tree(node));.
    • Just something you should confirm: is node.getChild1().getSeq() prone to causing a NullPointerException be thrown?
      • //after edit: okay, it throws ItemNotFoundException instead. In either case, your original condition in addNode is wrong since you can’t guarantee both children exist (because then you wouldn’t be able to add a child to it).
    • //after edit: Why are you replicating the functionality of Node.setChild* in Tree as static methods… and then NOT using either?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • 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 The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

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.