0% found this document useful (0 votes)
7 views

Data structures (Binary Trees)

Uploaded by

amjadiradat786
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Data structures (Binary Trees)

Uploaded by

amjadiradat786
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

BINARY TREES

Instructor:
Ms.Dur-e-Shawar Agha
ROAD MAP
 Introduction to Binary Tree

 Memory Representation of Binary Tree

 Tree Traversals
Binary Tree

o A tree whose elements have at most 2 children is called a binary tree.


o Since each element in a binary tree can have only 2 children, we typically
name
them the left and right child.
o A Binary Tree node contains following parts.
• Data
• Pointer to left child
• Pointer to right child
Representing Binary Tree in Memory

Let T be a Binary Tree. There are two ways of representing T in the memory
as follow

1. Sequential Representation of Binary Tree.

2. Link Representation of Binary Tree.


1. Link Representation of Binary Tree
Example
2. Sequential Representation of Binary Tree
Example
Tree Traversal
Tree traversal (also known as tree search) is a form of graph traversal and refers
to the process of visiting (checking and/or updating) each node in a tree data
structure, exactly once. Such traversals are classified by the order in which the
nodes are visited.”
Tree Traversal Algorithms can be classified broadly in the following two categories
by the order in which the nodes are visited:

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….

Consider the following example:


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)

Left → Root → Right


Contd….

Consider the following


example-
Contd….

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

Left → Right → Root


Contd….

Consider the following


example-
2. Breadth First search (BFS)

Level-Order Traversal-
Breadth First Traversal of a tree prints all the nodes of a tree level by
level.
Example
Implementation of Binary Tree

Binary Expression 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 is a famous algorithm used for lossless data encoding.

 It follows a Greedy approach, since it deals with generating minimum length prefix-free
binary codes
Huffman Coding Steps

The major steps involved in Huffman coding are-


Step I - Building a Huffman tree using the input set of symbols and weight/ frequency for
each symbol.

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

Step 2- Extract two nodes, say x and y, with minimum frequency.


Step 3- Create a new internal node z with x as its left child and y as
its right child. Also frequency(z)= frequency(x)+frequency(y)
Activity#01

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

Draw the binary expression tree for the following


expression

a. (3 * 7) + 9

b. (3 + 2)^3 / (46 - 12)

c. (NOT A) AND (B OR C)
SUMMARY
 Introduction to Binary Tree

 Types of Binary Tree

 Tree Traversal

 Binary Expression Tree

 Huffman Binary Tree

You might also like