Binary Tree Help

I have a project where I need to build a binary tree with random floats and count the comparisons made. The problem I'm having is I'm not sure where to place the comaprison count in my code. Here's where I have it:

publicvoid insert(float idata)

{

Node newNode =new Node();

newNode.data = idata;

if(root==null)

root = newNode;

else

{

Node current = root;

Node parent;

while(true)

{

parent = current;

if(idata < current.data)

{

comp++;

current = current.leftc;

if(current ==null)

{

parent.leftc = newNode;

return;

}

}

else

{

current = current.rightc;

if(current ==null)

{

parent.rightc = newNode;

return;

}

}

}

}

}//end insert

Do I have it in the right place? Also, if I'm building the tree for 10,000 numbers would I get a new count for each level or would I get one count for comparisons? I'd appreciate anyone's help on this.

[2019 byte] By [Nic_C78a] at [2007-11-27 8:56:16]
# 1
Plz give implementation of Node
OnlyForJavaa at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 2
What do you mean? You need to see all of my code?
Nic_C78a at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 3
if idata is equal to, or larger than current.data then you're not increasing your counter, but you DID make a comparison. Move the comp++; above theif(idata < current.data).
prometheuzza at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 4
Sweet, Thanks!
Nic_C78a at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 5
> Sweet, Thanks!You're welcome.
prometheuzza at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 6
one more question. Should I only be receiving one count of comparisons or will I get several for each level? I'm supposed to be generating a random set of 10,000 floats and keeping track of the number of comparisons. I'm getting many different numbers in my output.
Nic_C78a at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 7

> one more question. Should I only be receiving one

> count of comparisons or will I get several for each

> level?

One comparison will represent one hop to the next level of your tree.

> I'm supposed to be generating a random set

> of 10,000 floats and keeping track of the number of

> comparisons. I'm getting many different numbers in

> my output.

That is not surprising at all. Take the following trees with the same nodes for example:

Tree A, insert order: 5,3,7,1,6,8,4

5

/ \

/\

37

/ \/ \

24 68

Tree B, insert order: 7,8,6,5,4,3,2

7

/ \

68

/

5

/

4

/

3

/

2

Now try inserting the number 1 in both trees.

prometheuzza at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 8

what I mean is, I'm getting a list of numbers in my output like this as an example:

0

3

4

16

20...

It keeps going until it reaches somewhere around 30 thousand. Is that right or do I have something wrong in my code? I thought I should just get one number telling me the amount of comparisons made.

Nic_C78a at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 9

> what I mean is, I'm getting a list of numbers in my

> output like this as an example:

>

> 0

>

> 3

>

> 4

>

> 16

>

> 20...

>

> It keeps going until it reaches somewhere around 30

> thousand. Is that right or do I have something wrong

> in my code? I thought I should just get one number

> telling me the amount of comparisons made.

You never reset the comp variable, so each time you insert into the tree, it adds the number of inserts to the previous value.

hunter9000a at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 10

> what I mean is, I'm getting a list of numbers in my output

> like this as an example:

> ...

I looks like you're printing your counter while a test is running. Only print it when you're done testing.

> It keeps going until it reaches somewhere around 30

> thousand. Is that right or do I have something wrong

> in my code? I thought I should just get one number

> telling me the amount of comparisons made.

Yes, I thought that that was your aim. And that number can vary quite a lot between different tests like I showed in my example.

prometheuzza at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 11
> ...> You never reset the comp variable, so each time you> insert into the tree, it adds the number of inserts> to the previous value.Yes, or something like that. I'm not sure what the OP really means.
prometheuzza at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 12

> > ...

> > You never reset the comp variable, so each time

> you

> > insert into the tree, it adds the number of

> inserts

> > to the previous value.

>

> Yes, or something like that. I'm not sure what the OP

> really means.

Yeah, it's hard to be sure without seeing the rest of the code.

hunter9000a at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 13

> > what I mean is, I'm getting a list of numbers in my

> output

> > like this as an example:

> > ...

>

> I looks like you're printing your counter while a

> test is running. Only print it when you're done

> testing.

>

>

> > It keeps going until it reaches somewhere around

> 30

> > thousand. Is that right or do I have something

> wrong

> > in my code? I thought I should just get one

> number

> > telling me the amount of comparisons made.

>

> Yes, I thought that that was your aim. And that

> number can vary quite a lot between different tests

> like I showed in my example.

ok, so the last number that I see in my output is the total comparison count. Am I understanding that correctly?

Nic_C78a at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 14

> > > ...

> > > You never reset the comp variable, so each time

> > you

> > > insert into the tree, it adds the number of

> > inserts

> > > to the previous value.

> >

> > Yes, or something like that. I'm not sure what the

> OP

> > really means.

>

> Yeah, it's hard to be sure without seeing the rest of

> the code.

Sorry, I thought I had already posted my code for you to look at.

Here's a copy of it:

class Node

{

public float data;

public Node leftc;

public Node rightc;

}

public class Btree

{

private Node root;

private int comp;

//-

public Btree(int value)

{

root = null;

}

//-

public void insert(float idata)

{

Node newNode = new Node();

newNode.data = idata;

if(root==null)

root = newNode;

else

{

Node current = root;

Node parent;

while(true)

{

parent = current;

comp++;

if(idata < current.data)

{

current = current.leftc;

if(current == null)

{

parent.leftc = newNode;

return;

}

}

else

{

current = current.rightc;

if(current == null)

{

parent.rightc = newNode;

return;

}

}

}

}

}//end insert

//

public void display()

{

//System.out.print();

System.out.println("");

System.out.println(comp);

}

} //end Btree

//-

class BtreeApp

{

public static void main(String[] args)

{

int value = 10000;

Btree theTree = new Btree(value);

for(int j=0; j<value; j++)

{

float n = (int) (java.lang.Math.random() *99);

theTree.insert(n);

theTree.display();

}

}

}

>

Nic_C78a at 2007-7-12 21:18:51 > top of Java-index,Java Essentials,New To Java...
# 15
> ...> ok, so the last number that I see in my output is the> total comparison count. Am I understanding that> correctly?Yes, if all you N-nodes have been inserted, the counter will hold the total number of hops made by all nodes.
prometheuzza at 2007-7-21 22:50:36 > top of Java-index,Java Essentials,New To Java...
# 16
> ...> Sorry, I thought I had already posted my code for you> to look at. > Here's a copy of it:> > ...Ok, just move the linetheTree.display();in your BtreeApp class after the for-statement is done inserting.
prometheuzza at 2007-7-21 22:50:36 > top of Java-index,Java Essentials,New To Java...
# 17
that fixed it. Thank you so much for your time and help!!!
Nic_C78a at 2007-7-21 22:50:36 > top of Java-index,Java Essentials,New To Java...