There is also the small problem of working with trees as part of a program.
Clearly you can't keep on writing out
type names to specify a node of your choice – it’s too clumsy.
What actually happens in practice is that a pointer to a node is used as the “current” node and this moves its way down the tree by being set to the current nodes left or right child:
And so on.
You can generalize this to more than two child nodes.
Generally speaking tree algorithms are all about manipulating a "current node" pointer of some sort to achieve whatever it is you are trying to do - and it this is usually trying to find something stored in one of the tree nodes.
Searching a tree is mainly a matter of visiting the nodes and seeing what they contain. This is again another area where things can seem complicated because you can ask that every node is visited in a particular order usually to increase the efficiency of the search.
For example going down each branch as far as possible before starting again to go down the next branch is called “depth first”, while visiting all nodes at the same depth before going deeper, is called “breadth first”.
The jargon also gets more impressive - we talk of “traversing the tree” rather than "visiting the nodes" and eventually you will encounter the fact that trees and “recursion” go together.
How do you search a tree - you start at the root node and search its left sub-tree and if that doesn't work you search its right-sub tree.
As both of the sub-trees are just slightly smaller trees you can see that the search operation is the same for the sub-trees as for the complete tree. Repeat this recipe and you can search a tree without ever really having to write a search method.
Recursion is a whole topic in its own right and the source of the only good computer science joke I know –
dictionary definition of recursion “Recursion – see recursion”.
What are trees used for?
Well, everything from keeping track of where files are stored on a disk drive to analysing natural language and applications in artificial intelligence. It's worth point out that XML is a data language that can only describe tree structures so the idea must be powerful.
We often refer to things that are unpredictable as being "random" but this is not the same as truly random behavior - which is something we have to work hard to achieve. Put another way - how can a lo [ ... ]
John Conway's Life isn't just a fascinating program, it's an example of a cellular automaton. The theory of cellular automata (CA) sounds intimidating, but in fact it's simple and fun. It is a deep my [ ... ]