Given two connected nodes like this: Node A is called the parent, and node B is called the childA subtreeof a given node includes one of its children and all of that child's descendantsThe descendants of a node nare all nodes reachable from n(n's children, its children's children, etc.). In the original example, node A has three subtrees:
.
.
1.B, D 2.I 3.C, E, F, G, J.

An important special kind of tree is the
binary
tree. In a binary tree:
Each node has 0, 1, or 2 children.
Each child is either a left child or a right child.
Here are two examples of binary trees that are different:
The two trees are different because the children of node B are different: in the first tree, B's
left child is D and its right child is E; in the second tree, B's left child is E and its right child
is D. Also note that lines are used instead of arrows. We sometimes do this because it is clear
that the edge goes from the higher node to the lower node.
Representing Trees
Since a binary-tree node never has more than two children, a node can be represented using a
class with 3 fields: one for the data in the node, plus two child pointers:
class BinaryTreenode {
// *** fields ***
private Object data;
private BinaryTreenode leftChild;
private BinaryTreenode rightChild;
}
However, since a general-tree node can have an arbitrary number of children, a fixed number
of child-pointer fields won't work. Instead, we can use a List to keep all of the child pointers:
class Treenode {
// *** fields ***
private Object data;
private List children;
}
(Note that the items in the List will be of type Treenode.)
As we know, a list can be represented using either an array or a linked-list. For example,
consider this general tree (a simplified version of the original example):

#### You've reached the end of your free preview.

Want to read all 7 pages?

- Spring '08
- MarvinSolomon
- Data Structures, Articles with example pseudocode, Tree traversal, Nested set model