SlideShare a Scribd company logo
1 of 76
Download to read offline
Data Structures & Algorithm
CSC-102
Lecture 5
Binary Trees Traversing
Lecturer: Syeda Nazia Ashraf
1
Tree Data Structures
• There are a number of applications where
linear data structures are not appropriate.
• Consider a genealogy tree of a family.
Mohammad Aslam Khan
Sohail Aslam Javed Aslam Yasmeen Aslam
Saad
Haaris Qasim Asim Fahd Ahmad Sara Omer
2
3
Tree Data Structure
• A linear linked list will not be able to
capture the tree-like relationship with
ease.
• Shortly, we will see that for applications
that require searching, linear data
structures are not suitable.
• We will focus our attention on binary trees.
4
Binary Tree
• A tree in which every node can have a maximum of
two children is called as Binary Tree.
• In a binary tree, every node can have either 0
children or 1 child or 2 children but not more than 2
children. A
B
D
H
C
E F
G I
J
K
5
Binary Tree
• A binary tree is a finite set of elements
that is 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
subtrees.
• Each element of a binary tree is called a
node of the tree.
6
Binary Tree
• Binary tree with 9 nodes.
A
B
D
H
C
E F
G I
7
Binary Tree
A
B
D
H
C
E F
G I
Left subtree
root
Right subtree
8
Binary Tree
• Recursive definition
A
B
D
H
C
E F
G I
Left subtree
root
Right subtree
9
Binary Tree
• Recursive definition
A
B
D
H
C
E F
G I
Left subtree
root
10
Binary Tree
• Recursive definition
A
B
D
H
C
E F
G I
root
11
Binary Tree
• Recursive definition
A
B
D
H
C
E F
G I
root
Right subtree
12
Binary Tree
• Recursive definition
A
B
D
H
C
E F
G I
root
Right subtree
Left subtree
13
Not a Tree
• Structures that are not trees.
A
B
D
H
C
E F
G I
14
Not a Tree
• Structures that are not trees.
A
B
D
H
C
E F
G I
15
Not a Tree
• Structures that are not trees.
A
B
D
H
C
E F
G I
16
Binary Tree: Terminology
A
B
D
H
C
E F
G I
parent
Left descendant Right descendant
Leaf nodes Leaf nodes
17
Height of a Binary Tree Node
▪ The height of a node is the number of edges from the longest
downward path between the node and the deepest leaf.
▪ The height of a tree is a height of the root.
▪ Leaf cannot have height as there will be no path starting from
a leaf.
18
It is the longest path from the node to a leaf. So A's height is the number
of edges of the path to E, NOT to G. And its height is 3.
19
• The depth of a node is the number of edges from the node to
the root.
• The depth of a binary tree is the maximum level of any
leaf in the tree. i.e. Depth of tree = depth of deepest node
We don't care about path any more
when depth pops in. We just count how
many edges between the targeting node
and the root, ignoring directions.
For example, D's depth is 2. Recall that
when talking about height, we actually
imply a baseline located at bottom.
For depth, the baseline is at top which is
root level. That's why we call it depth.
Note that the depth of the root is 0.
Depth of a Binary Tree Node
20
• The level of a node is defined by 1+ (the number of
connections between the node and the root).
Level of a Binary Tree Node
Simply, level is depth plus 1.
The important thing to remember is when talking about level, it starts from
1 and the level of the root is 1.
We need to be careful about this when solving problems related to level
Level of a Binary Tree Node
A
B
D
H
C
E F
G I
1
0
1
2 2 2
3 3 3
Level 0
Level 1
Level 2
Level 3
21
Binary Tree
22
23
• A Strictly Binary Tree or Full Binary Tree or Proper Binary Tree or 2
Tree is a binary tree in which each node has exactly zero or two
children.
Strictly Binary Tree
• Strictly binary tree data structure is used to represent mathematical
expressions
24
Complete Binary Tree
• A complete binary tree is a binary tree, which is completely
filled, with the possible exception of the bottom level, which
is filled from left to right.
• Complete binary tree is also called as Perfect Binary Tree
Complete Binary Tree
• A complete binary tree of depth d is the
strictly binary all of whose leaves are at
level d.
A
B
N
C
G
O
1
0
1
2
3 3
L
F
M
2
3 3
H
D
I
2
3 J
E
K
2
3
25
Complete Binary Tree
A
B
Level 0: 20 nodes
H
D
I
E
J K
C
L
F
M
G
N O
Level 1: 21 nodes
Level 2: 22 nodes
Level 3: 23 nodes
26
In complete binary tree all the nodes must have exactly two
children and at every level of complete binary tree there must
be 2level number of nodes. For example at level 2 there must be
22 = 4 nodes and at level 3 there must be 23 = 8 nodes.
27
An extended binary tree is a transformation of any binary tree into
a complete binary tree by replacing each empty sub-tree by a
special node (dummy node). The nodes from the original tree are
then internal nodes, while the “special nodes” are external nodes.
In above figure, a normal binary tree is converted into full
binary tree by adding dummy nodes (In pink color).
Extended Binary Tree
Operations on Binary Tree
• There are a number of operations that can
be defined for a binary tree.
• If p is pointing to a node in an existing tree
then
▪ left(p) returns pointer to the left subtree
▪ right(p) returns pointer to right subtree
▪ parent(p) returns the father of p
▪ brother(p) returns brother of p.
▪ info(p) returns content of the node.
28
Operations on Binary Tree
• In order to construct a binary tree, the
following can be useful:
• setLeft(p,x) creates the left child node of p.
The child node contains the info ‘x’.
• setRight(p,x) creates the right child node
of p. The child node contains the info ‘x’.
29
Binary Search Tree
• A binary tree with the property: that items
in the left subtree are smaller than the root
and items are larger or equal in the right
subtree is called a binary search tree
(BST).
• The tree we built for searching for
duplicate numbers was a binary search
tree.
• BST and its variations play an important
role in searching algorithms.
30
Applications of Binary Trees
• A binary tree is a useful data structure
when two-way decisions must be made at
each point in a process.
• For example, suppose we wanted to find
all duplicates in a list of numbers:
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
31
Applications of Binary Trees
• One way of finding duplicates is to
compare each number with all those that
precede it.
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
32
Searching for Duplicates
• If the list of numbers is large and is
growing, this procedure involves a large
number of comparisons.
• A linked list could handle the growth but
the comparisons would still be large.
• The number of comparisons can be
drastically reduced by using a binary tree.
• The tree grows dynamically like the linked
list.
33
Searching for Duplicates
• The binary tree is built in a special way.
• The first number in the list is placed in a
node that is designated as the root of a
binary tree.
• Initially, both left and right subtrees of the
root are empty.
• We take the next number and compare it
with the number placed in the root.
• If it is the same then we have a duplicate.
34
Searching for Duplicates
• Otherwise, we create a new tree node and
put the new number in it.
• The new node is made the left child of the
root node if the second number is less
than the one in the root.
• The new node is made the right child if the
number is greater than the one in the root.
35
Searching for Duplicates
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
36
Searching for Duplicates
15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
37
Searching for Duplicates
15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
38
Searching for Duplicates
4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
39
Searching for Duplicates
4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
40
Searching for Duplicates
9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
9
41
Searching for Duplicates
9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
9
42
Searching for Duplicates
7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
9
7
43
Searching for Duplicates
7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
9
7
44
Searching for Duplicates
18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
9
7
18
45
Searching for Duplicates
18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
9
7
18
46
Searching for Duplicates
3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
9
7
18
3
47
Searching for Duplicates
3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
9
7
18
3
48
Searching for Duplicates
5, 16, 4, 20, 17, 9, 14, 5
14
15
4
9
7
18
3
5
49
Searching for Duplicates
5, 16, 4, 20, 17, 9, 14, 5
14
15
4
9
7
18
3
5
50
Searching for Duplicates
16, 4, 20, 17, 9, 14, 5
14
15
4
9
7
18
3
5
16
51
Searching for Duplicates
16, 4, 20, 17, 9, 14, 5
14
15
4
9
7
18
3
5
16
52
Searching for Duplicates
4, 20, 17, 9, 14, 5
14
15
4
9
7
18
3
5
16
4
53
Searching for Duplicates
20, 17, 9, 14, 5
14
15
4
9
7
18
3
5
16
20
54
Searching for Duplicates
20, 17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
55
Searching for Duplicates
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
56
Searching for Duplicates
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
57
Searching for Duplicates
9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
58
59
Exercise. Given a sequence of numbers:
11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
Draw a binary search tree by inserting the above numbers
from left to right.
60
Trace of insert
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
p
q
61
Trace of insert
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
p
q
62
Trace of insert
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
p
q
63
Trace of insert
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
p
q
64
Trace of insert
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
p
q
65
Trace of insert
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
p
q
66
Trace of insert
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
p
q
67
Trace of insert
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
p
q
68
Trace of insert
17, 9, 14, 5
14
15
4
9
7
18
3
5
16 20
17
p
p->setRight( node );
node
69
Cost of Search
• If the binary tree is built out of n numbers,
how many comparisons are needed to find
out if a number x is in the tree?
• The depth of the complete binary tree built
using ‘n’ nodes will be log2(n+1) – 1.
• For example, for n=100,000, log2(100001)
is less than 20; the tree would be 20 levels
deep.
70
Cost of Search
• If the tree is complete binary or nearly
complete, searching through 100,000
numbers will require a maximum of 20
comparisons.
• Or in general, approximately log2(n).
• Compare this with a linked list of 100,000
numbers. The comparisons required could
be a maximum of n.
71
Traversing a Binary Tree
• Suppose we have a binary tree, ordered
(BST) or unordered.
• We want to print all the values stored in
the nodes of the tree.
• In what order should we print them?
72
Traversing a Binary Tree
• Ways to print a 3 node tree:
14
15
4
(4, 14, 15), (4,15,14)
(14,4,15), (14,15,4)
(15,4,14), (15,14,4)
73
Traversing a Binary Tree
• In case of the general binary tree:
node
(L,N,R), (L,R,N)
(N,L,R), (N,R,L)
(R,L,N), (R,N,L)
left
subtree
right
subtree
L
N
R
74
Traversing a Binary Tree
• Three common ways
node
Preorder: (N,L,R)
Inorder: (L,N,R)
Postorder: (L,R,N)
left
subtree
right
subtree
L
N
R
75
76

