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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:01:25+00:00 2026-06-18T14:01:25+00:00

I want to create a tree that detect if the insert is a Object

  • 0

I want to create a tree that detect if the insert is a Object of type Characters it will compare each one and decide where to insert [ right or left ],( i know it can detect by position in ascii table) ,and if the insert is an object of int it will do the same operation.
My Questions:

1. I need to create the tree and on the same time to set a compartor ( for example if its a tree of Chars it will be a Chars_comperator that checks Chars and he implements Comparator ( of java ).?
2. My code now is good for int only. becuase i take the object convert to string and then to int and after all this i compare and decide where to insert, this is how i need to do it? or there is another way to do it that can take care all of kinds of Objects?
Here is my code and how i create the tree,

Tree class

public class tree {

bNode root;
public tree() {
    this.root = null;
}
public boolean isEmpty(){
    return root==null;
}
public void insert(Object data)
{
    if(isEmpty())
        this.root = new bNode(data);
    else
        this.root.insert(data);

   }
 }

bNode Class

public class bNode {
 protected Object data;
 protected bNode left;
 protected bNode right;


public bNode(Object data) {
    this.data = data;
    this.left = null;
    this.right = null;
}

public void insert(Object data){

    if(Integer.parseInt(data.toString())<Integer.parseInt(this.data.toString())){
        if(this.left==null)
             this.left = new bNode(data);
        else 
            this.left.insert(data);

    }
    else{
        if(this.right==null)
             this.right = new bNode(data);
        else 
            this.right.insert(data);



    }
}

Main class

public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    tree x = new tree();
    char a = 'G';
    x.insert(a);
    x.insert(60);
    x.insert(40);
    x.insert(30);
    x.insert(59);
    x.insert(61);
    x.root.printTree(x.root);


}

}
Thanks!

  • 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-18T14:01:27+00:00Added an answer on June 18, 2026 at 2:01 pm

    instead of passing an Object, you could pass a Comparable in insert().
    Standard type like Integer, String, etc. already implement the Conparable interface.

    instead of using if (a <b) you call

    compareTo(a,b);
    

    See java doc of Comparable.

    If, for any reason, you want to stay with Passing an Object to insert(), you also can solve that by not using toString, but by checking the class of object, and then casting:

    if (object instanceof Integer) {
        int val = ((Integer) object).intValue();
        // now compare 
    } else if (object instance of String) {
         String val .....
        // use val.compareTo()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to create a tree view that can get its
I want to create binary search tree data structure in C/C++ where each node
I want to create a tree that it's child nodes contain specific flex components
I want to create an application that will automate a very specific build process
I want to create a tree structure in my program. Right now I have
My problem is that I want to create xml tree and get a simple
I want to create tree structure using web service. I have used bottom up
I want to create a tree panel in sencha touch, but I am unable
I want to create a classification tree in Matlab using classregtree. However, a lot
I am trying to implement a B+ tree myself, but I want to create

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.