Introduction
- So far, all data structures have been "linear": items follow one
after another
- Next comes trees: number of items following another item may vary
- Advantages: Insert/delete are fast; find is fast (usually)
Tree Basics
- What is a tree? Consider the following example:
Terms
- Node: data (the value we actually care about) and key (the value
used to find the data), plus branches to other nodes. All of the above
circles are considered to be nodes of the tree
- Edges: The "branches" mentioned above. An edge is each line drawn in
the image. The branches are usually considered to be directional: if you
have an edge from A to D, you are not allowed to follow the edge back from
D to A.
- Each branch leads to a child. B - N are all children of some
kind.
- All of the fellow children are known as siblings. B, C, and D
are all siblings.
- All siblings have a common parent. However, a child is
usually not aware of its parent: instead, parents keep track of only their
parents. B, C, and D all have the common parent, A. A node is only
allowed to have one parent.
- There is a special node which has no parent: the root. Above,
A is the root node.
- The number of children a node has is the the degree.
A and J are of degree 3; K is of degree 0: it has no children.
- Some nodes have no children: these are known as leafs. E, G,
K, L, M, and N are all leafs.
- An interior node is a non-leaf node. An interior node always has at
least one child. A, B, D, F, and I are all interior nodes.
- The depth of a node is the number of edges which must be taken
in traversing from the root to that node. For instance, the depth of K is
3. The root has depth of 0.
- The height of a node is the maximum length of a reachable
path from this node to any other node. Note, that we are only allowed to
travel down the tree: the parent is not included as being a reachable node
from a given node. We can make a recursive definition of the height of a
node: The height of a leaf is 0. The height of an interior node is 1 +
the maximum height of any child. The height of D is 2; the height of M
is 0. The height of the tree is the height of the root: 4 in the
tree above.
- Size of a node is the number of descendants of a node;
including the node itself. The size of D is 4; K is 1; and A is 14.
- Subtree: we can view each node as the root of a separate tree.
Indeed, trees are often defined recursively. B, C, and D are the roots of
subtrees of A
- A tree with no data is said to be empty
Representing Trees
Representing trees can be tricky because each node may have an arbitrary
number of children. So, what kind of structure is used to store the
children?
Option 1: An array of references
We could use an array of references to store all of the children. A
problem is that we may have to assume a specific size. Or, we could use a
Vector for the children
Option 2: Linked List Representation
Each node has a reference to its first child as well as to its next
sibling.
Binary Trees
A special kind of tree is a binary tree A binary tree can have at
most 2 children: that is, binary trees have nodes with degree at most 2.
An important kind of binary tree is a full binary tree. In a full
binary tree, all of the children (except the leafs) degree 2, and all
leaves are at the same level.
Also, a complete binary tree is like a full tree, except the
bottom level may not be totally filled in. However, all leafs are
filled in from left to right.