More Related Content

What's hot (20)

B trees dbms
B trees dbmsB trees dbms
B trees dbms
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Tree
TreeTree
Tree
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
B+ tree
B+ treeB+ tree
B+ tree
 
Nikhat b+ trees ppt
Nikhat b+ trees pptNikhat b+ trees ppt
Nikhat b+ trees ppt
 
B-Tree
B-TreeB-Tree
B-Tree
 
b+ tree
b+ treeb+ tree
b+ tree
 
B+tree Data structures presentation
B+tree Data structures presentationB+tree Data structures presentation
B+tree Data structures presentation
 
B tree
B treeB tree
B tree
 
Trees
TreesTrees
Trees
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Best for b trees
Best for b treesBest for b trees
Best for b trees
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 

Similar to LEC 5-DS ALGO(updated).pdf

Similar to LEC 5-DS ALGO(updated).pdf (20)

6_1 (1).ppt
6_1 (1).ppt6_1 (1).ppt
6_1 (1).ppt
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Tree
TreeTree
Tree
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Binary tree
Binary tree Binary tree
Binary tree
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Tree
TreeTree
Tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
BTrees-fall2010.ppt
BTrees-fall2010.pptBTrees-fall2010.ppt
BTrees-fall2010.ppt
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 

More from MuhammadUmerIhtisham

Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfMuhammadUmerIhtisham
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdfMuhammadUmerIhtisham
 
SMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfSMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfMuhammadUmerIhtisham
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfMuhammadUmerIhtisham
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfMuhammadUmerIhtisham
 
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfSMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfMuhammadUmerIhtisham
 

More from MuhammadUmerIhtisham (14)

LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdf
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
 
LEC 8-DS ALGO(heaps).pdf
LEC 8-DS  ALGO(heaps).pdfLEC 8-DS  ALGO(heaps).pdf
LEC 8-DS ALGO(heaps).pdf
 
LEC 6-DS ALGO(updated).pdf
LEC 6-DS  ALGO(updated).pdfLEC 6-DS  ALGO(updated).pdf
LEC 6-DS ALGO(updated).pdf
 
lect 1-ds algo(final)_2.pdf
lect 1-ds  algo(final)_2.pdflect 1-ds  algo(final)_2.pdf
lect 1-ds algo(final)_2.pdf
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
 
SMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfSMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdf
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdf
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdf
 
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfSMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
 

Recently uploaded

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

LEC 5-DS ALGO(updated).pdf

  • 1. Data Structures & Algorithm CSC-102 Lecture 5 Binary Trees Traversing Lecturer: Syeda Nazia Ashraf 1
  • 2. Tree Data Structures • There are a number of applications where linear data structures are not appropriate. • Consider a genealogy tree of a family. Mohammad Aslam Khan Sohail Aslam Javed Aslam Yasmeen Aslam Saad Haaris Qasim Asim Fahd Ahmad Sara Omer 2
  • 3. 3
  • 4. Tree Data Structure • A linear linked list will not be able to capture the tree-like relationship with ease. • Shortly, we will see that for applications that require searching, linear data structures are not suitable. • We will focus our attention on binary trees. 4
  • 5. Binary Tree • A tree in which every node can have a maximum of two children is called as Binary Tree. • In a binary tree, every node can have either 0 children or 1 child or 2 children but not more than 2 children. A B D H C E F G I J K 5
  • 6. Binary Tree • A binary tree is a finite set of elements that is 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 subtrees. • Each element of a binary tree is called a node of the tree. 6
  • 7. Binary Tree • Binary tree with 9 nodes. A B D H C E F G I 7
  • 8. Binary Tree A B D H C E F G I Left subtree root Right subtree 8
  • 9. Binary Tree • Recursive definition A B D H C E F G I Left subtree root Right subtree 9
  • 10. Binary Tree • Recursive definition A B D H C E F G I Left subtree root 10
  • 11. Binary Tree • Recursive definition A B D H C E F G I root 11
  • 12. Binary Tree • Recursive definition A B D H C E F G I root Right subtree 12
  • 13. Binary Tree • Recursive definition A B D H C E F G I root Right subtree Left subtree 13
  • 14. Not a Tree • Structures that are not trees. A B D H C E F G I 14
  • 15. Not a Tree • Structures that are not trees. A B D H C E F G I 15
  • 16. Not a Tree • Structures that are not trees. A B D H C E F G I 16
  • 17. Binary Tree: Terminology A B D H C E F G I parent Left descendant Right descendant Leaf nodes Leaf nodes 17
  • 18. Height of a Binary Tree Node ▪ The height of a node is the number of edges from the longest downward path between the node and the deepest leaf. ▪ The height of a tree is a height of the root. ▪ Leaf cannot have height as there will be no path starting from a leaf. 18 It is the longest path from the node to a leaf. So A's height is the number of edges of the path to E, NOT to G. And its height is 3.
  • 19. 19 • The depth of a node is the number of edges from the node to the root. • The depth of a binary tree is the maximum level of any leaf in the tree. i.e. Depth of tree = depth of deepest node We don't care about path any more when depth pops in. We just count how many edges between the targeting node and the root, ignoring directions. For example, D's depth is 2. Recall that when talking about height, we actually imply a baseline located at bottom. For depth, the baseline is at top which is root level. That's why we call it depth. Note that the depth of the root is 0. Depth of a Binary Tree Node
  • 20. 20 • The level of a node is defined by 1+ (the number of connections between the node and the root). Level of a Binary Tree Node Simply, level is depth plus 1. The important thing to remember is when talking about level, it starts from 1 and the level of the root is 1. We need to be careful about this when solving problems related to level
  • 21. Level of a Binary Tree Node A B D H C E F G I 1 0 1 2 2 2 3 3 3 Level 0 Level 1 Level 2 Level 3 21
  • 23. 23 • A Strictly Binary Tree or Full Binary Tree or Proper Binary Tree or 2 Tree is a binary tree in which each node has exactly zero or two children. Strictly Binary Tree • Strictly binary tree data structure is used to represent mathematical expressions
  • 24. 24 Complete Binary Tree • A complete binary tree is a binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right. • Complete binary tree is also called as Perfect Binary Tree
  • 25. Complete Binary Tree • A complete binary tree of depth d is the strictly binary all of whose leaves are at level d. A B N C G O 1 0 1 2 3 3 L F M 2 3 3 H D I 2 3 J E K 2 3 25
  • 26. Complete Binary Tree A B Level 0: 20 nodes H D I E J K C L F M G N O Level 1: 21 nodes Level 2: 22 nodes Level 3: 23 nodes 26 In complete binary tree all the nodes must have exactly two children and at every level of complete binary tree there must be 2level number of nodes. For example at level 2 there must be 22 = 4 nodes and at level 3 there must be 23 = 8 nodes.
  • 27. 27 An extended binary tree is a transformation of any binary tree into a complete binary tree by replacing each empty sub-tree by a special node (dummy node). The nodes from the original tree are then internal nodes, while the “special nodes” are external nodes. In above figure, a normal binary tree is converted into full binary tree by adding dummy nodes (In pink color). Extended Binary Tree
  • 28. Operations on Binary Tree • There are a number of operations that can be defined for a binary tree. • If p is pointing to a node in an existing tree then ▪ left(p) returns pointer to the left subtree ▪ right(p) returns pointer to right subtree ▪ parent(p) returns the father of p ▪ brother(p) returns brother of p. ▪ info(p) returns content of the node. 28
  • 29. Operations on Binary Tree • In order to construct a binary tree, the following can be useful: • setLeft(p,x) creates the left child node of p. The child node contains the info ‘x’. • setRight(p,x) creates the right child node of p. The child node contains the info ‘x’. 29
  • 30. Binary Search Tree • A binary tree with the property: that items in the left subtree are smaller than the root and items are larger or equal in the right subtree is called a binary search tree (BST). • The tree we built for searching for duplicate numbers was a binary search tree. • BST and its variations play an important role in searching algorithms. 30
  • 31. Applications of Binary Trees • A binary tree is a useful data structure when two-way decisions must be made at each point in a process. • For example, suppose we wanted to find all duplicates in a list of numbers: 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 31
  • 32. Applications of Binary Trees • One way of finding duplicates is to compare each number with all those that precede it. 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 32
  • 33. Searching for Duplicates • If the list of numbers is large and is growing, this procedure involves a large number of comparisons. • A linked list could handle the growth but the comparisons would still be large. • The number of comparisons can be drastically reduced by using a binary tree. • The tree grows dynamically like the linked list. 33
  • 34. Searching for Duplicates • The binary tree is built in a special way. • The first number in the list is placed in a node that is designated as the root of a binary tree. • Initially, both left and right subtrees of the root are empty. • We take the next number and compare it with the number placed in the root. • If it is the same then we have a duplicate. 34
  • 35. Searching for Duplicates • Otherwise, we create a new tree node and put the new number in it. • The new node is made the left child of the root node if the second number is less than the one in the root. • The new node is made the right child if the number is greater than the one in the root. 35
  • 36. Searching for Duplicates 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 36
  • 37. Searching for Duplicates 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 37
  • 38. Searching for Duplicates 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 38
  • 39. Searching for Duplicates 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 39
  • 40. Searching for Duplicates 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 40
  • 41. Searching for Duplicates 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 41
  • 42. Searching for Duplicates 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 42
  • 43. Searching for Duplicates 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 43
  • 44. Searching for Duplicates 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 44
  • 45. Searching for Duplicates 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 18 45
  • 46. Searching for Duplicates 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 18 46
  • 47. Searching for Duplicates 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 18 3 47
  • 48. Searching for Duplicates 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 18 3 48
  • 49. Searching for Duplicates 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 18 3 5 49
  • 50. Searching for Duplicates 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 18 3 5 50
  • 51. Searching for Duplicates 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 51
  • 52. Searching for Duplicates 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 52
  • 53. Searching for Duplicates 4, 20, 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 4 53
  • 54. Searching for Duplicates 20, 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 54
  • 55. Searching for Duplicates 20, 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 55
  • 56. Searching for Duplicates 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 56
  • 57. Searching for Duplicates 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 57
  • 58. Searching for Duplicates 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 58
  • 59. 59 Exercise. Given a sequence of numbers: 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 Draw a binary search tree by inserting the above numbers from left to right.
  • 60. 60
  • 61. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q 61
  • 62. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q 62
  • 63. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q 63
  • 64. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q 64
  • 65. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q 65
  • 66. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q 66
  • 67. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q 67
  • 68. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q 68
  • 69. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p p->setRight( node ); node 69
  • 70. Cost of Search • If the binary tree is built out of n numbers, how many comparisons are needed to find out if a number x is in the tree? • The depth of the complete binary tree built using ‘n’ nodes will be log2(n+1) – 1. • For example, for n=100,000, log2(100001) is less than 20; the tree would be 20 levels deep. 70
  • 71. Cost of Search • If the tree is complete binary or nearly complete, searching through 100,000 numbers will require a maximum of 20 comparisons. • Or in general, approximately log2(n). • Compare this with a linked list of 100,000 numbers. The comparisons required could be a maximum of n. 71
  • 72. Traversing a Binary Tree • Suppose we have a binary tree, ordered (BST) or unordered. • We want to print all the values stored in the nodes of the tree. • In what order should we print them? 72
  • 73. Traversing a Binary Tree • Ways to print a 3 node tree: 14 15 4 (4, 14, 15), (4,15,14) (14,4,15), (14,15,4) (15,4,14), (15,14,4) 73
  • 74. Traversing a Binary Tree • In case of the general binary tree: node (L,N,R), (L,R,N) (N,L,R), (N,R,L) (R,L,N), (R,N,L) left subtree right subtree L N R 74
  • 75. Traversing a Binary Tree • Three common ways node Preorder: (N,L,R) Inorder: (L,N,R) Postorder: (L,R,N) left subtree right subtree L N R 75
  • 76. 76