Re: Binary Tree



Hi,

Am I on the right path in the section of the code that adds new nodes?
I'm not finished, but trying to make an attempt. It's under the
/****CODE TO ADD NEW NODES AND COUNT REPEATS *****/ section. I'm
pretty sure that it's supposed to check if it's null and if so,
allocates memory for a new node. Syntax is probably wrong, but was
hoping for some input.

Thanks,
James

[code]
#include <stdio.h>
#include <malloc.h>
struct tnode { // specify the "shape" of a tnode
structure ...
struct tnode *left; // the left and right branch pointers
struct tnode *right;
int count; // the word count as before
char *word; // a pointer to the word

} *root; // declare the root pointer variable

struct tnode **tree_search(struct tnode **, char *);
void tree_stats(struct tnode *);
int get_word(char *);

int total_nodes, total_words, high;
struct tnode *most_frequent;


int main(int argc, char *argv[]) {
struct tnode **tpp;
char word_buff[100]; // the reusable word buffer
int i;
while(get_word(word_buff)) {
tpp = tree_search(&root, word_buff);
/****CODE TO ADD NEW NODES AND COUNT REPEATS *****/
///new code below here
if(root==NULL)
if(*tpp==NULL){
tpp=malloc(sizeof(struct tnode));
tpp->word = strdup(word_buff);
tpp->*left = NULL;
tpp->*right = NULL;
}
else statement here if there's a node there, increments count I
think, not sure which variables to use

}
[code]

.



Relevant Pages

  • Re: doubly-linked list & sorting
    ... have rewritten myself is the getword function. ... int getword(char *, int); ... void print_tree(struct tnode *); ...
    (comp.lang.c)
  • Re: doubly-linked list & sorting
    ... have rewritten myself is the getword function. ... ordered manner unless it is a very special hash (e.g. hash(int) = int). ... int getword(char *, int); ... void print_tree(struct tnode *); ...
    (comp.lang.c)
  • Re: doing things with binary trees
    ... // arnold7.c text input using binary tree ... int getline(FILE *, char *, int); ... void print_tree(struct tnode *); ...
    (comp.programming)
  • Re: adapting getline
    ... int getline(FILE *, char *, int); ... void print_tree(struct tnode *); ...
    (comp.lang.c)
  • Re: doubly-linked list & sorting
    ... struct tnode *left; ... char *strdup; ... void print_tree; ... int getword(char *, int); ...
    (comp.lang.c)