0% found this document useful (0 votes)
4 views18 pages

Binary Tree

The document outlines key properties and types of binary trees, including minimum and maximum nodes based on height, and the relationship between leaf nodes and nodes with two children. It describes various binary tree types such as full, degenerate, skewed, complete, perfect, and balanced trees, along with their characteristics. Additionally, it covers tree traversal techniques, including inorder, preorder, and postorder traversals, along with their algorithms and applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Binary Tree

The document outlines key properties and types of binary trees, including minimum and maximum nodes based on height, and the relationship between leaf nodes and nodes with two children. It describes various binary tree types such as full, degenerate, skewed, complete, perfect, and balanced trees, along with their characteristics. Additionally, it covers tree traversal techniques, including inorder, preorder, and postorder traversals, along with their algorithms and applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Property-01:

Minimum number of nodes in a binary tree of height H

=H+1

Example-

To construct a binary tree of height = 4, we need at least 4 + 1 = 5 nodes.


Property-02:

Maximum number of nodes in a binary tree of height H

= 2H+1 – 1

Example-

Maximum number of nodes in a binary tree of height 3

= 23+1 – 1 = 16 – 1 = 15 nodes

Thus, in a binary tree of height = 3, maximum number of nodes that can be inserted = 15.
Property-03:

Total Number of leaf nodes in a Binary Tree

= Total Number of nodes with 2 children + 1

Example-

Here,

• Number of leaf nodes = 3

• Number of nodes with 2 children = 2

Clearly, the number of leaf nodes is one greater than number of nodes with 2 children.

This verifies the above relation.

NOTE

It is interesting to note that-

The number of leaf nodes in any binary tree depends only on the number of nodes with 2 children.
Property-04:

Maximum number of nodes at any level ‘L’ in a binary tree

= 2L

Example-

Maximum number of nodes at level-2 in a binary tree

= 22

=4

Thus, in a binary tree, maximum number of nodes that can be present at level-2 = 4.
Types of Binary Tree based on the number of children:

The following are the types of Binary Tree based on the number of children:

1. Full Binary Tree

2. Degenerate Binary Tree

3. Skewed Binary Trees

1. Full Binary Tree

A Binary Tree is a full binary tree if every node has 0 or 2 children. The following are examples of
a full binary tree. We can also say a full binary tree is a binary tree in which all nodes except leaf
nodes have two children.

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.

Full Binary Tree


2. Degenerate (or pathological) tree

A Tree where every internal node has one child. Such trees are performance-wise same as linked
list. A degenerate or pathological tree is a tree having a single child either left or right.

Degenerate (or pathological) tree


3. Skewed Binary Tree

A skewed binary tree is a pathological/degenerate tree in which the tree is either dominated by
the left nodes or the right nodes. Thus, there are two types of skewed binary tree: left-skewed
binary tree and right-skewed binary tree.

Skewed Binary Tree


Types of Binary Tree On the basis of the completion of levels:

1. Complete Binary Tree

2. Perfect Binary Tree

3. Balanced Binary Tree

1. Complete Binary Tree

A Binary Tree is a Complete Binary Tree if all the levels are completely filled except possibly the
last level and the last level has all the keys as left as possible.

A complete binary tree is just like a full binary tree, but with two major differences:

• Every level except the last level must be completely filled.

• All the leaf elements must lean towards the left.

• The last leaf element might not have a right sibling, i.e. a complete binary tree doesn’t
have to be a full binary tree.

Complete Binary Tree


2. Perfect Binary Tree

A Binary tree is a Perfect Binary Tree in which all the internal nodes have two children and all leaf
nodes are at the same level.

The following are examples of Perfect Binary Trees.

A perfect binary tree is a type of binary tree in which every internal node has exactly two child
nodes and all the leaf nodes are at the same level.

Perfect Binary Tree

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
2ℎ+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.
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 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.

Example of Balanced and Unbalanced Binary Tree

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.
Difference between Full and Complete Binary Tree

A binary tree is a type of data structure where each node can only have two offspring at most
named as “left” and “right” child.

A Binary Tree

There are different types of binary tree but here we are going to discuss about the difference
of Complete binary tree and Full binary tree.

Full Binary Tree:

A full binary tree is a binary tree in which all of the nodes have either 0 or 2 offspring. In other
terms, a full binary tree is a binary tree in which all nodes, except the leaf nodes, have two
offspring.
Complete Binary Tree:

