Understanding the terminology associated with trees is essential to effectively work with and understand tree data structures. Here’s a breakdown of the most important terms:
Basic Components
- Node:
- A basic unit of a tree containing data and links to its child nodes.
- Every node consists of:
- Key: The value or data stored in the node.
- Pointer(s): References to its child nodes.
- Edge:
- A connection between two nodes.
- Represents the relationship between a parent and a child.
- Root:
- The top-most node of the tree.
- It has no parent.
- Leaf:
- A node with no children.
- Also called a terminal node.
- Parent:
- A node that has one or more children.
- Child:
- A node directly connected and below a parent.
- Subtree:
- A tree formed by any node and its descendants.
Structural Relationships
- Siblings:
- Nodes that share the same parent.
- Ancestor:
- A node is an ancestor of another node if there is a path from the ancestor to the node.
- Example: In a binary tree, the root is an ancestor of all other nodes.
- Descendant:
- A node is a descendant of another node if it lies below that node in the tree.
Tree Metrics
- Degree:
- The number of children a node has.
- The degree of a tree is the maximum degree of any node in the tree.
- Level:
- The depth of a node in the tree.
- The root is at level 0, its children are at level 1, and so on.
- Height:
- The length of the longest path from a node to a leaf.
- The height of the tree is the height of the root.
- Depth:
- The length of the path from the root to a particular node.
- Depth of the root is 0.
- Path:
- A sequence of nodes and edges connecting a node to another node.
- Degree of a Tree:
- The maximum degree of any node in the entire tree.
Tree Traversal
Traversal is not part of the core terminology but is essential for understanding how nodes are visited:
- In-order Traversal: Visit left subtree, root, then right subtree.
- Pre-order Traversal: Visit root, then left and right subtrees.
- Post-order Traversal: Visit left subtree, right subtree, then root.
- Level-order Traversal: Visit nodes level by level.
Special Nodes in a Tree
- Internal Node:
- A node that has at least one child.
- It is not a leaf.
- External Node:
- Synonymous with a leaf node, having no children.
- Root Node:
- The starting point of the tree, with no parent.
- Terminal Node:
- Another term for a leaf node.
Tree Types Based on Terminology
- Full Tree: Every internal node has exactly the maximum number of children.
- Complete Tree: All levels, except possibly the last, are fully filled.
- Balanced Tree: The height difference between the left and right subtrees of any node is minimal.
Example
For the following tree:
mathematica
Copy code
A
/ \
B C
/ \ / \
D E F G
- Root: A
- Internal Nodes: A, B, C
- Leaf Nodes: D, E, F, G
- Parent of B: A
- Children of B: D, E
- Siblings of E: D
- Height of the Tree: 2
- Level of E: 2
- Path from Root to G: A → C → G
This structured understanding allows for easier implementation and manipulation of tree data structures.