What is tree?
A tree is a hierarchical data structure that consists of nodes connected by edges. It is NOT a linear data structure.
Element in a tree
The node at the top of the tree is called the root. There is only one root per tree and one path from the root node to any node.
Any node except the root node has one edge upward to a node called the parent.
The node below a given node connected by its edge downward is called its child node.
Subtree represents the descendants of a node.
Visiting refers to checking the value of a node when control is on the node.
Traversing means passing through nodes in a specific order.
The Level of a node represents the generation of a node. If the root node is at level 0, then its next child node is at level 1, its grandchild is at level 2, and so on.
The key represents a value of a node based on which a search operation is to be carried out for a node.
Leaf nodes are nodes that are on the bottom of the tree and have no children.
Node depth is the number of links from the root to the node.
Tree height is the number of links from its root to the furthest leaf. The tree height is the same as the maximum node depth.
Examples of Tree data structure
The tree data structure is widely used in many areas of computer science. The following are some examples of tree data structure:
Tree Traversal classification
Tree Traversal algorithms can be classified broadly into two categories:
- Depth-First Search (DFS) Algorithms
- Breadth-First Search (BFS) Algorithms
Depth-First Search (DFS) Algorithms
Depth-First Search (DFS) Algorithms have three variants:
Breadth-First Search (BFS) Algorithms
Breadth-First Search (BFS) Algorithm has one variant: