Trees. The Tree data structure has some similarities to their biological counterparts. They both have 'leaves', 'branches' and a 'root'. That's about where the similarities end. Let's go into a few more details about trees. But first, I like to think about trees containing "Nodes". Each node contains two items. A value and a collection of branches. The value is just the data that needs to be stored in the node and the branches are what other nodes that specific node points to.
Tree's are just a collection of nodes. A node can only be accessed through one unique path. The start-point being the top of the tree, called the root. The root is a unique node because no other nodes pointing to it. Each node has can have an unlimited number of branches, or even no branches. If a node has no branches, it is called a Leaf node. Leaf nodes signify the end of that specific chain of nodes.
Trees offer an interesting way to think about different things such as inheritance and auto text complete.
Atleast for single inheritance (where you can only inherit one class such as in java) we can think of the "object" class being the root of all other classes. Object inherits from no other class, and therefore has no other nodes pointing to it. Below the root, we have other classes which all occur below Object. And we can make our own classes and super classes which follow this same tree/inheritance structure (and all of them inherit from the root: object).
In the case of auto-complete, we can think of each node having a combination of letters, where the ones near the top are very short combination of letters (1-3 letters) and each of those link downwards to more complex words. So the auto-complete can guess what word you will type in based on what you are actively typing in. Ofcourse it isn't a simple tree, but it follows a lot of the same ideas.
No comments:
Post a Comment