A binary tree is said to be a complete binary tree if all its levels, except possibly the last level,
have the maximum number of possible nodes, and all the nodes in the last level appear as far
left as possible.

A Complete Binary Tree

There are 2 points that you can recognize from here,

1. The leftmost side of the leaf node must always be filled first.
2. It isn’t necessary for the last leaf node to have a right sibling.
Check the following examples to understand the full and complete binary tree in a better way.

Example 1:

Neither complete nor full

• Node C has just one child therefore, it is not a Full binary tree.
• Node C also has a right child but no left child, therefore it is also not a Complete binary
tree.
Hence, the binary tree shown above is neither complete nor full binary tree.
Example 2:

Full but not complete

• All of the nodes have either 0 or 2 offspring, therefore, it is a Full binary tree.

• It is not a Complete binary tree because node B has no children whereas node C has
children, and according to a complete binary tree, nodes should be filled from the left side.

Hence, the binary tree shown above is a Full binary tree and it is not a Complete binary tree.

Example 3:

Complete but not full

• It is a complete binary tree as all the nodes are left filled.

• Node B has just one child, therefore, it is not a full binary tree.

Hence, the binary tree shown above is a Complete binary tree and it is not a Full binary tree.

Example 4:
Complete and full

• It is a Complete binary tree because all the nodes are left filled.

• All of the nodes have either 0 or 2 offspring, therefore, it is a full binary tree.

Hence, the binary tree shown above is both a complete and a full binary tree.

S.
Complete Binary Tree Full Binary Tree
No.

In a complete binary tree, a node in the In a full binary tree, a node cannot have just
1.
last level can have only one child. one child.

In a complete binary tree, the node There is no order of filling nodes in a full
2.
should be filled from the left to right. binary tree.

Complete binary trees are mainly used Full binary tree has no application as such
3.
in heap-based data structures. but is also called a proper binary tree.

A complete binary tree is also called A full binary tree also called proper binary
4.
almost complete binary tree. tree or 2-tree.
Tree Traversal Techniques
Tree Traversal techniques include various ways to visit all the nodes of the tree. Unlike
linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical
way to traverse them, trees can be traversed in different ways. In this article, we will
discuss all the tree traversal techniques along with their uses.

Tree Traversal Meaning:

Tree Traversal refers to the process of visiting or accessing each node of the tree exactly
once in a certain order. Tree traversal algorithms help us to visit and process all the nodes
of the tree. Since tree is not a linear data structure, there are multiple nodes which we
can visit after visiting a certain node. There are multiple tree traversal techniques which
decide the order in which the nodes of the tree are to be visited.

Tree Traversal Techniques:

A Tree Data Structure can be traversed in following ways:


• Depth First Search or DFS
o Inorder Traversal
o Preorder Traversal
o Postorder Traversal

• Level Order Traversal or Breadth First Search or BFS

Inorder Traversal:
Inorder traversal visits the node in the order: Left -> Root -> Right
Algorithm for Inorder Traversal:
Inorder(tree)
• Traverse the left subtree, i.e., call Inorder(left->subtree)
• Visit the root.
• Traverse the right subtree, i.e., call Inorder(right->subtree)

Uses of Inorder Traversal:


• In the case of binary search trees (BST), Inorder traversal gives
nodes in non-decreasing order.
• To get nodes of BST in non-increasing order, a variation of Inorder
traversal where Inorder traversal is reversed can be used.
• Inorder traversal can be used to evaluate arithmetic expressions
stored in expression trees.
Preorder Traversal:
Preorder traversal visits the node in the order: Root -> Left -> Right
Algorithm for Preorder Traversal:
Preorder(tree)
• Visit the root.
• Traverse the left subtree, i.e., call Preorder(left->subtree)
• Traverse the right subtree, i.e., call Preorder(right->subtree)

Uses of Preorder Traversal:


• Preorder traversal is used to create a copy of the tree.
• Preorder traversal is also used to get prefix expressions on an
expression tree.
Postorder Traversal:
Postorder traversal visits the node in the order: Left -> Right -> Root

Algorithm for Postorder Traversal:


Algorithm Postorder(tree)
• Traverse the left subtree, i.e., call Postorder(left->subtree)
• Traverse the right subtree, i.e., call Postorder(right->subtree)
• Visit the root

Uses of Postorder Traversal:


• Postorder traversal is used to delete the tree. Postorder traversal is
also useful to get the postfix expression of an expression tree.
• Postorder traversal can help in garbage collection algorithms,
particularly in systems where manual memory management is used.

You might also like