Tree in Data Structure
What is a Tree?
A tree is a non-linear data structure that stores data in a hierarchical form. It consists of nodes connected by edges. The topmost node of the tree is called the root node. Every node can have one or more child nodes. Trees help organize data in a structured and efficient manner. A family tree is a common real-life example of a tree structure.
Terminologies of a tree :-
1. Tree
A tree is a non-linear data structure that stores data in a hierarchical form. It consists of nodes connected by edges. The topmost node of the tree is called the root node. Every node can have one or more child nodes. Trees help organize data in a structured and efficient manner. A family tree is a common real-life example of a tree structure.
2. Subtree
A subtree is a smaller part of a tree that starts from a child node and includes all its descendants. Every child node in a tree can form its own subtree. Subtrees follow the same rules as the main tree. They help divide large trees into smaller sections. Subtrees are useful in searching and processing tree data. A family branch in a family tree is an example of a subtree.
3. Root Node
The root node is the first and topmost node in a tree. It is the starting point of the tree structure. Every tree must contain only one root node. All other nodes are connected directly or indirectly to the root. The root node has no parent node. In an organization chart, the CEO can be considered the root node.
4. Edge
An edge is the connection between two nodes in a tree. It represents the relationship between parent and child nodes. Edges help form the structure of the tree. A tree with N nodes contains N−1 edges. Without edges, nodes cannot communicate with each other. The line joining a parent and child in a family tree is an example of an edge.
5. Parent Node
A parent node is a node that has one or more child nodes. It is located above its child nodes in the hierarchy. A parent node helps create the structure of the tree. Every node except the root has a parent. A parent can have multiple children. In a family tree, a father or mother acts as a parent node.
6. Child Node
A child node is a node directly connected below a parent node. Every child node has a parent except the root node. Child nodes help form branches in a tree. A child node may also become a parent if it has children. Trees can have many child nodes at different levels. In a family tree, sons and daughters are child nodes.
7. Sibling Nodes
Sibling nodes are nodes that share the same parent node. They are located at the same level in the tree. Sibling nodes are related through a common parent. They do not have a parent-child relationship with each other. A parent may have two or more sibling nodes. Brothers and sisters in a family are examples of sibling nodes.
8. Leaf Node
A leaf node is a node that does not have any child nodes. It is usually found at the lowest level of a tree. Leaf nodes are also called terminal nodes. They represent the end points of tree branches. A tree can have multiple leaf nodes. The youngest members of a family tree are examples of leaf nodes.
9. Internal Node
An internal node is a node that has at least one child node. It is located between the root and leaf nodes. Internal nodes help connect different parts of the tree. The root node can also be an internal node if it has children. These nodes are important for traversal operations. Parents in a family tree are examples of internal nodes.
10. Degree of Node
The degree of a node is the number of child nodes connected to it. A node with no children has a degree of zero. Degree helps identify the branching factor of a node. The highest degree among all nodes is called the degree of the tree. Different nodes may have different degrees. A node having three children has a degree of three.
11. Level
The level of a node indicates its position in a tree. The root node is always at level zero. The children of the root are at level one. Their children are placed at level two and so on. Levels help organize nodes hierarchically. In a school hierarchy, principal, teachers, and students can represent different levels.
12. Height
The height of a tree is the number of edges in the longest path from the root node to a leaf node. It indicates how tall the tree is. Height is useful in measuring tree efficiency. A tree with only one node has a height of zero. Trees with smaller heights generally provide faster operations. The longest branch in a family tree determines its height.
13. Depth
Depth is the number of edges from the root node to a particular node. The root node has a depth of zero. Depth increases as we move down the tree. It helps determine the location of a node. Depth is important in searching and traversal. The distance between the CEO and an employee is similar to depth in a tree.
14. Path
A path is a sequence of nodes connected by edges. It shows the route from one node to another. A path may contain one or more edges. The length of a path depends on the number of edges present. Paths help in searching and traversal operations. The route from grandparent to grandchild in a family tree is a path.
What is Ancestor and Descendants?
An ancestor is a node that appears above another node in a tree. A descendant is a node that appears below another node. The root node is an ancestor of all nodes in the tree. A node can have many descendants. These relationships help represent hierarchy. In a family tree, grandparents are ancestors and grandchildren are descendants.
Types of tree :-












0 Comments