LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   please help "structures" in c (https://www.linuxquestions.org/questions/programming-9/please-help-structures-in-c-485965/)

thtr2k 09-22-2006 07:15 AM

please help "structures" in c
 
Can someone please help ... I have an array and in this array each element contains a binary tree. How do i compare the string (same string) in the binary tree of array[1] with array[2]


array [1,2, ....]
eg;
array[1] array[2]
| |
root root
/ \ / \
left right left right
/ \ / \
... ... ... ...
/ \
hello hello

my declarations are like this:

typedef struct {
char *mystring;
}mydata;

and in my function
void myfunc(...){
mydata *p;
....
/* i'm stuck here */
....
}

pankaj99 09-22-2006 07:46 AM

your question is not clear.
Give a full description of what you are trying to do.

can you post the code
1) how you have constructed the array
2)data structure for a node of the binary tree.

what you are trying to do in myfunc?

thtr2k 09-22-2006 09:52 AM

void
print_unique_substring(tree_t *treeArray[],int fnum){
int i,j;
tree_t *treeA, *treeB;
data_t *p;
...
...
printf("print unique substring function\n");
printf("===============================\n");

treeA = treeArray[1];
traverse_tree(treeA, print_tree);
treeB = treeArray[2];
traverse_tree(treeB, print_tree);
/* code to compare the substrings */
/* stuck */
}

my output from running program:

print unique substring function
===============================
A 4
AA 2
AAG 1
AAGA 1
AAGAA 1
AG 1
AGA 1
AGAA 1
G 2
GA 2
GAA 2
GAAG 1
GAAGA 1
GAAGAA 1
=================================
A 2
AA 1
AAG 1
AAGC 1
AG 1
AGC 1
C 2
CG 1
CGA 1
CGAA 1
CGAAG 1
CGAAGC 1
G 2
GA 1
GAA 1
GAAG 1
GAAGC 1
GC 1
=================================

i want the output to be --->
same substring from file1 & file2:
=================================
A 4 2
AA 2 1
AAG 1 1
AG 1 1
G 2 2
GA 2 1
GAA 2 1
GAAG 1 1

i hope this makes my problem a liltle bit more clear. Thanks

xhi 09-22-2006 02:23 PM

i dont know what exactly you are doing, are you just looking for a string compare? like strcmp, or are you looking for an algorithm for searching the strings..

im not following exactly, if you havent yet do a
man strcmp

thtr2k 09-22-2006 06:38 PM

still not clear? i wonder my description... sorry
addition
problem description:
i have 2 files to read in from command line argument, content of file1 and file2 are: GAAGAA\n and CGAAGC\n respectively. what i need to do is to format these strings into substrings (i already worked out my substring part and printed out the output from above) and store them in treeA and treeB. Once that is done, i have to compare the substrings that are the same (eg, from above in my treeA and my treeB both substrings (A, AA, AAG, G, GA, GAA, GAAG) occur in 2 treeA & treeB. i want to know how to know how to do this (ie. able to find out that both same substrings occur in treeA & treeB). if i use strcmp how to i know what to compare.

typedef struct {
char *substring; /* pointer to a substring */
int freq; /* counter for the substring(s) */
}data_t;

void
print_tree(void *x){
data_t *p=x;
printf("%s %d \n", p->substring, p->freq);
}

pankaj99 09-22-2006 07:37 PM

perhaps you should be using a 'trie' data structure.
see if it helps you.
just google 'trie'.

sundialsvcs 09-22-2006 08:49 PM

Just to clarify ... A data structure of this kind (a "tree") is not a struct.

A struct is a name given to a chunk of contiguous memory which might consist of a number of subfields.

"A binary tree" is a thing that is physically composed of nodes and such (each of which are structs), linked by pointers. Very hairy stuff.

A binary tree is also a perfect example of what (should be) a C++ "object" or "class." With such a thing, you can create a instance of "a binary tree," and "put into it" various strings, then "see if the two trees are equal."

When you are using "a binary tree," you don't care and certainly shouldn't have to care how it is physically implemented. A class enables you to do that. You create "this thing," and you tell this thing to "do stuff" (like put a string into itself), and you ask it "questions," like "are you equal to this tree over here?"


All times are GMT -5. The time now is 04:29 PM.