Note that there are some explanatory texts on larger screens.

plurals
  1. POTree,Nodes,Type of Trees
    text
    copied!<p><br> 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.<br> My Questions: <br> 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,<br></p> <p><strong>Tree class</strong></p> <pre><code>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); } } </code></pre> <p><br> <strong>bNode Class</strong></p> <pre><code>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())&lt;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); } } </code></pre> <p><strong>Main class</strong></p> <pre><code>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); } </code></pre> <p>}<br> Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload