Binary Trees

I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked lists. I'm totally new to binary searches by the way. Can anyone help me with the commented sections below? Much of the code such as functions and printfs has already been completed. Any help is greatly appreciated.

Thanks, James

[code] #include#includestruct 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

Comments

: Hi all, : : : I'm trying to do a binary search and collect some stats from a text : file in order to compare the processing times of this program (binary : searching) versus an old program using linked lists. I'm totally new : to binary searches by the way. Can anyone help me with the commented : sections below? Much of the code such as functions and printfs has : already been completed. Any help is greatly appreciated. : : : Thanks, : James

[blue]Can't do binary searches on text files -- they contain random-length records, so there is no way to determine beforehand where the beginning of a randomly chosen record starts. And that is probably why the program uses linked lists -- read the file into memory and then you can do some sort of in-memory searching.[/blue]

Thanks for the reply Stober. However, according to my book, it is possible to do it with a text file, but it's not very reassuring that someone like you with alot more coding experience than me says it can't be done ).

: Thanks for the reply Stober. However, according to my book, it is possible to do it with a text file, but it's not very reassuring that someone like you with alot more coding experience than me says it can't be done ). : : Thanks,: James:

: Thanks for the reply Stober. However, according to my book, it is possible to do it with a text file, but it's not very reassuring that someone like you with alot more coding experience than me says it can't be done ). : : Thanks,: James:

As stober says, you must read the whole file and place it in memory.To to a binary search, the data must be sorted. So if the file isn't sorted, you must add the items to the linked list so that it becomes sorted, for example in alphabetic order.

A binary tree might be more efficient than a regular linked list, I don't know. You must balance it all the time, which make take a few extra ticks. Hash tables are usually the most efficient when dealing with large amounts of data.

[b][red]This message was edited by Vilanye at 2005-10-10 15:12:21[/red][/b][hr]: Thanks for the reply Stober. However, according to my book, it is possible to do it with a text file, but it's not very reassuring that someone like you with alot more coding experience than me says it can't be done ). : : Thanks,: James:

You can do it with a binary tree, but it might not make sense. Read a line(perhaps more then one line if it makes sense to do it), add that line to the tree, rinse and repeat. It would make sense to do it this way if it were numerical values,single word, or in a format that would lend itself to a sorting routine.

Here is a text file with integral values:

576455377435767686767622457899345422

You could create a binary tree with that. If it were random sentences, you could still build a tree with the data, but wouldn't make as much sense.

In short, if there is a way to determine which object/struct is less then or equal to, you can put it in a binary tree. Where the data comes from is irrelevant.

If you have so much data that it can not be stored in memory, you can use external storage, such as B-trees and manipulate them as if they were in a tree. This isn't a good option for smaller amounts of data, especially if it is accessed often, as disk IO is slow. [italic][blue]Just my 2 bits[/blue][/italic]

Thanks for the replies so far guys. I've tried to make an attempt at the first part I need to program. How's it look?

Hi,

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#includestruct 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