SlideShare a Scribd company logo
Tree
Unit 6
So far we discussed Linear data structures like
stack
Ashim Lamichhane 2
Introduction to trees
• So far we have discussed mainly linear data structures – strings, arrays,
lists, stacks and queues
• Now we will discuss a non-linear data structure called tree.
• Trees are mainly used to represent data containing a hierarchical
relationship between elements, for example, records, family trees and
table of contents.
• Consider a parent-child relationship
Ashim Lamichhane 3
Ashim Lamichhane 4
Tree
• A tree is an abstract model of a hierarchical structure that consists of
nodes with a parent-child relationship.
• Tree is a sequence of nodes
• There is a starting node known as a root node
• Every node other than the root has a parent node.
• Nodes may have any number of children
Ashim Lamichhane 5
Ashim Lamichhane 6
Ashim Lamichhane 7
Some Key Terms:
• Root − Node at the top of the tree is called root.
• Parent − Any node except root node has one edge upward to a node called parent.
• Child − Node below a given node connected by its edge downward is called its child node.
• Sibling – Child of same node are called siblings
• Leaf − Node which does not have any child node is called leaf node.
• Sub tree − Sub tree represents descendants of a node.
• Levels − Level of a node represents the generation of a node. If root node is at level 0, then its next child node
is at level 1, its grandchild is at level 2 and so on.
• keys − Key represents a value of a node based on which a search operation is to be carried out for a node.
Ashim Lamichhane 8
Some Key Terms:
• Degree of a node:
• The degree of a node is the number of children of that node
• Degree of a Tree:
• The degree of a tree is the maximum degree of nodes in a given tree
• Path:
• It is the sequence of consecutive edges from source node to destination node.
• Height of a node:
• The height of a node is the max path length form that node to a leaf node.
• Height of a tree:
• The height of a tree is the height of the root
• Depth of a tree:
• Depth of a tree is the max level of any leaf in the tree
Ashim Lamichhane 9
Ashim Lamichhane 10
Characteristics of trees
• Non-linear data structure
• Combines advantages of an ordered array
• Searching as fast as in ordered array
• Insertion and deletion as fast as in linked list
• Simple and fast
Ashim Lamichhane 11
Application
• Directory structure of a file store
• Structure of an arithmetic expressions
• Used in almost every 3D video game to determine what objects need to be
rendered.
• Used in almost every high-bandwidth router for storing router-tables.
• used in compression algorithms, such as those used by the .jpeg and .mp3 file-
formats.
FOR Further detail Click Here
Ashim Lamichhane 12
Introduction To Binary Trees
• A binary tree, is a tree in which no node can have more than two
children.
• Consider a binary tree T, here ‘A’ is the root node of the binary tree T.
• ‘B’ is the left child of ‘A’ and ‘C’ is the right
child of ‘A’
• i.e A is a father of B and C.
• The node B and C are called siblings.
• Nodes D,H,I,F,J are leaf node
Ashim Lamichhane 13
Binary Trees
• A binary tree, T, is either empty or such that
I. T has a special node called the root node
II. T has two sets of nodes LT and RT, called the left subtree and right subtree of
T, respectively.
III. LT and RT are binary trees.
Ashim Lamichhane 14
Binary Tree
• A binary tree is a finite set of elements that are either empty or is
partitioned into three disjoint subsets.
• The first subset contains a single element called the root of the tree.
• The other two subsets are themselves binary trees called the left and right
sub-trees of the original tree.
• A left or right sub-tree can be empty.
• Each element of a binary tree is called a node of the tree.
Ashim Lamichhane 15
The following figure shows a binary tree with 9 nodes where A is the root
Ashim Lamichhane 16
Binary Tree
• The root node of this binary tree is A.
• The left sub tree of the root node, which we denoted by LA, is the set
LA = {B,D,E,G} and the right sub tree of the root node, RA is the set
RA={C,F,H}
• The root node of LA is node B, the root node of RA is C and so on
Ashim Lamichhane 17
Binary Tree Properties
• If a binary tree contains m nodes at level L, it contains at most 2m
nodes at level L+1
• Since a binary tree can contain at most 1 node at level 0 (the root), it
contains at most 2L nodes at level L.
Ashim Lamichhane 18
Types of Binary Tree
• Complete binary tree
• Strictly binary tree
• Almost complete binary tree
Ashim Lamichhane 19
Strictly binary tree
• If every non-leaf node in a binary tree has nonempty left and right sub-trees, then
such a tree is called a strictly binary tree.
• Or, to put it another way, all of the nodes in a strictly binary tree are of degree zero
or two, never degree one.
• A strictly binary tree with
N leaves always contains 2N – 1 nodes.
Ashim Lamichhane 20
Complete binary tree
• A complete binary tree is a binary tree in which every level, except possibly the last, is completely
filled, and all nodes are as far left as possible.
• A complete binary tree of depth d is called strictly binary tree if all of whose leaves are at level d.
• A complete binary tree has 2d nodes at every depth d and 2d -1 non leaf nodes
Ashim Lamichhane 21
Almost complete binary tree
• An almost complete binary tree is a tree where for a right child, there is always a
left child, but for a left child there may not be a right child.
Ashim Lamichhane 22
Ashim Lamichhane 23
Ashim Lamichhane 24
Tree traversal
• Traversal is a process to visit all the nodes of a tree and may print their
values too.
• All nodes are connected via edges (links) we always start from the root
(head) node.
• There are three ways which we use to traverse a tree
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
• Generally we traverse a tree to search or locate given item or key in the tree
or to print all the values it contains.
Ashim Lamichhane 25
Pre-order, In-order, Post-order
• Pre-order
<root><left><right>
• In-order
<left><root><right>
• Post-order
<left><right><root>
Ashim Lamichhane 26
Pre-order Traversal
• The preorder traversal of a nonempty binary tree is defined as follows:
• Visit the root node
• Traverse the left sub-tree in preorder
• Traverse the right sub-tree in preorder
Ashim Lamichhane 27
Pre-order Pseudocode
struct Node{
char data;
Node *left;
Node *right;
}
void Preorder(Node *root)
{
if (root==NULL) return;
printf (“%c”, root->data);
Preorder(root->left);
Preorder(root->right);
}
Ashim Lamichhane 28
In-order traversal
• The in-order traversal of a nonempty binary tree is defined as follows:
• Traverse the left sub-tree in in-order
• Visit the root node
• Traverse the right sub-tree in inorder
• The in-order traversal output
of the given tree is
H D I B E A F C G
Ashim Lamichhane 29
In-order Pseudocode
struct Node{
char data;
Node *left;
Node *right;
}
void Inorder(Node *root)
{
if (root==NULL) return;
Inorder(root->left);
printf (“%c”, root->data);
Inorder(root->right);
}
Ashim Lamichhane 30
Post-order traversal
• The in-order traversal of a nonempty binary tree is defined as follows:
• Traverse the left sub-tree in post-order
• Traverse the right sub-tree in post-order
• Visit the root node
• The in-order traversal output
of the given tree is
H I D E B F G C A
Ashim Lamichhane 31
Post-order Pseudocode
struct Node{
char data;
Node *left;
Node *right;
}
void Postorder(Node *root)
{
if (root==NULL) return;
Postorder(root->left);
Postorder(root->right);
printf (“%c”, root->data);
}
Ashim Lamichhane 32
Binary Search Tree(BST)
• A binary search tree (BST) is a binary tree that is either empty or in
which every node contains a key (value) and satisfies the following
conditions:
• All keys in the left sub-tree of the root are smaller than the key in the root
node
• All keys in the right sub-tree of the root are greater than the key in the root
node
• The left and right sub-trees of the root are again binary search trees
Ashim Lamichhane 33
Binary Search Tree(BST)
Ashim Lamichhane 34
Binary Search Tree(BST)
• A binary search tree is basically a binary tree, and therefore it can be
traversed in inorder, preorder and postorder.
• If we traverse a binary search tree in inorder and print the identifiers
contained in the nodes of the tree, we get a sorted list of identifiers in
ascending order.
Ashim Lamichhane 35
Why Binary Search Tree?
• Let us consider a problem of searching a list.
• If a list is ordered searching becomes faster if we use contiguous
list(array).
• But if we need to make changes in the list, such as inserting new
entries or deleting old entries, (SLOWER!!!!) because insertion and
deletion in a contiguous list requires moving many of the entries every
time.
Ashim Lamichhane 36
Why Binary Search Tree?
• So we may think of using a linked list because it permits insertion and
deletion to be carried out by adjusting only few pointers.
• But in an n-linked list, there is no way to move through the list other
than one node at a time, permitting only sequential access.
• Binary trees provide an excellent solution to this problem. By making
the entries of an ordered list into the nodes of a binary search tree, we
find that we can search for a key in O(logn)
Ashim Lamichhane 37
Binary Search Tree(BST)
Time Complexity
Array Linked List BST
Search O(n) O(n) O(logn)
Insert O(1) O(1) O(logn)
Remove O(n) O(n) O(logn)
Ashim Lamichhane 38
Operations on Binary Search Tree (BST)
• Following operations can be done in BST:
• Search(k, T): Search for key k in the tree T. If k is found in some node of tree
then return true otherwise return false.
• Insert(k, T): Insert a new node with value k in the info field in the tree T such
that the property of BST is maintained.
• Delete(k, T):Delete a node with value k in the info field from the tree T such
that the property of BST is maintained.
• FindMin(T), FindMax(T): Find minimum and maximum element from the
given nonempty BST.
Ashim Lamichhane 39
Searching Through The BST
• Compare the target value with the element in the root node
 If the target value is equal, the search is successful.
