

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
² Definition: A binary tree is a non-linear tree data structure whose all nodes have either zero, one, or at most two children nodes. These two children are generally referred to as left and right child respectively. ² Properties: ○ At each level of i, the maximum number of nodes is 2i. ○ If a binary tree has L number of leaf nodes, its height is given by L + 1. ○ The height of the tree is defined as the longest path from the root node to the leaf node. Say a tree has height equal to 3.
Typology: Lecture notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Definition: A binary tree is a non-linear tree data structure whose all
nodes have either zero, one, or at most two children nodes. These two
children are generally referred to as left and right child respectively.
Properties:
At each level of i, the maximum number of nodes is 2
i
. ○
If a binary tree has L number of leaf nodes, its height is given by L +
The height of the tree is defined as the longest path from the root
node to the leaf node. Say a tree has height equal to 3. Therefore, the
maximum number of nodes at height 3 is equal to (1+2+4+8) = 15.
In general, the maximum number of nodes possible at height h is (
+ 21 + 22+….2h) = 2
h+
○ The minimum number of nodes possible at height h is equal to h+1.
If the number of nodes is minimum, then the height of the tree would
be maximum. Conversely, if the number of nodes is maximum, then
the height of the tree would be minimum.
If there are 'n' number of nodes in the binary tree.
The minimum height can be computed as:
As we know that,
n = 2
h+
n+1 = 2
h+
Taking log on both the sides,
log 2
(n+1) = log 2
h+
log 2
(n+1) = h+
h = log 2
(n+1) - 1
The maximum height can be computed as:
As we know that,
n = h+
h= n - 1
Types of binary tree:
Full/proper/strict binary tree: The full binary tree is also known
as a strict binary tree. The tree can only be considered as the full
binary tree if each node must contain either 0 or 2 child node of its
parent node or the parent node is itself the leaf node or the external
node.
In other words a full binary tree is either a single vertex and the
root node has two subtrees, both of which are full binary trees.
Here, the quantity of leaf nodes is equal to the number of internal
nodes plus one.
In the above tree, we can observe that each node is either containing zero
or two children; therefore, it is a Full Binary tree.
Properties of Full Binary Tree
The number of leaf nodes is = the number internal nodes(i.e.,
excluding root and leaf nodes) + 1.
The maximum number of nodes is the same as the number of
nodes in the binary tree, i.e., 2
h+
▪ The minimum number of nodes in the full binary tree is 2*h - 1.
The minimum height of the full binary tree is log 2
▪ (n+1) - 1.
The maximum height of the full binary tree can be computed as:
n= 2*h - 1
n+1 = 2*h
h = n+1/
Complete binary tree: The complete binary tree is a tree in which
all the nodes are completely filled except the last level that may or may
Array implementation of Binary tree:
If a node is at i
th
index:
▪ Left child would be at: [(2 * i) + 1]
▪ Right child would be at: [(2 * i) + 2]
▪ Parent would be at:
For example,
For D,
Left child = [(2 * 3) + 1] = 7 (H)
Right child = [(2 * 3) + 2] = 8 (I)
Parent = (B)
For E,
Left child = [(2 * 4) + 1] = 9 (As there's no child of E)
Same for right child.
If a node is at i
th
index:
▪ Left child would be at: [(2 * i)]
▪ Right child would be at: [(2 * i) + 1]
▪ Parent would be at:
For example,
For D,
Left child = [(2 * 4)] = 8 (H)
Right child = [(2 * 4) + 1] = 9 (I)
Parent = = 2(B)
For E,
Left child = [(2 * 5)] = 10 (As there's no child of E)
Same for right child.
For finding children using these formulas the tree have to be a Complete
Binary Tree.
In case of the tree is not complete we have to make it complete by
placing empty nodes. Here if D does not have any child and E have so
to make it complete we have to place empty nodes in left.
In that case the array representation will be like this:
For example,
For E,
Left child = [(2 * 4) + 1] = 9 (H)
Right child = [(2 * 4) + 2] = 10 (I)
Parent = (B)
09 December 2022 17:
n= 2*h - 1
n+1 = 2*h
h = n+1/
Complete binary tree: The complete binary tree is a tree in which
all the nodes are completely filled except the last level that may or may
not be completely filled. In the last level, all the nodes must be as left
as possible. In a complete binary tree, the nodes should be added from
the left.
The above tree is a complete binary tree because all the nodes are
completely filled, and all the nodes in the last level are added at the left
first.
Properties of Complete Binary Tree
The maximum number of nodes in complete binary tree is 2
h+
▪ - 1
The minimum number of nodes in complete binary tree is 2
h
The minimum height of a complete binary tree is log 2
▪ (n+1) - 1.
▪ The maximum height of a complete binary tree is log n.
Perfect binary tree: A tree is a perfect binary tree if all the internal
nodes have 2 children, and all the leaf nodes are at the same
level/depth. Consider a perfect binary tree with height h, the total
number of nodes in this case is given by 2h – 1.
A perfect tree is always complete or full but a complete tree is not
necessarily perfect or full.
Balanced binary tree: T he balanced binary tree is a tree in which
both the left and right trees differ by at most 1. A binary tree is said to
be ‘balanced’ if the tree height is O(logN), where ‘N’ is the number of
nodes. For example, AVL and Red-Black trees are balanced binary
tree.
Notice that in the first image, the right subtree is at the height of 3,
and the left subtree is at 2. The difference between the heights of the
left and the right subtree is therefore 1. Therefore, the given tree is a
balanced binary tree.
Degenerated/pathological binary tree: If in a binary tree each
node contains only one child node either on the left side or the right
side of the tree, it is known as a degenerate binary tree.
Degenerate binary trees are equal to linked lists in terms of
performance.
For E,
Left child = [(2 * 4) + 1] = 9 (H)
Right child = [(2 * 4) + 2] = 10 (I)
Parent = (B)
For D,
Left child = [(2 * 3) + 1] = 7 (empty)
Same for right child.
Traversal of Binary tree:
Inorder Traversal: LEFT - ROOT - RIGTHT [in means root in
the middle]
Preorder Traversal: ROOT
[pre means root
first]
Postorder Traversal: LEFT - RIGHT - ROOT [post means root
last]
Construct Binary tree:
○ Preorder and Inorder:
For root, scan the preorder from left to right and in preorder traversal first
one i.e. 1 is root then go to inorder find 1. In inorder all the left of 1 is left
children and right is right children. Then for next node find in preorder
which is after 1 i.e. 2 and the same process goes on.
○ Postorder and Inorder: