I have a question I want to store values of my tree in a table in ascending order but first I just want to store all the values.
Could you help me with that ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int i=0;
noeud *a=r; //noeud are nodes and r are the racine
noeud *b=r;
while(a->fg!=NULL) // fg = left son
{
T[i]=a->cle; // cle = key (value of the nodes) and T is the table
i++;
if(a->fd!=NULL) // fd = right son
{
b=a->fd;
T[i]=b->cle;
}
a=a->fg;
}
Is the tree sorted already?
usually trees are done with recursion; it is tricky to write a loop to do it.
in pseudo code..
1 2 3 4 5 6 7 8 9 10 11 12 13 14
for a tree
a
/\
b c
\
d
it prints a b c d
void foo(tree)
{
if(!tree) return;
cout << tree.value << endl;
foo(tree->left);
foo(tree->right;
}
if its sorted you need to arrange the above to get the values in your sorted order to load the vector. If its not sorted, sort the vector after loading.
void AB::Tri()
{
int i=0;
fill_table(r,T,i);
//sort(T,T+n); // This doesn't work so I'm trying to sort myself but it's doesn't working also.
int temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(T[i]>T[j])
{
temp =T[i];
T[i]=T[j];
T[j]=temp;
}
}
}
}
use sort().
you have the syntax correct, so check n to ensure it is what you think it should be.
n should be the true size of the array or part you want to sort, eg
int x[10]
sort that with
sort(x, x+10)
n should have been incremented every time you added an item to your array, if you forgot this it will still be zero, and that will do nothing in the sort call...
the only difference is how you store the data.
you change your insert into the tree routine to follow this simple rule, starting at the root: if its less than the current node's sort key value, recursively go left, else go right. (If tree is empty, just put it at root, of course). Then you search the same way: is the item you are looking for less than (go left) or greater (go right) or equal (found it).
note that without some extra routines (balance routine or an advanced type of tree that self balances) the order you insert the data matters; for example if you insert 1,2,3,4 you will get a linked list, not a tree, but if you insert 2,3,1,4 you will get a decent search tree (do this on paper, see what I am saying!).
probably not. each recursive copy of the function resets i to zero (if you want to retain the value, it should be static, but attempting to mush the tree insert and the array iteration together looks dangerous/bug prone to me). This does not look right, but it may be, I can't honestly tell without time studying your code that I do not have right now.
if it were me, I would do it explicitly in distinct chunks:
for(i = 0; i < tsize; i++)
insert into tree (T[i]) //this can be named whatever, I am using words to explain not real code.
insert into tree function, then:
if null, insert here
if less than here, insert into tree->left
else
insert into tree tree->right
what it does:
it does recursion until it hits a null, and it inserts there. The logic sorts it, and because its a tree, you only need to search the height to find an item, making it on par with binary search.
not sure about what order you want to do things in.
you can get your data, put it into the array, sort it and insert it. To do that, insert from the center out of the sorted array, eg 1234567 you pick 4 (middle value) then insert 3 and 5, then 2 and 6, then 1 and 7 ... like that.
you can get your data, insert into sorted tree, and then use the correct traversal to load the array, and it will be sorted for you.
you should not be loading 2 trees, one sorted and one not, though, unless its for demonstration purposes. Figure out what you have and what you want to produce, and avoid unnecessary middle steps as much as you can (again, if you need the middle steps for a classroom problem, that is what it is).
say carefully what it is you want to do, exactly.
you can search a sorted table with a binary search (what you said above, which is unlike what you have been saying so far), or you can make a search tree, which does the binary search by design.
Also you may need to post your latest code, what you have been trying, as the above is several days old and you have been editing it a lot since then?
If its the tree, I don't know how to say it any clearer...
make the insert as described above. Add the data to the tree, and avoiding loading it in sorted order (either acending or decending, both are no good).