Hi!
I've got a few problems with implementation BST using an array. I have to use array, where 2i - left child, 2i + 1 - right child - 2/i - parent of i.
Maximum size of any table must be maxmium 4096 bytes (1024*sizeof(int)) - I don't know how to solve it(currently i've got simple table), maybe table of pointers at another table ??. The height of tree is maximum 30.
There are 3 operations: adding new item, finding max and min.
This is what I've done:
Finding max and min seems to not work, maybe because of bad adding function :PCode:int add(int *tree,int number,int *counter) { if(tree[*counter] == -1) { tree[*counter]=number; return 0; } else if(liczba > tree[*counter]) { *counter = 2*(*counter); add(tree,liczba,counter); } else { *counter = 1+(2*(*counter)); add(tree,liczba,counter); } } int max(int *tree,int i) { int max = tree[i]; if(tree[2*i+1] != -1) { max=tree[2*i+1]; max(tree,++i); } printf("%d\n",max); return 0; } int min(int *tree,int i) { int min = tree[i]; if(tree[2*i] != -1) { min=tree[2*i]; min(tree,++i); } printf("%d\n",min); return 0; }
Thanks for any help.



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.