If target value is less, search the left subtree.
If target value is greater, search the right subtree.
If the subtree is empty, the search is unsuccessful.
Ashim Lamichhane 40
Ashim Lamichhane 41
Insertion of a node in BST
• To insert a new item in a tree, we must first verify that its key is different
from those of existing elements.
• If a new value is less, than the current node's value, go to the left subtree,
else go to the right subtree.
• Following this simple rule, the algorithm reaches a node, which has no left
or right subtree.
• By the moment a place for insertion is found, we can say for sure, that a
new value has no duplicate in the tree.
Ashim Lamichhane 42
Algorithm for insertion in BST
• Check, whether value in current node and a new value are equal. If so,
duplicate is found. Otherwise,
• if a new value is less, than the node's value:
• if a current node has no left child, place for insertion has been found;
• otherwise, handle the left child with the same algorithm.
• if a new value is greater, than the node's value:
• if a current node has no right child, place for insertion has been found;
• otherwise, handle the right child with the same algorithm.
Ashim Lamichhane 43
Ashim Lamichhane 44
Ashim Lamichhane 45
Deleting a node from the BST
• While deleting a node from BST, there may be three cases:
1. The node to be deleted may be a leaf node:
• In this case simply delete a node and set null pointer to its parents those side
at which this deleted node exist.
Ashim Lamichhane 46
Deleting a node from the BST
2. The node to be deleted has one child
• In this case the child of the node to be deleted is appended to its parent node.
Suppose node to be deleted is 18
Ashim Lamichhane 47
Deleting a node from the BST
Ashim Lamichhane 48
Ashim Lamichhane 49
Ashim Lamichhane 50
Huffman Algorithm
• Huffman algorithm is a method for building an extended binary tree
with a minimum weighted path length from a set of given weights.
• This is a method for the construction of minimum redundancy codes.
• Applicable to many forms of data transmission
• multimedia codecs such as JPEG and MP3
Ashim Lamichhane 51
Huffman Algorithm
• 1951, David Huffman found the “most efficient method of representing
numbers, letters, and other symbols using binary code”. Now standard
method used for data compression.
• In Huffman Algorithm, a set of nodes assigned with values if fed to the
algorithm. Initially 2 nodes are considered and their sum forms their parent
node.
• When a new element is considered, it can be added to the tree.
• Its value and the previously calculated sum of the tree are used to form the
new node which in turn becomes their parent.
Ashim Lamichhane 52
Huffman Algorithm
• Let us take any four characters and their frequencies, and sort this list by
increasing frequency.
• Since to represent 4 characters the 2 bit is sufficient thus take initially two
bits for each character this is called fixed length character.
• Here before using Huffman algorithm the total number of bits required is:
nb=3*2+5*2+7*2+10*2 =06+10+14+20 =50bits
Ashim Lamichhane 53
character frequencies
E 10
T 7
O 5
A 3
Character frequencies code
A 3 00
O 5 01
T 7 10
E 10 11
sort
Ashim Lamichhane 54
• Thus after using Huffman algorithm the total number of bits required is
nb=3*3+5*3+7*2+10*1 =09+15+14+10 =48bits
i.e
(50-48)/50*100%=4%
Since in this small example we save about 4% space by using Huffman algorithm. If we take large
example with a lot of characters and their frequencies we can save a lot of space
Ashim Lamichhane 55
Character frequencies code
A 3 110
O 5 111
T 7 10
E 10 0
• Lets say you have a set of numbers and their frequency of use and
want to create a huffman encoding for them
Ashim Lamichhane 56
Value Frequencies
1 5
2 7
3 10
4 15
5 20
6 45
Huffman Algorithm
Huffman Algorithm
• Creating a Huffman tree is simple. Sort this list by frequency and make the two-lowest elements
into leaves, creating a parent node with a frequency that is the sum of the two lower element's
frequencies:
12:*
/ 
5:1 7:2
• The two elements are removed from the list and the new parent node, with frequency 12, is
inserted into the list by frequency. So now the list, sorted by frequency, is:
10:3
12:*
15:4
20:5
45:6
Ashim Lamichhane 57
Huffman Algorithm
• You then repeat the loop, combining the two lowest elements. This results in:
22:*
/ 
10:3 12:*
/ 
5:1 7:2
• The two elements are removed from the list and the new parent node, with frequency 12, is
inserted into the list by frequency. So now the list, sorted by frequency, is:
15:4
20:5
22: *
45:6
Ashim Lamichhane 58
Huffman Algorithm
Ashim Lamichhane 59
After sorted
Ashim Lamichhane 60
Value Z K M C L D U E
Frequency 2 7 24 32 42 42 37 120
Value C D E K L M U Z
Frequency 32 42 120 7 42 24 37 2
Huffman Algorithm
We can represent it using 3 bit
Huffman Algorithm
• Find code for
• DEED
• MUCK
• Try to decode the bit string
1011001110111101
Ashim Lamichhane 61
Assignments
• Slides at myblog
http://www.ashimlamichhane.com.np/2016/07/tree-slide-for-data-
structure-and-algorithm/
• Assignments at github
https://github.com/ashim888/dataStructureAndAlgorithm/tree/dev/As
signments/assignment_7
Ashim Lamichhane 62
Reference
• https://www.siggraph.org/education/materials/HyperGraph/video/mp
eg/mpegfaq/huffman_tutorial.html
• https://en.wikipedia.org/wiki/Binary_search_tree
• https://www.cs.swarthmore.edu/~newhall/unixhelp/Java_bst.pdf
• https://www.cs.usfca.edu/~galles/visualization/BST.html
• https://www.cs.rochester.edu/~gildea/csc282/slides/C12-bst.pdf
• http://www.tutorialspoint.com/data_structures_algorithms/tree_data
_structure.htm
Ashim Lamichhane 63
Ad

More Related Content

What's hot (20)

Linked list
Linked listLinked list
Linked list
akshat360
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Poojith Chowdhary
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Ninad Mankar
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
Archie Jamwal
 
Stack
StackStack
Stack
Zaid Shabbir
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
NalinNishant3
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
Kevin Jadiya
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
stack presentation
stack presentationstack presentation
stack presentation
Shivalik college of engineering
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
harsh kothari
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
Anuj Modi
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
Anandhasilambarasan D
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 

Viewers also liked (20)

Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
Zaid Shabbir
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
Sumit Gupta
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
Taibah University, College of Computer Science & Engineering
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Tree
TreeTree
Tree
SHEETAL WAGHMARE
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
Äshïsh Jäïn
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
geeksrik
 
Tree
TreeTree
Tree
Raj Sarode
 
Tree
TreeTree
Tree
Simran Kaur
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
Dhaval Kaneria
 
Preparation Data Structures 10 trees
Preparation Data Structures 10 treesPreparation Data Structures 10 trees
Preparation Data Structures 10 trees
Andres Mendez-Vazquez
 
Threaded binarytree&heapsort
Threaded binarytree&heapsortThreaded binarytree&heapsort
Threaded binarytree&heapsort
Ssankett Negi
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
Rai University
 
Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1
Amar Rawat
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
Ashim Lamichhane
 
Implementation of trees
Implementation of trees Implementation of trees
Implementation of trees
Mubashar Mehmood
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
Ashim Lamichhane
 
Full threded binary tree
Full threded binary treeFull threded binary tree
Full threded binary tree
Soham Kansodaria
 
Ad

Similar to Tree - Data Structure (20)

BASIC TREE AND TYPES OF DI CONCEPTS.pptx
BASIC TREE  AND TYPES OF DI CONCEPTS.pptxBASIC TREE  AND TYPES OF DI CONCEPTS.pptx
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
tpvvsreenivasarao
 
Introduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptxIntroduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
DEEPAK948083
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
LavanyaJ28
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
SeethaDinesh
 
Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
Dr. SURBHI SAROHA
 
Binary trees
Binary treesBinary trees
Binary trees
Rajendran
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
sherrilsiddhardh
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
worldchannel
 
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptxDS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
Dabbal Singh Mahara
 
Binary tree
Binary treeBinary tree
Binary tree
MKalpanaDevi
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
Farhana Shaikh
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
Parthipan Parthi
 
Tree
TreeTree
Tree
invertis university
 
Lecture 9 (DS) - Tree, Tree Traversal.pptx
Lecture 9 (DS) - Tree, Tree Traversal.pptxLecture 9 (DS) - Tree, Tree Traversal.pptx
Lecture 9 (DS) - Tree, Tree Traversal.pptx
itxdevilmehar
 
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTINGSEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
mohanrajm63
 
Tree
TreeTree
Tree
sbkbca
 
Presentation1-Data structure S-Tree.pptx
Presentation1-Data structure S-Tree.pptxPresentation1-Data structure S-Tree.pptx
Presentation1-Data structure S-Tree.pptx
Praveen156918
 
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
BASIC TREE  AND TYPES OF DI CONCEPTS.pptxBASIC TREE  AND TYPES OF DI CONCEPTS.pptx
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
tpvvsreenivasarao
 
Introduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptxIntroduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptxDS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
Farhana Shaikh
 
Lecture 9 (DS) - Tree, Tree Traversal.pptx
Lecture 9 (DS) - Tree, Tree Traversal.pptxLecture 9 (DS) - Tree, Tree Traversal.pptx
Lecture 9 (DS) - Tree, Tree Traversal.pptx
itxdevilmehar
 
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTINGSEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
mohanrajm63
 
Presentation1-Data structure S-Tree.pptx
Presentation1-Data structure S-Tree.pptxPresentation1-Data structure S-Tree.pptx
Presentation1-Data structure S-Tree.pptx
Praveen156918
 
Ad

More from Ashim Lamichhane (16)

Searching
SearchingSearching
Searching
Ashim Lamichhane
 
Sorting
SortingSorting
Sorting
Ashim Lamichhane
 
Queues
QueuesQueues
Queues
Ashim Lamichhane
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
Ashim Lamichhane
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
Ashim Lamichhane
 
Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
Ashim Lamichhane
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
Ashim Lamichhane
 
Unit 11. Graphics
Unit 11. GraphicsUnit 11. Graphics
Unit 11. Graphics
Ashim Lamichhane
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
Ashim Lamichhane
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
Ashim Lamichhane
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
Ashim Lamichhane
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
Ashim Lamichhane
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
Ashim Lamichhane
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
Ashim Lamichhane
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
Ashim Lamichhane
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
Ashim Lamichhane
 

Recently uploaded (20)

Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 

Tree - Data Structure

  • 2. So far we discussed Linear data structures like stack Ashim Lamichhane 2
  • 3. Introduction to trees • So far we have discussed mainly linear data structures – strings, arrays, lists, stacks and queues • Now we will discuss a non-linear data structure called tree. • Trees are mainly used to represent data containing a hierarchical relationship between elements, for example, records, family trees and table of contents. • Consider a parent-child relationship Ashim Lamichhane 3
  • 5. Tree • A tree is an abstract model of a hierarchical structure that consists of nodes with a parent-child relationship. • Tree is a sequence of nodes • There is a starting node known as a root node • Every node other than the root has a parent node. • Nodes may have any number of children Ashim Lamichhane 5
  • 8. Some Key Terms: • Root − Node at the top of the tree is called root. • Parent − Any node except root node has one edge upward to a node called parent. • Child − Node below a given node connected by its edge downward is called its child node. • Sibling – Child of same node are called siblings • Leaf − Node which does not have any child node is called leaf node. • Sub tree − Sub tree represents descendants of a node. • Levels − Level of a node represents the generation of a node. If root node is at level 0, then its next child node is at level 1, its grandchild is at level 2 and so on. • keys − Key represents a value of a node based on which a search operation is to be carried out for a node. Ashim Lamichhane 8
  • 9. Some Key Terms: • Degree of a node: • The degree of a node is the number of children of that node • Degree of a Tree: • The degree of a tree is the maximum degree of nodes in a given tree • Path: • It is the sequence of consecutive edges from source node to destination node. • Height of a node: • The height of a node is the max path length form that node to a leaf node. • Height of a tree: • The height of a tree is the height of the root • Depth of a tree: • Depth of a tree is the max level of any leaf in the tree Ashim Lamichhane 9
  • 11. Characteristics of trees • Non-linear data structure • Combines advantages of an ordered array • Searching as fast as in ordered array • Insertion and deletion as fast as in linked list • Simple and fast Ashim Lamichhane 11
  • 12. Application • Directory structure of a file store • Structure of an arithmetic expressions • Used in almost every 3D video game to determine what objects need to be rendered. • Used in almost every high-bandwidth router for storing router-tables. • used in compression algorithms, such as those used by the .jpeg and .mp3 file- formats. FOR Further detail Click Here Ashim Lamichhane 12
  • 13. Introduction To Binary Trees • A binary tree, is a tree in which no node can have more than two children. • Consider a binary tree T, here ‘A’ is the root node of the binary tree T. • ‘B’ is the left child of ‘A’ and ‘C’ is the right child of ‘A’ • i.e A is a father of B and C. • The node B and C are called siblings. • Nodes D,H,I,F,J are leaf node Ashim Lamichhane 13
  • 14. Binary Trees • A binary tree, T, is either empty or such that I. T has a special node called the root node II. T has two sets of nodes LT and RT, called the left subtree and right subtree of T, respectively. III. LT and RT are binary trees. Ashim Lamichhane 14
  • 15. Binary Tree • A binary tree is a finite set of elements that are either empty or is partitioned into three disjoint subsets. • The first subset contains a single element called the root of the tree. • The other two subsets are themselves binary trees called the left and right sub-trees of the original tree. • A left or right sub-tree can be empty. • Each element of a binary tree is called a node of the tree. Ashim Lamichhane 15
  • 16. The following figure shows a binary tree with 9 nodes where A is the root Ashim Lamichhane 16
  • 17. Binary Tree • The root node of this binary tree is A. • The left sub tree of the root node, which we denoted by LA, is the set LA = {B,D,E,G} and the right sub tree of the root node, RA is the set RA={C,F,H} • The root node of LA is node B, the root node of RA is C and so on Ashim Lamichhane 17
  • 18. Binary Tree Properties • If a binary tree contains m nodes at level L, it contains at most 2m nodes at level L+1 • Since a binary tree can contain at most 1 node at level 0 (the root), it contains at most 2L nodes at level L. Ashim Lamichhane 18
  • 19. Types of Binary Tree • Complete binary tree • Strictly binary tree • Almost complete binary tree Ashim Lamichhane 19
  • 20. Strictly binary tree • If every non-leaf node in a binary tree has nonempty left and right sub-trees, then such a tree is called a strictly binary tree. • Or, to put it another way, all of the nodes in a strictly binary tree are of degree zero or two, never degree one. • A strictly binary tree with N leaves always contains 2N – 1 nodes. Ashim Lamichhane 20
  • 21. Complete binary tree • A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. • A complete binary tree of depth d is called strictly binary tree if all of whose leaves are at level d. • A complete binary tree has 2d nodes at every depth d and 2d -1 non leaf nodes Ashim Lamichhane 21
  • 22. Almost complete binary tree • An almost complete binary tree is a tree where for a right child, there is always a left child, but for a left child there may not be a right child. Ashim Lamichhane 22
  • 25. Tree traversal • Traversal is a process to visit all the nodes of a tree and may print their values too. • All nodes are connected via edges (links) we always start from the root (head) node. • There are three ways which we use to traverse a tree • In-order Traversal • Pre-order Traversal • Post-order Traversal • Generally we traverse a tree to search or locate given item or key in the tree or to print all the values it contains. Ashim Lamichhane 25
  • 26. Pre-order, In-order, Post-order • Pre-order <root><left><right> • In-order <left><root><right> • Post-order <left><right><root> Ashim Lamichhane 26
  • 27. Pre-order Traversal • The preorder traversal of a nonempty binary tree is defined as follows: • Visit the root node • Traverse the left sub-tree in preorder • Traverse the right sub-tree in preorder Ashim Lamichhane 27
  • 28. Pre-order Pseudocode struct Node{ char data; Node *left; Node *right; } void Preorder(Node *root) { if (root==NULL) return; printf (“%c”, root->data); Preorder(root->left); Preorder(root->right); } Ashim Lamichhane 28
  • 29. In-order traversal • The in-order traversal of a nonempty binary tree is defined as follows: • Traverse the left sub-tree in in-order • Visit the root node • Traverse the right sub-tree in inorder • The in-order traversal output of the given tree is H D I B E A F C G Ashim Lamichhane 29
  • 30. In-order Pseudocode struct Node{ char data; Node *left; Node *right; } void Inorder(Node *root) { if (root==NULL) return; Inorder(root->left); printf (“%c”, root->data); Inorder(root->right); } Ashim Lamichhane 30
  • 31. Post-order traversal • The in-order traversal of a nonempty binary tree is defined as follows: • Traverse the left sub-tree in post-order • Traverse the right sub-tree in post-order • Visit the root node • The in-order traversal output of the given tree is H I D E B F G C A Ashim Lamichhane 31
  • 32. Post-order Pseudocode struct Node{ char data; Node *left; Node *right; } void Postorder(Node *root) { if (root==NULL) return; Postorder(root->left); Postorder(root->right); printf (“%c”, root->data); } Ashim Lamichhane 32
  • 33. Binary Search Tree(BST) • A binary search tree (BST) is a binary tree that is either empty or in which every node contains a key (value) and satisfies the following conditions: • All keys in the left sub-tree of the root are smaller than the key in the root node • All keys in the right sub-tree of the root are greater than the key in the root node • The left and right sub-trees of the root are again binary search trees Ashim Lamichhane 33
  • 35. Binary Search Tree(BST) • A binary search tree is basically a binary tree, and therefore it can be traversed in inorder, preorder and postorder. • If we traverse a binary search tree in inorder and print the identifiers contained in the nodes of the tree, we get a sorted list of identifiers in ascending order. Ashim Lamichhane 35
  • 36. Why Binary Search Tree? • Let us consider a problem of searching a list. • If a list is ordered searching becomes faster if we use contiguous list(array). • But if we need to make changes in the list, such as inserting new entries or deleting old entries, (SLOWER!!!!) because insertion and deletion in a contiguous list requires moving many of the entries every time. Ashim Lamichhane 36
  • 37. Why Binary Search Tree? • So we may think of using a linked list because it permits insertion and deletion to be carried out by adjusting only few pointers. • But in an n-linked list, there is no way to move through the list other than one node at a time, permitting only sequential access. • Binary trees provide an excellent solution to this problem. By making the entries of an ordered list into the nodes of a binary search tree, we find that we can search for a key in O(logn) Ashim Lamichhane 37
  • 38. Binary Search Tree(BST) Time Complexity Array Linked List BST Search O(n) O(n) O(logn) Insert O(1) O(1) O(logn) Remove O(n) O(n) O(logn) Ashim Lamichhane 38
  • 39. Operations on Binary Search Tree (BST) • Following operations can be done in BST: • Search(k, T): Search for key k in the tree T. If k is found in some node of tree then return true otherwise return false. • Insert(k, T): Insert a new node with value k in the info field in the tree T such that the property of BST is maintained. • Delete(k, T):Delete a node with value k in the info field from the tree T such that the property of BST is maintained. • FindMin(T), FindMax(T): Find minimum and maximum element from the given nonempty BST. Ashim Lamichhane 39
  • 40. Searching Through The BST • Compare the target value with the element in the root node  If the target value is equal, the search is successful. If target value is less, search the left subtree. If target value is greater, search the right subtree. If the subtree is empty, the search is unsuccessful. Ashim Lamichhane 40
  • 42. Insertion of a node in BST • To insert a new item in a tree, we must first verify that its key is different from those of existing elements. • If a new value is less, than the current node's value, go to the left subtree, else go to the right subtree. • Following this simple rule, the algorithm reaches a node, which has no left or right subtree. • By the moment a place for insertion is found, we can say for sure, that a new value has no duplicate in the tree. Ashim Lamichhane 42
  • 43. Algorithm for insertion in BST • Check, whether value in current node and a new value are equal. If so, duplicate is found. Otherwise, • if a new value is less, than the node's value: • if a current node has no left child, place for insertion has been found; • otherwise, handle the left child with the same algorithm. • if a new value is greater, than the node's value: • if a current node has no right child, place for insertion has been found; • otherwise, handle the right child with the same algorithm. Ashim Lamichhane 43
  • 46. Deleting a node from the BST • While deleting a node from BST, there may be three cases: 1. The node to be deleted may be a leaf node: • In this case simply delete a node and set null pointer to its parents those side at which this deleted node exist. Ashim Lamichhane 46
  • 47. Deleting a node from the BST 2. The node to be deleted has one child • In this case the child of the node to be deleted is appended to its parent node. Suppose node to be deleted is 18 Ashim Lamichhane 47
  • 48. Deleting a node from the BST Ashim Lamichhane 48
  • 51. Huffman Algorithm • Huffman algorithm is a method for building an extended binary tree with a minimum weighted path length from a set of given weights. • This is a method for the construction of minimum redundancy codes. • Applicable to many forms of data transmission • multimedia codecs such as JPEG and MP3 Ashim Lamichhane 51
  • 52. Huffman Algorithm • 1951, David Huffman found the “most efficient method of representing numbers, letters, and other symbols using binary code”. Now standard method used for data compression. • In Huffman Algorithm, a set of nodes assigned with values if fed to the algorithm. Initially 2 nodes are considered and their sum forms their parent node. • When a new element is considered, it can be added to the tree. • Its value and the previously calculated sum of the tree are used to form the new node which in turn becomes their parent. Ashim Lamichhane 52
  • 53. Huffman Algorithm • Let us take any four characters and their frequencies, and sort this list by increasing frequency. • Since to represent 4 characters the 2 bit is sufficient thus take initially two bits for each character this is called fixed length character. • Here before using Huffman algorithm the total number of bits required is: nb=3*2+5*2+7*2+10*2 =06+10+14+20 =50bits Ashim Lamichhane 53 character frequencies E 10 T 7 O 5 A 3 Character frequencies code A 3 00 O 5 01 T 7 10 E 10 11 sort
  • 55. • Thus after using Huffman algorithm the total number of bits required is nb=3*3+5*3+7*2+10*1 =09+15+14+10 =48bits i.e (50-48)/50*100%=4% Since in this small example we save about 4% space by using Huffman algorithm. If we take large example with a lot of characters and their frequencies we can save a lot of space Ashim Lamichhane 55 Character frequencies code A 3 110 O 5 111 T 7 10 E 10 0
  • 56. • Lets say you have a set of numbers and their frequency of use and want to create a huffman encoding for them Ashim Lamichhane 56 Value Frequencies 1 5 2 7 3 10 4 15 5 20 6 45 Huffman Algorithm
  • 57. Huffman Algorithm • Creating a Huffman tree is simple. Sort this list by frequency and make the two-lowest elements into leaves, creating a parent node with a frequency that is the sum of the two lower element's frequencies: 12:* / 5:1 7:2 • The two elements are removed from the list and the new parent node, with frequency 12, is inserted into the list by frequency. So now the list, sorted by frequency, is: 10:3 12:* 15:4 20:5 45:6 Ashim Lamichhane 57
  • 58. Huffman Algorithm • You then repeat the loop, combining the two lowest elements. This results in: 22:* / 10:3 12:* / 5:1 7:2 • The two elements are removed from the list and the new parent node, with frequency 12, is inserted into the list by frequency. So now the list, sorted by frequency, is: 15:4 20:5 22: * 45:6 Ashim Lamichhane 58
  • 60. After sorted Ashim Lamichhane 60 Value Z K M C L D U E Frequency 2 7 24 32 42 42 37 120 Value C D E K L M U Z Frequency 32 42 120 7 42 24 37 2 Huffman Algorithm We can represent it using 3 bit
  • 61. Huffman Algorithm • Find code for • DEED • MUCK • Try to decode the bit string 1011001110111101 Ashim Lamichhane 61
  • 62. Assignments • Slides at myblog http://www.ashimlamichhane.com.np/2016/07/tree-slide-for-data- structure-and-algorithm/ • Assignments at github https://github.com/ashim888/dataStructureAndAlgorithm/tree/dev/As signments/assignment_7 Ashim Lamichhane 62
  • 63. Reference • https://www.siggraph.org/education/materials/HyperGraph/video/mp eg/mpegfaq/huffman_tutorial.html • https://en.wikipedia.org/wiki/Binary_search_tree • https://www.cs.swarthmore.edu/~newhall/unixhelp/Java_bst.pdf • https://www.cs.usfca.edu/~galles/visualization/BST.html • https://www.cs.rochester.edu/~gildea/csc282/slides/C12-bst.pdf • http://www.tutorialspoint.com/data_structures_algorithms/tree_data _structure.htm Ashim Lamichhane 63