home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
POINT Software Programming
/
PPROG1.ISO
/
pascal
/
binhelp
/
binary.doc
next >
Wrap
Text File
|
1993-04-26
|
4KB
|
94 lines
-------------------------------------------------------------------------
This code is released as public domain to anyone who wishes to use it
for learning purposes. This was a building block in a project that is
under construction, and may be able to help your understanding of Binary
Trees. I hope this program can help you out.
Sinerely in Jesus Christ!
Romans 10:9-17
-------------------------------------------------------------------------
The object of binary trees is to lesson search time for a load of records.
If you have 3000 records of information for sorting, you know that it
can take a long time to search through all of these records. If you
can sort by position (i.e. This is less, or greater then another element)
then you probably can find a better way of storing the information.
For example: If you wanted to search for phone numbers then you could use
a binary tree, because the computer can interpret 555-5555 as
being greater then 000-0000.
If you have a list of states, the computer can interpret
Arkansas as being before Hawaii.
Trying to get the computer to sort for parts of words, or
other things, will take some more thinking out. And you may
find that sorting through that information takes a much more
time consuming process. There are some possible routes to
follow, but this program does not deal with those.
Say you needed to sort these fruits as they came in:
Apple
Orange
Banana
Kiwi
Pear
Grapes
In a binary tree it would look like this:
Apple(1)
(No left node) ─────────┴─────────┐
Orange(2)
┌─────────┴─────────┐
Banana(3) Pear(5)
(No left node) ─────────┴─────────┐
Kiwi(4)
┌─────────┴───────── ( No right node )
Grapes(6)
Since orange is higher alphabetically in order, it goes to the right
of apple which is placed first. Then Banana, it is greater then Apple,
but less then orange so it goes to the left of orange. Kiwi is greater then
apple, less then orange, and greater then banana; hence, it goes to the
right of banana. Pear is greater then apple, and orange it moves to the
next slot (right node). Grapes is greater then apple, less then orange,
greater then banana, and less then Kiwi; hence, it goes to the left of
Kiwi.
Confusing? Try figuring out how you would place a few more names on the
table and where they would go. When the tree is looked at in a number
sense you end up like this:
Node Left Parent Right Word
-------------------------------------
1 0 0 2 Apple
2 3 1 5 Orange
3 0 2 4 Banana
4 6 3 0 Kiwi
5 0 2 0 Pear
6 0 4 0 Grapes
A parent node is the node that the word branches from. 0's mark empty
paths. Try running the program a few times, and diagram the nodes, and
see what you come up with.
-------------------------------------------------------------------------
This code is released as public domain to anyone who wishes to use it
for learning purposes. This was a building block in a project that is
under construction, and may be able to help your understanding of Binary
Trees. I hope this program can help you out.
Sinerely in Jesus Christ!
Romans 10:9-17
-------------------------------------------------------------------------