A binary tree is a hierarchical data structure in which each node can have at most two children, known as the left child and the right child. It is one of the most widely used tree data structures and forms the foundation of advanced structures such as Binary Search Trees, Heaps, and Expression Trees.
Each node in a binary tree can have zero, one, or two children
The topmost node is called the root, while nodes with no children are called leaf nodes
Binary trees are commonly used for hierarchical data representation, searching, and expression evaluation
Implementation of Binary Tree in C++
In C++, a binary tree is typically implemented using a node structure or class. Each node stores data along with pointers to its left and right children. These pointers create links between nodes, allowing the tree to represent hierarchical relationships efficiently.
A node contains a data field to store the value.
A left pointer stores the address of the left child node.
A right pointer stores the address of the right child node.
Nodes are dynamically connected using pointers to form the tree structure.
Representation of Binary Tree
To represent a binary tree in C++, we declare a Node class that contains the data and pointers to its left and right children.
The Node class represents an individual node of the binary tree
Each node stores a data value and links to its child nodes
Templates are used to make the binary tree generic so that it can store different data types
A BinaryTree class can be used to encapsulate the tree data members and its associated operations
template <typename T> class Node { public: T data; Node* left; Node* right;
Each node of a binary tree consists of the following components:
data - stores the value associated with the node
left - points to the left child of the current node
right - points to the right child of the current node
The following program illustrates how we can implement a binary tree in C++.
Output
Inorder traversal: 4 2 5 1 6 3
Preorder traversal: 1 2 4 5 3 6
Postorder traversal: 4 5 2 6 3 1
Level order traversal: 1 2 3 4 5 6
Searching for 7: Not Found
Searching for 6: Found
Inorder traversal after removing 3: 4 2 5 1 6
Basic Operations on Binary Tree in C++
Several operations can be performed on a binary tree to insert, delete, search, and traverse its nodes efficiently.
Insertion in Binary Tree
Insertion in a binary tree involves adding a new node at the first available position while maintaining the structure of the tree.
Approach
Check whether the tree is empty
If the tree is empty, create a new node and make it the root node
Otherwise, perform a level order traversal using a queue
Dequeue a node and check its left child
If the left child is NULL, insert the new node as the left child
Otherwise, check the right child
If the right child is NULL, insert the new node as the right child
If both children exist, enqueue them and continue the traversal
Deletion in Binary Tree
Deletion in a binary tree involves removing a node and rearranging the tree to maintain its structure.
Approach
Find the node to be deleted using level order traversal
Simultaneously identify the deepest node in the tree
Replace the value of the node to be deleted with the value of the deepest node
Locate the deepest node again using level order traversal
Remove the deepest node from the tree
If the node is a leaf node, it can be removed directly
Searching in Binary Tree
To search a given node in a binary tree, we need to traverse and compare each node with target node (node to be searched). Following is the algorithm to search a node in the binary tree:
Approach
Check whether the tree is empty
If the tree is empty, return false
Perform a level order traversal using a queue
Compare each node with the target value
If a match is found, return true
Enqueue the left and right children of the current node if they exist
If all nodes are visited and no match is found, return false
Traversals in Binary Tree
Traversal refers to the process of visiting every node of a binary tree in a specific order. Different traversal techniques are used depending on the order in which nodes need to be processed.
Common Traversal Techniques
Inorder Traversal (Left -> Root -> Right): Visits the left subtree first, then the root node, and finally the right subtree
Preorder Traversal(Root -> Left -> Right): Visits the root node first, followed by the left and right subtrees
Postorder Traversal(Left -> Right -> Root): Visits both subtrees before processing the root node
Level Order Traversal : Visits nodes level by level from top to bottom, moving from left to right within each level
Complexity Analysis of Basic Operations
The following table summarizes the time and space complexities of common binary tree operations.
Operation Name
Description
Time Complexity
Space Complexity
Insertion
Inserts a new node into the binary tree.
O(N)
O(N)
Deletion
Deletes a specific node from the binary tree.
O(N)
O(N)
Searching
Searches for a specific value in the binary tree.
O(N)
O(N)
Traversal
In-order, Pre-order, Post-order traversals
O(N)
O(N)
Types of Binary Tree in C++
Binary trees can be classified into different types based on their structure, level completion, and node-value properties.
According to Number of Children
These types are classified based on the number of children associated with each node.
Full Binary Tree: A is a tree in which every node has either 0 or 2 children. In other words, all nodes except leaf nodes have two children.
Degenerate Binary Tree: A is similar to a skewed binary tree. It's a tree where each parent node has only one child node.
Skewed Binary Tree: A is a tree in which all nodes have only one child, either left or right. There are two types: left-skewed and right-skewed.
According to Completion of Levels
These types are classified based on how completely the levels of the tree are filled.
Complete Binary Tree: A is a binary tree in which all levels are completely filled except possibly the last level, which is filled from left to right.
Perfect Binary Tree: A is a binary tree in which all interior nodes have two children and all leaves are at the same level.
Balanced Binary Tree: A is a binary tree in which the height of the left and right subtrees of every node differs by at most one.
According to Node Values
These types are classified based on the ordering and organization of node values.
Binary Search Tree: A is a binary tree in which all nodes in the left subtree are smaller than the root node and all nodes in the right subtree are greater.
AVL Tree: An is a self-balancing BST where the height of the left and right subtrees of any node differ by at most one.
Red Black Tree: A is a self-balancing BST where each node has an extra bit for denoting the color of the node, either red or black.
B Tree: is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time.
B+ Tree: A is a variant of the B-tree that has all data stored in the leaf nodes, while internal nodes only store keys.
Segment Tree: A is a tree data structure used for storing information about intervals, or segments. It allows querying which of the stored segments contain a given point.
Fenwick Tree: A is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers.
Applications of Binary Tree
Binary trees are widely used in computer science for organizing hierarchical data and performing efficient search and processing operations.
File System Organization: Used to represent hierarchical file and directory structures.
Expression Trees: Used to represent and evaluate arithmetic and logical expressions.
Huffman Coding Trees: Used in data compression algorithms to generate efficient binary codes.
Binary Search Trees (BSTs): Used for efficient searching, insertion, and deletion of data.
Decision Trees: Used in machine learning for classification and prediction tasks.
Game Trees: Used in artificial intelligence to analyze possible game moves and outcomes.
Syntax Trees: Used by compilers to represent the structure of source code.
Database Indexing: Used in database systems to support efficient data retrieval and searching.
Related Articles
You can go through the following articles to improve your understanding about the binary tree data structure: