This article explains by example how a binary tree works.
A structure can't contain itself as a member, but it can include as members pointers to structures of the same type. Using this fact we can make sequences of structures called branches of a binary tree. Each tree has a root node structur and two branches (sequence of structures). The branches are organized as left and right branch. Each node of a branch (a structure) may also be splited to left and right branches of nodes etc.
First, we pass data to the root node.
Second, we receive new data and we compare them, under a desired property, with the data of the root node. If they have the same property we count the similarity, else we put the new data to the left or right branch for a new comparison with new incoming data, etc. So, we can sort data under a property.
The example C code is included at the book of Ted Van Sickle , pages 95 to 106 (an error :
you have to replace TNODE word with Tnode). Of course it needs a few of modifications to run. Malloc() needs to initialize the memory pool for allocation as we have a small amount of RAM memory inside of a microcontroller.
I have done some unimportant casts (we work with small integers) to stop compiler sounds for trunctated values.
We used new library functions included at <ctype.h> folder.
The C code is here the result
Example: Open Hyperterminal at 19200 kbaud, print on the screen aBcA ,then 'enter' and after it zero (The microcontroller receives the integer 0x30 and stops asking for a new word to send). Then you will see on the screen how many times a letter is appeared in the word aBcA.
Upper-case letters are transformed to low- case letters by the program.
At the code I added and a breakpoint. It helps to the watch window.Remove it.
In my opinion: Ted Van Sickle's book is a wonderful and almost complete book, playing a hard
"Rock dance " music about C. Enjoy! |