简体   繁体   中英

Putting words from a file into a binary tree (C)

I'm currently learning C in Uni but for some reason it's just so difficult for me. I couldn't find a simple step by step guide and everything that's on the internet is just so complex and without much explanation. I'm supposed to write this program:

'Using the binary tree and list write a program that reads a text file and prints to the output file all the words in alphabetical order along with the line numbers in which the word occurs.'

And I just don't know how to start it. I can open files, run it from the command line, but I have no idea how to create a binary tree, get the words from a file and put them there, and then create a list inside the binary tree. All the examples I've found are so different that I don't know how to rewrite them so they would work for me. Could anyone help? Even a few lines of code that would guide me in the right direction would help so much!

For starters, its binary-search tree ,(special kind of binary tree) that is required for the given problem.

A binary search tree , is a binary tree, that is populated with comparable objects, like numbers. Meaning given two numbers x and y , the following three boolean conditions can be answered without any ambiguity.

x greater than y

x less than y

x equal to y

Now a binary search tree is built upon the above boolean conditions. The analogy here is that words are also comparable , which decides there order in a typical oxford dictionary . Like apple < box and therefore, apple comes before box in alphabetical order.

How to get alphabetical order of words?

Once you have populated your tree, a simple inorder traversal will do the rest, that is, listing the words in alphabetical order. Just remember to also have variable for line numbers, which can be stored at the same time you are building your tree, which can later be retrieved while printing words in order.

Take the code as an exercise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM