Unit III - Topic 3 - Binary Tree Adt
Unit III - Topic 3 - Binary Tree Adt
Binary Tree is defined as a tree data structure where each node has at most 2 children. Since each element in a binary tree can
have only 2 children, we typically name them the left and right child.
1. Data
2. Pointer to left child
3. Pointer to right child
Inserting an element.
1
Unit III – Topic 3 - BINARY TREE ADT
Removing an element.
Searching for an element.
Traversing the tree.
Tree Traversal using Depth-First Search (DFS) algorithm can be further classified into three categories:
Preorder Traversal (current-left-right): Visit the current node before visiting any nodes inside the left or right subtrees.
Here, the traversal is root – left child – right child. It means that the root node is traversed first then its left child and
finally the right child.
Inorder Traversal (left-current-right): Visit the current node after visiting all nodes inside the left subtree but before
visiting any node within the right subtree. Here, the traversal is left child – root – right child. It means that the left
child is traversed first then its root node and finally the right child.
Postorder Traversal (left-right-current): Visit the current node after visiting all the nodes of the left and right
subtrees. Here, the traversal is left child – right child – root. It means that the left child has traversed first then the
right child and finally its root node.
2
Unit III – Topic 3 - BINARY TREE ADT
Tree Traversal using Breadth-First Search (BFS) algorithm can be further classified into one category:
Level Order Traversal: Visit nodes level-by-level and left-to-right fashion at the same level. Here, the traversal is
level-wise. It means that the most left child has traversed first and then the other children of the same level from left to
right have traversed.
Let us traverse the following tree with all four traversal methods:
Binary Tree
3
Unit III – Topic 3 - BINARY TREE ADT
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
4
Unit III – Topic 3 - BINARY TREE ADT
};
// newNode() allocates a new node with the given data and NULL left and right pointers.
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
5
Unit III – Topic 3 - BINARY TREE ADT
{
if (root != NULL) {
display(root->left);
printf("%d ", root->data);
display(root->right);
}
}
int main()
// Create root
/\
NULL NULL
*/
6
Unit III – Topic 3 - BINARY TREE ADT
root->left = newNode(2);
root->right = newNode(3);
/\
23
/\/\
*/
root->left->left = newNode(4);
/\
23
/\/\
7
Unit III – Topic 3 - BINARY TREE ADT
/\
NULL NULL
*/
display(root);
getchar();
return 0;
Output
Binary Tree
4213
8
Unit III – Topic 3 - BINARY TREE ADT
N <= 2h – 1
2h >= N+1
log2(2h) >= log2(N+1) (Taking log both sides)
hlog22 >= log2(N+1) (h is an integer)
h >= | log2(N+1) |
So the minimum height possible is | log 2(N+1) |
4. A Binary Tree with L leaves has at least | Log 2L |+ 1 levels:
A Binary tree has the maximum number of leaves (and a minimum number of levels) when all levels are fully filled. Let
all leaves be at level l, then below is valid for the number of leaves L
L <= 2l-1 [From Point 1] [Note: Here, consider level of root node as 1]
l = | Log2L | + 1
where l is the minimum number of levels
9
Unit III – Topic 3 - BINARY TREE ADT
5. In a Binary tree where every node has 0 or 2 children, the number of leaf nodes is always one more than nodes
with two children:
L=T+1
Where L = Number of leaf nodes
T = Number of internal nodes with two children
Proof:
No. of leaf nodes (L) i.e. total elements present at the bottom of tree = 2 h-1 (h is height of tree)
No. of internal nodes = {total no. of nodes} – {leaf nodes} = { 2 h – 1 } – {2h-1} = 2h-1 (2-1) – 1 = 2h-1 – 1
So , L = 2h-1
T = 2h-1 – 1
Therefore L = T + 1
Hence proved
6. In a non-empty binary tree, if n is the total number of nodes and e is the total number of edges, then e = n-1:
Every node in a binary tree has exactly one parent with the exception of the root node. So if n is the total number of nodes
then n-1 nodes have exactly one parent. There is only one edge between any child and its parent. So the total number of
edges is n-1.
Each node in a binary tree can have at most two child nodes: In a binary tree, each node can have either zero, one, or
two child nodes. If a node has zero children, it is called a leaf node. If a node has one child, it is called a unary node. If
a node has two children, it is called a binary node.
The node at the top of the tree is called the root node: The root node is the first node in a binary tree and all other
nodes are connected to it. All other nodes in the tree are either child nodes or descendant nodes of the root node.
10
Unit III – Topic 3 - BINARY TREE ADT
Nodes that do not have any child nodes are called leaf nodes: Leaf nodes are the endpoints of the tree and have no
children. They represent the final result of the tree.
The height of a binary tree is defined as the number of edges from the root node to the deepest leaf node: The height of
a binary tree is the length of the longest path from the root node to any of the leaf nodes. The height of a binary tree is
also known as its depth.
In a full binary tree, every node except the leaves has exactly two children: In a full binary tree, all non-leaf nodes
have exactly two children. This means that there are no unary nodes in a full binary tree.
In a complete binary tree, every level of the tree is completely filled except for the last level, which can be partially
filled: In a complete binary tree, all levels of the tree except the last level are completely filled. This means that there
are no gaps in the tree and all nodes are connected to their parent nodes.
In a balanced binary tree, the height of the left and right subtrees of every node differ by at most 1: In a balanced
binary tree, the height of the left and right subtrees of every node is similar. This ensures that the tree is balanced and
that the height of the tree is minimized.
The in-order, pre-order, and post-order traversal of a binary tree are three common ways to traverse the tree : In-order,
pre-order, and post-order are three different ways to traverse a binary tree. In-order traversal visits the left subtree, the
node itself, and then the right subtree. Pre-order traversal visits the node itself, the left subtree, and then the right
subtree. Post-order traversal visits the left subtree, the right subtree, and then the node itself.
11
Unit III – Topic 3 - BINARY TREE ADT
A full Binary tree is a special type of binary tree in which every parent node/internal node has either two or no children. It
is also known as a proper binary tree.
12
Unit III – Topic 3 - BINARY TREE ADT
13
Unit III – Topic 3 - BINARY TREE ADT
14
Unit III – Topic 3 - BINARY TREE ADT
In a Perfect Binary Tree, the number of leaf nodes is the number of internal nodes plus 1
L = I + 1 Where L = Number of leaf nodes, I = Number of internal nodes.
A Perfect Binary Tree of height h (where the height of the binary tree is the number of edges in the longest path from the
root node to any leaf node in the tree, height of root node is 0) has 2h+1 – 1 node.
An example of a Perfect binary tree is ancestors in the family. Keep a person at root, parents as children, parents of
parents as their children.
Refer to this article to read about more on Perfect Tree
3. Balanced Binary Tree
A binary tree is balanced if the height of the tree is O(Log n) where n is the number of nodes. For Example, the AVL tree
maintains O(Log n) height by making sure that the difference between the heights of the left and right subtrees is at most
1. Red-Black trees maintain O(Log n) height by making sure that the number of Black nodes on every root to leaf paths is
15
Unit III – Topic 3 - BINARY TREE ADT
the same and that there are no adjacent red nodes. Balanced Binary Search trees are performance-wise good as they
provide O(log n) time for search, insert and delete.
It is a type of binary tree in which the difference between the height of the left and the right subtree for each node is either
0 or 1. In the figure above, the root node having a value 0 is unbalanced with a depth of 2 units.
Some Special Types of Trees:
On the basis of node values, the Binary Tree can be classified into the following special types:
1. Binary Search Tree
2. AVL Tree
3. Red Black Tree
4. B Tree
5. B+ Tree
16
Unit III – Topic 3 - BINARY TREE ADT
6. Segment Tree
Below Image Shows Important Special cases of binary Trees:
17
Unit III – Topic 3 - BINARY TREE ADT
2. AVL Tree
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees
cannot be more than one for all nodes.
4. B – Tree
A B-tree is a type of self-balancing tree data structure that allows efficient access, insertion, and deletion of data items. B-
trees are commonly used in databases and file systems, where they can efficiently store and retrieve large amounts of
data. A B-tree is characterized by a fixed maximum degree (or order), which determines the maximum number of child
nodes that a parent node can have. Each node in a B-tree can have multiple child nodes and multiple keys, and the keys
are used to index and locate data items.
5. B+ Tree
A B+ tree is a variation of the B-tree that is optimized for use in file systems and databases. Like a B-tree, a B+ tree also
has a fixed maximum degree and allows efficient access, insertion, and deletion of data items. However, in a B+ tree, all
data items are stored in the leaf nodes, while the internal nodes only contain keys for indexing and locating the data items.
This design allows for faster searches and sequential access of the data items, as all the leaf nodes are linked together in a
linked list.
6. Segment Tree
In computer science, a Segment Tree, also known as a statistic tree, 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. It is, in principle, a
static structure; that is, it’s a structure that cannot be modified once it’s built. A similar data structure is the interval tree.
A segment tree for a set I of n intervals uses O(n log n) storage and can be built in O(n log n) time. Segment trees support
searching for all the intervals that contain a query point in time O(log n + k), k being the number of retrieved intervals or
segments.
Segment Tree
19
Unit III – Topic 3 - BINARY TREE ADT
20
Unit III – Topic 3 - BINARY TREE ADT
Complex balancing algorithms: To ensure that binary trees remain balanced, various balancing algorithms can be used,
such as AVL trees and red-black trees. These algorithms can be complex to implement and require additional
overhead, making them less suitable for some applications.
22