Tree Data Structure
A tree is a data structure.
A tree starts with a single node known as the root node.
Here, 1 is the root node. The root node can have 0 or more children. The root node has three children nodes 2, 3, and 4.
As you can see the children nodes also has their own children.
A child node can have only one parent node. As you can see every child node has only one parent. For example, node 6 has only one parent which is 3.
The tree is unidirectional. This means it can only point from a parent node to a child node.
Leaf node
The node with no children is known as the leaf node. As node 5, 6, and 8 has no children, they are leaf node.
Level of tree
Level of tree data structure starts from 0.
Height of Tree
Height of tree starts from 1.
Subtree
Trees inside a tree are a subtree.
Example Tree Data Structure
A tree is a common data structure. We work with them on daily basis. For let’s see an example of web page code.
<!DOCTYPE html>
<html lang="en">
<head> </head><body>
<div>
<img src="" alt="">
<p></p>
</div>
</body>
</html>
Now, let’s see this in tree format.