A rooted tree is a special type of tree in graph theory where one vertex is designated as the "root," and all edges are directed away from or towards the root. Rooted trees are often used to model hierarchical structures, such as organizational charts, file systems, or family trees.
Before understanding a rooted tree, it's important to define what a tree is:
A rooted tree is a tree where one vertex is chosen as the root, and every edge is directed either towards or away from the root, depending on the convention.
Rooted trees have several key properties that distinguish them from general trees:
Unique Root: There is one vertex, called the root, that serves as the starting point for all paths in the tree.
Parent-Child Relationship: In a rooted tree, every vertex, except the root, has exactly one parent, which is the vertex from which it is connected. Any vertex can have one or more children (subsequent vertices connected to it).
Levels and Depth:
Subtree: A subtree of a rooted tree is a tree consisting of a node and all its descendants in the tree. For any node in a rooted tree, the subtree rooted at that node includes the node itself and all of its children, grandchildren, and so on.
Leaf: A leaf is a node in the tree that has no children (i.e., a vertex with degree 1 except the root).
Rooted trees can be represented in various ways:
Adjacency List: An adjacency list is a collection of lists or arrays where each list corresponds to a vertex and contains the list of its children.
For example, consider the following rooted tree:
A
/ \
B C
/ \
D E
The adjacency list representation is:
Parent Array: Each node has an array where the index represents the node, and the value represents its parent. The root is typically denoted by a special value (such as -1) since it has no parent.
Binary Tree Representation: In a binary tree (a specific type of rooted tree where each node has at most two children), each node can have two pointers or references to its left and right children.
There are several specific types of rooted trees that have additional constraints:
Binary Tree:
Binary Search Tree (BST):
AVL Tree (Height-Balanced Tree):
Trie (Prefix Tree):
Traversal refers to the process of visiting each node in the tree. There are several common methods for traversing a rooted tree:
Preorder Traversal (Root, Left, Right):
Example for the tree:
A
/ \
B C
/ \
D E
Preorder traversal: A, B, D, E, C
Inorder Traversal (Left, Root, Right):
Inorder traversal: D, B, E, A, C (typically used in binary search trees to visit nodes in sorted order)
Postorder Traversal (Left, Right, Root):
Postorder traversal: D, E, B, C, A
Level Order Traversal (Breadth-First):
Level-order traversal: A, B, C, D, E
Rooted trees are useful in many applications:
Hierarchical Data: Rooted trees are ideal for representing hierarchical data structures such as organizational charts, classification hierarchies, or folder structures in file systems.
Decision Trees: In machine learning, rooted trees are used in decision tree algorithms, where each node represents a decision or a test, and each edge represents the outcome of that test.
Binary Search Trees: Binary search trees (BSTs) are used to store data in such a way that allows for efficient searching, insertion, and deletion operations.
Expression Parsing: Rooted trees can represent arithmetic expressions, where each node represents an operator or operand, and the tree structure reflects the order of operations.
Data Compression (Huffman Encoding): Rooted trees are used in Huffman encoding for optimal data compression. The tree structure is used to assign shorter codes to more frequent elements.
Consider a simple rooted tree:
A
/ \
B C
/ \
D E
Rooted trees provide a structured and efficient way to model and process hierarchical relationships in various applications.
Open this section to load past papers