• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with creating an array for numbers using binary tree concept  RSS feed

 
yu wong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys i'm very new to java and need help to create my first app .

What i'm trying to do:
input some numbers
create a binary tree in array form. The tree is complete.

The problem:
There seem to be some problems with my loops as I only get the two numbers after the root node and others are gone. The two numbers i get are usually near the end of a list of random numbers.




Thank you in advance
 
binhua zhong
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yu wong wrote:Hey guys i'm very new to java and need help to create my first app .

What i'm trying to do:
input some numbers
create a binary tree in array form. The tree is complete.

The problem:
There seem to be some problems with my loops as I only get the two numbers after the root node and others are gone. The two numbers i get are usually near the end of a list of random numbers.




Thank you in advance



my englist is bad!
can you tell me ? what is your enter? then what are you want get! For example.....
 
yu wong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@binhua, Thank you for your reply

First, I input numbers into argument e.g. 23 35 80 2 13 56 11 60 71

my binary tree for this is:

23
/ \
2 35
\ \
13 80

Note 80 is meant to be under 35 not 2...

etc...

In my programme, an array for it would look like (23, 2, 35, 0, 13, 0, 80,..... etc)

 
binhua zhong
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first(Integer.parseInt(args[i]) > treeArray[k]) && (zeroArray[(2*k + 2)] = true))

is (zeroArray[(2*k + 2)] == true)?
 
yu wong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@binhua, Thank you for spotting that mistake

corrected that and array is all zeros except for index 0.

More help would be grateful
 
binhua zhong
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,I have forgotten the binary tree ;
you can reference the promgram
public class ABTree {
static int[] xx = new int[]{23, 35, 80, 2, 13, 56, 11, 60, 71};
static int MaxNum = 60;
int[] node = new int[MaxNum];
public ABTree() {
for (int i = 0; i < MaxNum; i++) {
node[i] = 0;
}
}
public void createTree(int data) {
int level = 1;
while (node[level] != 0) {
if (data > node[level]) {
level = 2 * level + 1;
} else {
level = 2 * level;
}
}
node[level] = data;
}
public void print() {
for (int i = 1; i < MaxNum; i++) {
System.out.println("data"+ i + ": " + node[i]);
}
}
public static void main(String[] args) {
ABTree t = new ABTree();
for(int i= 0;i<xx.length;i++){
t.createTree(xx[i]);
}
t.print();
}
}
 
yu wong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@binhua, thank you for the solution.
Would you mind to tell me what's wrong with my codes?
 
binhua zhong
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yu wong wrote:@binhua, thank you for the solution.
Would you mind to tell me what's wrong with my codes?


Sorry!I can't explain now! I need to learn the binary tree algorithm!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yu wong wrote:Hey guys i'm very new to java and need help to create my first app .

And you chose this? Phew. You're ambitious.

However, I suspect what you actually want is a Heap. And the normal process for adding to a heap is as follows:
  • Add the new value to the end of the array.
  • "Heapify" the array.

  • Tips:
    1. You're likely to be in for a long and winding road if you try to do this all in main().
    2. Don't use labeled loops. EVER (or at least not for a long time yet). I honestly can't think of a situation that requires them - and yours definitely doesn't.

    I believe the page I linked provides an example of a heap implementation. If not, Google "Java Heap implementation". But I warn you: you'll need to understand how recursive methods work.

    That's why I say: Very ambitious for a first app.

    Winston
     
    binhua zhong
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    binhua zhong wrote:
    yu wong wrote:@binhua, thank you for the solution.
    Would you mind to tell me what's wrong with my codes?


    Sorry!I can't explain now! I need to learn the binary tree algorithm!
     
    binhua zhong
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!