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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:13:08+00:00 2026-06-13T16:13:08+00:00

I am wandering what i have done wrong, could anyone help me please? I

  • 0

I am wandering what i have done wrong, could anyone help me please? I am trying to make a generic BST, with DDL(doubly linked list) data structure. The thing is my ADT is initalisated since my isEmpty method works, but my public addNewElement which sues private insert do not work. Any help and advices are welcome.

Interface:

public interface SortedSetBST <type> extends Iterable<type>{
            void addNewElement(Comparable<type> newElement);

    }

ADT:

package adt;

import java.util.Iterator;
import java.util.Iterator;

import interfaces.SortedSetBST;
import exceptions.*;

public class BinarySearchTree <type> implements SortedSetBST<type> {
    BinaryNode <type> root;
    int size;

    @Override
    public void addNewElement(Comparable  <type>  newElement) {
         insert(newElement, root);  
    }

protected BinaryNode  <type> insert( Comparable <type> x, BinaryNode <type>  t ) {
   if( t == null )
       t = new BinaryNode( x );
   else if( x.compareTo( t.element ) < 0 )
       t.left = insert( x, t.left );
   else if( x.compareTo( t.element ) > 0 )
       t.right = insert( x, t.right );
   else
       throw new DuplicateItemException( x.toString( ) );  // Duplicate
   return t;
}
class BinaryNode<type> {
    type element;      // The data in the node
    BinaryNode<type> left;         // Left child
    BinaryNode<type> right;        // Right child

    // Constructors
    BinaryNode( type theElement ) {
        element = theElement;
        left = right = null;
    }   
}

class App

public class App {
    public static void main(String [] args){
        System.out.println("   Main:");
        BinarySearchTree <String> db = new BinarySearchTree<String>();

        if(db.isEmpty() == true){
            System.out.println(".db empty!");
        }
        db.addNewElement("unu");
        db.addNewElement("doi");
        db.addNewElement("trei");
        System.out.println(db.getSize());
    }
}

output:

   Main:
.db empty!
0
  • 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-13T16:13:09+00:00Added an answer on June 13, 2026 at 4:13 pm

    This is clear.

    In “insert” you are checking if t is null. If this is the case, you initialize the parameter t with an instance of BinaryNode thinking you would also update root when it is first passed in being “null”. But “null” is “null” in Java and not a C-style-address as you might think!

    Then you return this parameter but the return value is lost in “addNewElement”.

    How to do this right:

    if( t == null ) {
       if (root == null){
          root = new BinaryNode(x);
          return root;
       } else {
          t = new BinaryNode( x );
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

wondering if anyone can guide me to what ive done wrong (or need to
I am wondering how this is done.Suppose i have a client application and it
I am trying to empty the tables but I was wondering have I got
I have a text file, I was wondering anyone have a batch file to
I have this small script and I was wondering if someone could tell me
I'm currently trying to make a button which will take the user back to
I have just been trying to fix a few memory leaks in my project
I'm wondering how this is typically done at a high level. Please correct me
Does anyone have or know of a good template / plan for doing automated
I am a new python user and I was wondering how I could make

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.