A tree is a hierarchical, non-linear data structure made up of nodes connected by edges. It starts with a single root node, and every child node has exactly one parent, creating a parent-child relationship. Trees are widely used to represent hierarchical data such as file systems, organizational structures, XML/HTML documents and decision trees.

Basic Tree Terminologies
Before learning different types of trees, let's understand some important terms used in a tree data structure.
- Root Node: The topmost node of a tree that has no parent. Every non-empty tree has exactly one root node. (Example: A)
- Parent Node: A node that has one or more child nodes. (Example: B is the parent of D and E.)
- Child Node: A node directly connected below a parent node. (Example: D and E are the children of B.)
- Leaf Node (External Node): A node that has no children. (Example: F, G, H, I, J, and K are leaf nodes.)
- Internal Node: A node that has at least one child. (Example: A, B, C, and E are internal nodes.)
- Ancestor: Any node on the path from the root to a given node. (Example: A and B are ancestors of E.)
- Descendant: Any node that lies below another node in the tree. (Example: D and E are descendants of B.)
- Sibling: Nodes that share the same parent. (Example: D and E are siblings because both have parent B.)
- Level of a Node: The number of edges from the root node to that node. The root node is always at level 0.
- Neighbor: The parent or child of a node is called its neighbor.
- Subtree: A tree formed by any node along with all of its descendants.
Representation of a Node
A tree is built using nodes, where each node stores some data and references to its child nodes. In Python, a node can be represented by creating a class with a data field and a list to store its children.
Example: Here, we create a simple Node class to represent a node in a tree.
class Node:
def __init__(self, data):
self.data = data
self.children = []
Explanation:
- data stores the value of the node.
- children is a list that stores references to all child nodes.
- Each object created from the Node class represents one node in the tree.
Types of Tree Data Structures
Trees come in different forms, each designed for specific use cases. Below are some of the most commonly used tree data structures.
1. Binary Tree: a tree in which each node can have at most two children, called the left child and right child. It is commonly used to represent hierarchical data and forms the basis for many other tree structures.
To know more, refer to: Binary Tree in Python
2. Binary Search Tree (BST): a special type of binary tree where:
- The left subtree contains values smaller than the parent node.
- The right subtree contains values greater than the parent node.
This ordering makes searching, insertion and deletion more efficient.
To know more, refer to: Binary Search Tree in Python
3. AVL Tree: a self-balancing Binary Search Tree. After every insertion or deletion, it automatically balances itself so that the height difference between the left and right subtrees of any node is at most 1.
To know more, refer to: AVL Tree in Python
4. Red Black Tree: another self-balancing Binary Search Tree. It uses color-based balancing rules to maintain an approximately balanced structure, ensuring efficient search, insertion and deletion operations.
To know more, refer to: Red Black Tree in Python
5. B-Tree: a balanced tree in which each node can contain multiple keys and multiple child nodes. It is widely used in databases and file systems because it minimizes disk access and efficiently handles large amounts of data.
To know more, refer to: B Tree in Python
6. B+ Tree: a variation of the B-Tree in which all data is stored in the leaf nodes, while internal nodes are used only for searching. The leaf nodes are linked together, making B+ Trees ideal for fast sequential access and database indexing.
To know more, refer to: B+ Tree in Python
Properties of a Tree Data Structure
Some important properties of a tree data structure are:
- Number of Edges: A tree with N nodes always has N − 1 edges. There is exactly one unique path between any two nodes in a tree.
- Depth of a Node: The depth of a node is the number of edges from the root node to that node. The root node always has a depth of 0.
- Height of a Node: The height of a node is the number of edges on the longest path from that node to any leaf node.
- Height of a Tree: The height of a tree is the height of its root node, which is equal to the number of edges on the longest path from the root to a leaf node.
- Degree of a Node: The degree of a node is the number of children it has. A leaf node always has a degree of 0.
- Degree of a Tree: The degree of a tree is the highest degree among all the nodes in the tree.
Implementation of Tree
A way to represent a tree in Python is by creating a Node class. Each node stores its own data and a list of child nodes. We can then connect multiple nodes to form a tree.
class Node:
def __init__(self, data):
self.data = data
self.children = []
root = Node("A")
node_b = Node("B")
node_c = Node("C")
node_d = Node("D")
root.children.append(node_b)
root.children.append(node_c)
node_b.children.append(node_d)
print("Root:", root.data)
for child in root.children:
print("Child:", child.data)
print("Child of B:", node_b.children[0].data)
Output
Root: A Child: B Child: C Child of B: D
Explanation:
- A Node class is created with data and children attributes.
- Four nodes (A, B, C and D) are created.
- B and C are added as children of A.
- D is added as a child of B, forming a simple tree.
- Finally, the tree structure is displayed by accessing the child nodes.
Tree Traversal Techniques
Tree traversal is the process of visiting every node in a tree exactly once in a specific order. Different traversal techniques are used depending on how the tree needs to be processed. Three most common depth-first traversal techniques are:
- Inorder Traversal: Visits the left subtree, then the root node, and finally the right subtree.
- Preorder Traversal: Visits the root node first, followed by the left subtree and then the right subtree.
- Postorder Traversal: Visits the left subtree, then the right subtree, and finally the root node.
To know more, refer to: Tree Traversal Techniques in Python