Re: Binary Tree
- From: "Foodbank" <v8killah03@xxxxxxxxx>
- Date: 10 Oct 2005 19:23:11 -0700
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]
.
- Follow-Ups:
- Re: Binary Tree
- From: mensanator@xxxxxxx
- Re: Binary Tree
- References:
- Binary Tree
- From: Foodbank
- Re: Binary Tree
- From: Malcolm
- Binary Tree
- Prev by Date: Re: Realloc destroys?
- Next by Date: Re: Stack Pointer..?
- Previous by thread: Re: Binary Tree
- Next by thread: Re: Binary Tree
- Index(es):
Relevant Pages
|