VOOZH about

URL: https://www.geeksforgeeks.org/dsa/introduction-to-tree-data-structure/

⇱ Introduction to Tree Data Structure - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Tree Data Structure

Last Updated : 7 Oct, 2025

A tree is a hierarchical data structure used to organize and represent data in a parent–child relationship.
It consists of nodes, where the topmost node is called the root, and every other node can have one or more child nodes.

Basic Terminologies In Tree Data Structure:

  • Parent Node: A node that is an immediate predecessor of another node. Example: 35 is the parent of 3 and 6.
  • Child Node: A node that is an immediate successor of another node. Example: 3 and 6 are children of 35.
  • Root Node: The topmost node in a tree, which does not have a parent. Example: 15 is the root node.
  • Leaf Node (External Node): Nodes that do not have any children. Example: 1, 10, 12, 5, 7, 7 are leaf nodes.
  • Ancestor: Any node on the path from the root to a given node (excluding the node itself). Example: 15 and 35 are ancestors of 10.
  • Descendant: A node x is a descendant of another node y if y is an ancestor of x. Example: 1, 10, and 6 are descendants of 35.
  • Sibling: Nodes that share the same parent. Example: 1 and 10 are siblings, and 5 and 7 are siblings.
  • Level of a Node: The number of edges in the path from the root to that node.The root node is at level 0.
  • Internal Node: A node with at least one child.
  • Neighbor of a Node: The parent or children of a node.
  • Subtree:  A node and all its descendants form a subtree.

Why Tree is considered a non-linear data structure?

Data in a tree is not stored sequentially (i.e., not in a linear order). Instead, it is organized across multiple levels, forming a hierarchical structure. Because of this arrangement, a tree is classified as a non-linear data structure.

Representation of a Node in Tree Data Structure:

A tree can be represented using a collection of nodes. Each of the nodes can be represented with the help of class or structs.

Importance of Tree Data Structure:

  • Trees are useful for storing data that naturally forms a hierarchy.
  • File systems on computers are structured as trees, with folders containing subfolders and files.
  • The DOM (Document Object Model) of an HTML page is a tree:The <html> tag is the root.<head> and <body> are its children.These tags can have their own child nodes, forming a hierarchical structure.
  • Trees help in efficient data organization and retrieval for hierarchical relationships.

Types of Tree Data Structures :

Tree data structures can be classified based on the number of children each node can have.

Binary Tree:

Each node can have a maximum of two children.

  • Full Binary Tree – every node has either 0 or 2 children.
  • Complete Binary Tree – all levels are fully filled except possibly the last, which is filled from left to right.
  • Balanced Binary Tree – height difference between left and right subtrees of every node is minimal.

Ternary Tree:

Each node can have at most three children, often labeled as left, middle, and right.

N-ary Tree (or Generic Tree):

  • Each node can have any number of children.
  • Each node typically contains: Some data , A list of references to its children (duplicates are not allowed).
  • Unlike linked lists, nodes store references to multiple child nodes.

Please refer Types of Trees in Data Structures for details.

Basic Operations Of Tree Data Structure:


Output
Parents of each node:
1 -> NULL
2 -> 1
4 -> 2
5 -> 2
3 -> 1
Children of each node:
1 -> 2 3 
2 -> 4 5 
4 -> 
5 -> 
3 -> 
Leaf nodes: 4 5 3 
Degrees of nodes:
1 -> 2
2 -> 3
4 -> 1
5 -> 1
3 -> 1

Properties of Tree Data Structure:

  • Number of edges: An edge is the connection between two nodes. A tree with N nodes will always have N - 1 edges. There is exactly one path from any node to any other node in the tree.
  • Depth of a node: The depth of a node is the length of the path from the root to that node. Each edge in the path adds 1 unit to the length. Equivalently, it is the number of edges from the root to the node.
  • Height of the tree: The height of the tree is the length of the longest path from the root to any leaf node.
  • Degree of a node: The degree of a node is the number of subtrees attached to it (i.e., the number of children it has).
    A leaf node has a degree of 0.
    The degree of the tree is the maximum degree among all nodes in the tree.
Comment
Article Tags: