Tree terminology is an important concept in data structures that helps beginners understand how trees are organized and how their elements are connected. A tree consists of nodes connected through edges, forming a hierarchical structure. Learning terms such as root, parent, child, leaf node, sibling, height, depth, and subtree makes it easier to understand tree operations, traversal techniques, and advanced topics like Binary Trees and Binary Search Trees. In this article, we will explore the most important tree terminology with simple explanations and JavaScript examples.
Table of Contents
What Is Tree Terminology?
Tree terminology refers to the set of basic terms used to describe the structure and relationships between nodes in a tree data structure. A tree is a non-linear data structure that stores elements in a hierarchical manner. Each element is represented by a node, and nodes are connected using edges.
Understanding tree terminology is useful because it helps programmers describe tree structures, implement tree operations, and solve data structure and algorithm problems.
Node
A node is the basic building block of a tree. It stores data and may contain references to its child nodes.
For example, a node may store a number, a string, or another type of value.
In JavaScript, a basic tree node can be created using a class.
// Create a basic tree node.
class TreeNode {
constructor(value) {
this.value = value;
this.children = [];
}
}
const node = new TreeNode(10);
console.log(node.value);
Output:
10
Here, the node stores the value 10 and an empty array for its child nodes.
Root Node
The root node is the topmost node of a tree. It is the starting point from which all other nodes are connected.
Important points about the root node:
- Every non-empty tree has exactly one root node.
- The root node does not have a parent.
- All other nodes are descendants of the root.
For example, if a tree begins with a node containing the value 10, that node is the root.
Parent Node
A parent node is a node that has one or more child nodes directly connected below it. A node can be a parent of multiple children, depending on the type of tree.
For example, if node 10 has children 20 and 30, then node 10 is the parent of both nodes.
Important points:
- A node can have multiple children.
- Every non-root node has exactly one parent in a rooted tree.
- The root node has no parent.
Child Node
A child node is a node that is directly connected below another node. For example, if node 10 has nodes 20 and 30 connected to it, then 20 and 30 are the children of 10.
Important points:
- A node can have zero or more children.
- A child node has only one direct parent in a rooted tree.
- A node with no children is called a leaf node.
Leaf Node
A leaf node is a node that does not have any children. Leaf nodes are also known as external nodes or terminal nodes. For example, if nodes 20, 30, and 40 do not have any children, they are leaf nodes.
Important points:
- A leaf node has zero children.
- A tree can have one or multiple leaf nodes.
- Leaf nodes represent the endpoints of branches in a tree.
Internal Node
An internal node is a node that has at least one child. Internal nodes are also called non-leaf nodes.
For example, if node 10 has children 20 and 30, it is an internal node.
Important points:
- An internal node has at least one child.
- The root can also be an internal node if it has children.
- A leaf node is not an internal node.
Sibling Nodes
Sibling nodes are nodes that share the same parent. For example, if node 10 has children 20 and 30, then nodes 20 and 30 are siblings.
Important points:
- Sibling nodes have the same direct parent.
- Siblings are located at the same depth.
- Nodes with different parents are not siblings, even if they are at the same level.
Edge
An edge is a connection between two nodes in a tree. Edges establish relationships between parent and child nodes.
Important points:
- Every connection between two nodes represents an edge.
- A tree with n nodes has exactly n - 1 edges.
- A tree with one node has zero edges.
For example, a tree containing five nodes has four edges.
Path
A path is a sequence of nodes connected through edges. It describes how one node can be reached from another node. For example, if node 10 is connected to node 20, and node 20 is connected to node 40, then the path from 10 to 40 is:
10 → 20 → 40
Important points:
- A path consists of connected nodes.
- The path length is the number of edges in the path.
- In a tree, there is exactly one simple path between any two nodes.
Ancestor
An ancestor of a node is any node that appears above it on the path from the root. For example, if node 10 is the root, node 20 is its child, and node 40 is a child of node 20, then nodes 10 and 20 are ancestors of node 40.
Important points:
- The parent is the immediate ancestor.
- The root is an ancestor of every other node.
- A node is not considered its own ancestor.
Descendant
A descendant is any node that appears below another node in a tree. For example, if node 10 has child 20, and node 20 has child 40, then nodes 20 and 40 are descendants of node 10.
Important points:
- Children are immediate descendants.
- Grandchildren are also descendants.
- Every node is a descendant of the root except the root itself.
Degree of a Node
The degree of a node is the number of children that the node has. For example, if a node has three children, its degree is three.
Important points:
- A leaf node has degree zero.
- A node with two children has degree two.
- The degree of a tree is the maximum degree of any node in that tree.
Level of a Node
The level of a node represents its position in the hierarchical structure of a tree.
In the common convention used in introductory data structures:
- The root node is at level 0.
- Its children are at level 1.
- Their children are at level 2.
Some textbooks start counting levels from 1, so always check the convention being used in a question.
Depth of a Node
The depth of a node is the number of edges from the root node to that particular node.
For example:
- The root node has depth 0.
- A direct child of the root has depth 1.
- A node two edges away from the root has depth 2.
Important points:
- The depth of the root is always zero.
- Depth increases as we move away from the root.
- Depth is measured using the number of edges.
Height of a Node
The height of a node is the number of edges on the longest downward path from that node to a leaf node.
Important points:
- A leaf node has height zero.
- The height of a node depends on its longest downward path.
- The height of a tree is the height of its root node.
- The height of an empty tree is sometimes defined as -1, depending on the convention.
Subtree
A subtree is a smaller tree formed by a node and all its descendants. Every node in a tree can be considered the root of its own subtree.
For example, if node 20 has children 40 and 50, then the subtree rooted at node 20 includes node 20 and all its descendants.
Important points:
- A subtree follows the properties of a tree.
- Every node can form a subtree.
- Subtrees are useful in recursive tree algorithms.
Forest
A forest is a collection of separate trees that are not connected to each other.
Important points:
- A forest contains zero or more trees.
- Removing the root of a tree separates its child subtrees into a forest.
- Each individual tree in a forest has its own root.
Forests are commonly used in graph theory and disjoint-set data structures.
JavaScript Example of Tree Terminology
The following example creates a simple tree using JavaScript and identifies some of its basic properties, including the root, children, leaf nodes, and degree.
// Create a tree and identify its basic terminology.
class TreeNode {
constructor(value) {
this.value = value;
this.children = [];
}
}
const root = new TreeNode(10);
const child1 = new TreeNode(20);
const child2 = new TreeNode(30);
const child3 = new TreeNode(40);
root.children.push(child1, child2);
child1.children.push(child3);
console.log("Root Node:", root.value);
console.log(
"Children of Root:",
root.children.map(child => child.value)
);
console.log(
"Degree of Root:",
root.children.length
);
console.log(
"Is Root a Leaf Node:",
root.children.length === 0
);
console.log(
"Is Node 30 a Leaf Node:",
child2.children.length === 0
);
Output:
Root Node: 10
Children of Root: [20, 30]
Degree of Root: 2
Is Root a Leaf Node: false
Is Node 30 a Leaf Node: true
Explanation:
- Node 10 is the root node because it is the starting node.
- Nodes 20 and 30 are children of node 10.
- Node 20 is the parent of node 40.
- Node 30 and node 40 are leaf nodes because they have no children.
- The degree of node 10 is two because it has two children.
Common Mistakes to Avoid
- Confusing depth and height: Depth measures the distance from the root to a node, while height measures the longest downward distance from a node to a leaf.
- Mixing up parent and ancestor: A parent is the immediate predecessor of a node, while an ancestor can be any node above it.
- Considering every node a leaf: Only nodes without children are leaf nodes.
- Confusing siblings with nodes at the same level: Siblings must have the same direct parent, not just the same level.
- Forgetting the edge-count property: A non-empty tree with n nodes always contains exactly n - 1 edges.
Frequently Asked Questions
1. What is a root node in a tree?
A root node is the topmost node of a tree. It does not have a parent.
2. What is the difference between a leaf node and an internal node?
A leaf node has no children, whereas an internal node has at least one child.
3. What is the difference between depth and height?
Depth is the number of edges from the root to a node. Height is the number of edges on the longest downward path from a node to a leaf.
4. How many edges are present in a tree with 10 nodes?
A tree with 10 nodes contains exactly 9 edges.
5. What is a subtree in data structures?
A subtree is a smaller tree consisting of a selected node and all its descendants.
Conclusion
Tree terminology provides the foundation for understanding tree data structures and their hierarchical relationships. Concepts such as root, parent, child, leaf, sibling, depth, height, and subtree are essential for learning advanced topics like Binary Trees, Binary Search Trees, and tree traversal algorithms. Understanding these terms makes it easier to implement tree operations and solve data structure and algorithm problems in programming and technical interviews.
0 Comments