问题
1)I have a structure
struct node
{
char symbol;
int freq;
int left, right,root;
int value;
short is_sorted;
};
struct node data[1000];
where data[215].freq (frequency) {0,1,2,3,4,5} is obtained by reading following alphabetical symbols(symbol) (abcdef) values from a file "Input.txt" as a sole argument.Now i have to add two minimum frequencies of this array and then position the newly element obtained in the same array such that it will maintain the increasing order of the freq(It was already sorted array, see we have 0,1,2,3,4,5).
(2)I also have to take care that the two minimum added elements must not participate in sorting and addition again, they must be fixed at their position once if they are already added, but the newly obtained element by addition can participate in addition and sorting again. eg: we add two minimum element 0 and 1, 0+1=1, so "1" is the result obtained by addition, now this "1" must be positioned in data[].freq such that still there should be increasing order. so :
data[i].freq = 0 1 1(added here) 2 3 4 5
Now we have to again find the minimum two nodes (please read the comment (2) again to understand well) .We cannot add 0 abnd 1 again because they have already participated in in the addition. so this time we will add 1 and 2(this one is at index three, please don't get confused wwith the one at index two). so we get 1+2=3
0 1 1 2 3 3 4 5 we again positioned 3 to maintain increasing order. we repeat again: for element at index 4 and 5(because we have already done addition for element at index 0,1 and 2,3) we will get 3+3=6, again position it in data[].freq.
0 1 1 2 3 3 4 5 6 this time 6 is greater then 4 and 5 so it will appear after 5 to maintain increasing order.
At last we will get data[dataSize].freq
like this:
data[dataSize].freq= [0 1 1 2 3 3 4 5 6 9 15].
so the addition held was between index 0,1 and 2,3 and 4,5 and 6, 7 and 8,9 and at last we have 15 which is last one, so here we stops.
This part i have already done in my code.
What help do i need ?
When i run the code the shows data will look like this:
Symbol Freq Left Right
(It's actually a huffman tree implementation using only arrays,No malloc and No pointers).
The expected output is this: (when i use qsort() it works fine but i am not using qsort() because of complexity reasons):
./extended Input.txt
Reading file...
Data is:
0: symbol: a, Freq: 0, Left: -1, Right -1
1: symbol: b, Freq: 1, Left: -1, Right -1
2: symbol: 0, Freq: 1, Left: 0, Right 1
3: symbol: c, Freq: 2, Left: -1, Right -1
4: symbol: d, Freq: 3, Left: -1, Right -1
5: symbol: 5, Freq: 3, Left: 2, Right 3
6: symbol: e, Freq: 4, Left: -1, Right -1
7: symbol: f, Freq: 5, Left: -1, Right -1
8: symbol: 4, Freq: 6, Left: 4, Right 5
9: symbol: 3, Freq: 9, Left: 6, Right 7
10: symbol: 2, Freq: 15, Left: 8, Right 9
d: 00
a: 0100
b: 0101
c: 011
e: 10
f: 11
So I have implemented the algorithm i mentioned above and it works well for printing frequency(Freq) But it don't print the symbols and Left and Right child correctly, I am trying since long but not able to get the point where i have mistake in my code.(I am sure that tree creation part is correct (which is traverse)tree() function, The problem is some where in the code written in main() function , moreover i have commented the suspected part). Below is my code:
nt main(int argc, char **argv)
{
int i,count=0,j,f,s;
struct node data[1000];
int data_size=-1;
if(argc<2)
{
printf("Provide input file..");
return;
}
printf("Reading file...\n");
read_data(argv[1], data, &data_size);
printf("datasize: %d\n", data_size);
for (i=0; i+1<data_size; i+=2)
{
//printf("count1: %d\n", count);
data[data_size].symbol='0'+count;
data[data_size].freq=data[data_size].root =data[i].freq+data[i+1].freq;
data[data_size].left=i;
data[data_size].right=i+1;
data[i].value=0;
data[i+1].value=1;
data[i].is_sorted=1;
data[i+1].is_sorted=1;
data[data_size].value='\0';
data[data_size].is_sorted=0;
for (j=data_size-1; j>f+1; j--)
{
if (data[data_size].root >=data[j].freq)
{
// printf("Inside if loop\n");
break;
}
else
{
// printf("else loop\n");
data[j+1].freq=data[j].freq;
data[j+1].symbol=data[j].symbol ;
data[j+1].left=data[j].left ;
data[j+1].right=data[j].right ;
//data[j+1]=data[j];
}
}
////////////////
//printf("data[j+1].sym1: %c\n", data[j+1].symbol );
data[j+1].freq=data[data_size].root ;
data[j+1].symbol=data[data_size].symbol ;
data[j+1].left=data[data_size].left ;
data[j+1].right=data[data_size].right ;
count=count_unsorted(data,data_size);
data_size++;
printf("count2: %d\n", count);
}
printf("Data is:\n");
for(i=0;i<data_size;i++)
{
printf("%d: symbol: %c, Freq: %d, Left: %d, Right %d\n", i,data[i].symbol, data[i].freq, data[i].left, data[i].right);
}
char path[100]={'\0'};
traverse_tree(data, data_size-1,path);
return 0;
}
Bu the output of my code is:
Reading file...
0: symbol: a, Freq: 0, Left: -1, Right -1
1: symbol: b, Freq: 1, Left: -1, Right -1
2: symbol: f, Freq: 1, Left: -1, Right -1
3: symbol: c, Freq: 2, Left: -1, Right -1
4: symbol: d, Freq: 3, Left: -1, Right -1
5: symbol: f, Freq: 3, Left: -1, Right -1
6: symbol: e, Freq: 4, Left: -1, Right -1
7: symbol: f, Freq: 5, Left: -1, Right -1
8: symbol: 2, Freq: 6, Left: 4, Right 5
9: symbol: 3, Freq: 9, Left: 6, Right 7
10: symbol: 4, Freq: 15, Left: 8, Right 9
f: 11
e: 10
f: 01
d: 00
hp@ubuntu:~/Desktop
When i try to do :
printf("data[j].sym: %c\n", data[data_size].symbol );insde the commented part (which is the most suspected part where i assin the values to element and symbols)" then i have very strange value of symbol which is "data[j].sym: f data[j].sym: f data[j].sym: 2 data[j].sym: 3 data[j].sym: 4 "
I don't know why it has "f" at first two places , there should be "0 1 2 3 4
" not "f f 2 3 4
"
回答1:
So at last,I have just done it myself:Here i put code for someone if getsimilar type of problem in future:
void sort_array(struct node *data, int length)
{
int i,j;
for(i=0;i<length-1 && data[i].freq<=data[length-1].freq;i++); //This loop breaks when it gets the desired position, So complexity is about n-1 for total n elements.
for(j=length-1;j>i;j--)
{
struct node temp;
temp.symbol=data[j].symbol;
temp.freq=data[j].freq;
temp.left=data[j].left;
temp.right=data[j].right;
temp.value=data[j].value;
data[j].symbol=data[j-1].symbol;
data[j].freq=data[j-1].freq;
data[j].left=data[j-1].left;
data[j].right=data[j-1].right;
data[j].value=data[j-1].value;
data[j-1].symbol=temp.symbol;
data[j-1].freq=temp.freq;
data[j-1].left=temp.left;
data[j-1].right=temp.right;
data[j-1].value=temp.value;
}
}
来源:https://stackoverflow.com/questions/21295201/structure-array-giving-wrong-output-on-sorting