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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:12:02+00:00 2026-06-17T16:12:02+00:00

I am trying to write a binary tree and the add method is continually

  • 0

I am trying to write a binary tree and the add method is continually overwriting the root of the tree. I have two methods a recursive add method that takes in a String and a Node and then a regular add method that simply calls the recursive method. Any help would be greatly appreciated.

public   Node recAdd(String event , Node tNode ){
//tNode -> tree node         
  if (tNode == null){
  // Addition place found
    root = new Node(event); 
    System.out.println("added root");
  }
  else if (tNode.event.compareTo(event) <= 0){
   tNode.setLeft(recAdd(event, tNode.getLeft()));   
   // System.out.println("added left");// Add in left subtree
  }
  else{
    tNode.setRight(recAdd(event, tNode.getRight()));
    //   System.out.println("added right");
  }// Add in right subtree
  return tNode;
}

//////////////////////////////////////////////

public  void add(String event){
  if(root != null){
    System.out.println("The root currently is " + root.event );
   }
   recAdd(event , root);
}

And my node class is as follows

public class Node {
  Node left;
  Node  right;
  String event;

  Node(String event){
    this.event = event;
    this.left = null;
    this.right = null;  
  }

  public void setEvent(String event){
    this.event = event;
  } 
  public void setLeft(Node left){
    this.left = left;
  } 
  public void setRight(Node right){
    this.right = right;
  } 

  public  String getEvent(){
    return event;
  }

  public Node getLeft(){
    return left;
  }
  public Node getRight(){
    return right;
  }
}    
  • 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-06-17T16:12:03+00:00Added an answer on June 17, 2026 at 4:12 pm

    You are replacing the root node whenever you find a spot to insert the current node:

    public Node recAdd(String event, Node tNode) {
        //tNode -> tree node         
        if (tNode == null) {
          // Addition place found
          root = new Node(event);     // Problem is RIGHT HERE
          System.out.println("added root");
        }
      //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to write a recursive string method for a binary search tree
I am trying to write a binary function that takes two vectors(of the same
I am trying to write a code, which, given a Binary Search Tree root
I'm trying to write code for a binary search tree, the first method I'm
I am trying to write my own binary tree and I have problems with
I am trying to write simple Binary Search tree class (BST). When I add
I am trying to write myself the isEmpty method for a binary tree but
I am trying to write a program that allows a binary to be run,
I have a binary file to which I'm trying to write however I dont
I'm trying to write binary search tree's content to temporary array in order to

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.