LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2006, 07:15 AM   #1
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Rep: Reputation: 15
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 */
....
}
 
Old 09-22-2006, 07:46 AM   #2
pankaj99
Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 47

Rep: Reputation: 15
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?
 
Old 09-22-2006, 09:52 AM   #3
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Original Poster
Rep: Reputation: 15
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
 
Old 09-22-2006, 02:23 PM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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
 
Old 09-22-2006, 06:38 PM   #5
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Original Poster
Rep: Reputation: 15
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);
}
 
Old 09-22-2006, 07:37 PM   #6
pankaj99
Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 47

Rep: Reputation: 15
perhaps you should be using a 'trie' data structure.
see if it helps you.
just google 'trie'.
 
Old 09-22-2006, 08:49 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,610
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
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?"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
what is "sticky bit mode" , "SUID" , "SGID" augustus123 Linux - General 10 08-03-2012 04:40 AM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration