Jul 27, 2008

Binary Tree

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Binary Tree

kvarnerexpress
Ok my program is supposed to read in from a file and create a linked list of chapters and each linked list is supposed to have a link to a binary tree that holds words that are related to each chapter so eventually I can code a search function

the input file is as follows

3
Drop_Box 10 need to drop box with secure box emergency drop urgent
Carpet_maintenance 7 grease remove immediately cover floor laundry man
Smoke_alarm 8 please floor scene urgently scene battery scene box

there are only 3 lines the top number indicates how many chapters there are and then each line contains the title of the chapter (which goes into the linked list) and the number of words to add to the tree and then the words follow

Code:

CODE
#include<stdio.h>
#include<string.h>
#include<stdlib.h>


struct tree_node         //Declaration of binary tree
{
char word[20];
int w_freq;
struct tree_node *left;
struct tree_node *right;

};


struct list_node         //Declaration of linked list
{
char title[50];
int num_words;
int height;
struct list_node *next;
struct tree_node *tree_link;
};


struct list_node *list;
struct tree_node *tree;

FILE *fp;
struct list_node *add_chapter(struct list_node *list);
struct tree_node *add_words(struct tree_node* tree, char[]);
void print_tree(struct tree_node *tree);
void print_list(struct list_node *list);


int main(void)
{
char file_name[50];
int num_of_words;
int num_of_chapters;
char word[20];


 printf("Enter the name of the file: ");    //Prompts the user to enter the name of the file
 scanf("%s", &file_name);
 fp = fopen(file_name, "r");
     
 if(fp == 0)
 {  
  printf ("\nCould not open file!\n");
  return 0;
 }
 fscanf(fp, "%d", &num_of_chapters);
 while(num_of_chapters > 0)
 {
   
  list = add_chapter(list);
  fscanf(fp,"%d", &num_of_words);
  while(num_of_words > 0)
  {
   fscanf(fp, "%s", word);
   tree = add_words(list -> tree_link, word);
   num_of_words--;
  }
  num_of_chapters--;
 }
 print_list(list);

}

struct list_node *add_chapter(struct list_node *list)
{
 struct list_node *p_new = NULL;
 struct list_node *p_cur = NULL;
 struct list_node *p_pre = NULL;
 char title[50];
 
 p_new = (struct list_node *)malloc(sizeof(struct list_node));
 
 
 fscanf(fp, "%s", title);
 strcpy(p_new -> title, title);
 p_new -> next = NULL;
 p_cur = list;
 
 
  if(list  == NULL)
  {
   return p_new;
  }            
 
 if(strcmp(p_cur -> title, title) > 0)  
 {                  
  p_new -> next = p_cur;
  p_cur = p_new;
  return p_cur;
 }
 
 
 while(p_cur -> next != NULL)
 {
    p_pre = p_cur;
  p_cur= p_cur -> next;
  if(strcmp(p_cur -> title, title) > 0)  
  {
   p_new -> next = p_pre -> next;
   p_pre -> next = p_new;
   return list;
  }
 }
 p_cur -> next = p_new;


return list;
}

struct tree_node *add_words(struct tree_node* tree,char word[])
{  

  struct tree_node *p_new = NULL;
  p_new = tree;
       
 
  if(p_new == NULL)
        {
   p_new = (struct tree_node *)malloc(sizeof(struct tree_node));
         strcpy(p_new -> word, word);
   p_new -> left = NULL;
   p_new -> right = NULL;
 
        }
  else if(strcmp(p_new -> word, word) > 0)
   p_new -> left = add_words(p_new -> left, word);
  else
   p_new -> right = add_words(p_new -> right, word);
  return p_new;  

}

void print_list(struct list_node *list)
{
struct list_node *p_cur = NULL;
p_cur = list;

while(p_cur != NULL)
{
 printf("%s", p_cur -> title);
 p_cur = p_cur -> next;
 print_tree(tree);
}

}


void print_tree(struct tree_node *tree)
{
if (tree !=NULL)
{
 printf(" %s", tree -> word);
 print_tree(tree -> left);
 print_tree(tree -> right);
}
}





my problem is it's crashing giving me a status access violation and I'm not sure why. I'm having trouble linking each node of the linked list to the binary trees that correspond to them.

if anyone has any suggestions I'd appreciate it

Notice from cmatcmextra:
Timestamp altered to a more reasonable time/date

 

 

 


Reply

switch
hey!

I haven't had a look at all of your code in heaps of depth, but from the basic level, it SEEMS like you're doing things the hard way. (I'm not entirely sure of it though smile.gif )

From the looks of your code, it seems that you are creating one chapter, and then that chapter has a pointer that links to another chapter, and so on and so forth in a recursive kind of way biggrin.gif... in relation to that, wouldn't you just be better off making an array of list_nodes, and then in each list_node make an array of tree_nodes? Once again, just a suggestion, I'm not entirely sure about that, as I don't quite understand if the tree structure has a specific purpose or if it's a means to an end.

On that note, you're crashes could be in relation to that- i've seen LOGO crash a few times from recursive function calling (mind you, LOGO is an old language). Apart from that, you seem to do a lot of memory management stuff. You might just want to CAREFULLY check over that: I've found heaps of bugs in my programs from memory management problems and I avoid it as much as possible!

Ultimately, i think you'd probably be better off just using a string class to hold each word, and then use arrays to link your list_nodes and tree_nodes together.

Ok, hope i've helped. Best advice is to keep trying hard at it, and eventually it'll work smile.gif

good luck bro.

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : binary

  1. Opening Files In Binary Format? - (3)



Looking for binary, tree

Searching Video's for binary, tree
advertisement



Binary Tree



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE