Data structures (Binary Trees)
Data structures (Binary Trees)
Instructor:
Ms.Dur-e-Shawar Agha
ROAD MAP
Introduction to Binary Tree
Tree Traversals
Binary Tree
Let T be a Binary Tree. There are two ways of representing T in the memory
as follow
1. Depth-First Search (DFS) Algorithm: It starts with the root node and first
visits all nodes of one branch as deep as possible of the chosen Node and
before backtracking, it visits all other branches in a similar fashion. There are
three sub- types under this, which we will cover in this article.
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
Contd….
2. Breadth-First Search (BFS) Algorithm: It also starts from the root node and
visits all nodes of current depth before moving to the next depth in the tree.
We will cover one algorithm of BFS type in the upcoming section.
• Level-order Traversal
1. Depth First search (DFS)
Preorder Traversal
• Algorithm
i. Visit the root
ii.Traverse the left sub tree i.e. call Preorder (left sub
tree)
iii.Traverse the right sub tree i.e. call Preorder (right sub
tree)
Root → Left → Right
Contd….
Inorder Traversal
• Algorithm
i. Traverse the left sub tree i.e. call Inorder (left sub
tree)
ii. Visit the root
iii. Traverse the right sub tree i.e. call Inorder (right sub
tree)
Postorder Traversal
• Algorithm
i. Traverse the left sub tree i.e. call Postorder (left sub
tree)
ii.Traverse the right sub tree i.e. call Postorder (right sub
tree) iii.Visit the root
Level-Order Traversal-
Breadth First Traversal of a tree prints all the nodes of a tree level by
level.
Example
Implementation of Binary Tree
The expression tree is a binary tree in which each internal node corresponds
to the
operator and each leaf node corresponds to the operand.
Example
a + (b * c) + d * (e + f)
Huffman Coding
Developed by David Huffman in 1951, this technique is the basis for all data
compression and encoding schemes.
It follows a Greedy approach, since it deals with generating minimum length prefix-free
binary codes
Huffman Coding Steps
A Huffman tree, similar to a binary tree data structure, needs to be created having n leaf
nodes and n-1 internal nodes.
Step II - Assigning the binary codes to each symbol by traversing Huffman tree.
Generally, bit ‘0’ represents the left child and bit ‘1’ represents the right child
Algorithm for creating Huffman Tree
The Huffman code for each letter is derived from a full binary tree called the Huffman
coding tree, or simply the Huffman tree.
Each leaf of the Huffman tree corresponds to a letter, and we define the weight of the
leaf node to be the weight (frequency) of its associated letter. The goal is to build a tree
with the minimum external path weight.
A letter with high weight should have low depth, so that it will count the least against
the total path length. As a result, another letter might be pushed deeper in the tree if it
has less weight.
Types of Huffman Coding
"Fixed-Length Encoding“
Fixed-Length Encoding means assigning each character binary codes of fixed length. Since
there are 6 characters so we need 3 bits to store each character uniquely. So, total bits
required to store the whole file is 3.(45+11+6+16+100+50) = 684 bits.
"Variable-Length Encoding“
Since in this method each character is being assigned variable length binary codes so what
we try to do here is to assign frequent characters short code words and unfrequented
characters long code words. Consider the scheme:
a=10,b=100,c=101,d=110,e=1,f=11
Example # 1
Example # 2
Characters Frequencies
a 10
e 15
i 12
o 3
u 4
s 13
t 1
Step 1- Create a leaf node for each character
If the binary tree in figure is traversed in inorder, preorder and postorder then the
order in which the nodes will be visited is ?
Activity#02
a. (3 * 7) + 9
c. (NOT A) AND (B OR C)
SUMMARY
Introduction to Binary Tree
Tree Traversal