SlideShare une entreprise Scribd logo
1  sur  76
Data Structures
Tree
Binary Tree
Binary Search Tree
Operations
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
SaadHaaris Qasim Asim Fahd Ahmad Sara Omer
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.
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.
Binary Tree
• Binary tree with 9 nodes.
A
B
D
H
C
E F
G I
Binary Tree
A
B
D
H
C
E F
G I
Left subtree
root
Right subtree
Binary Tree
• Recursive definition
A
B
D
H
C
E F
G ILeft subtree
root
Right subtree
Binary Tree
• Recursive definition
A
B
D
H
C
E F
G I
Left subtree
root
Binary Tree
• Recursive definition
A
B
D
H
C
E F
G I
root
Binary Tree
• Recursive definition
A
B
D
H
C
E F
G I
root
Right subtree
Binary Tree
• Recursive definition
A
B
D
H
C
E F
G I
root
Right subtreeLeft subtree
Not a Tree
• Structures that are not trees.
A
B
D
H
C
E F
G I
Not a Tree
• Structures that are not trees.
A
B
D
H
C
E F
G I
Not a Tree
• Structures that are not trees.
A
B
D
H
C
E F
G I
Binary Tree: Terminology
A
B
D
H
C
E F
G I
parent
Left descendant Right descendant
Leaf nodes Leaf nodes
Binary Tree
• If every non-leaf node in a binary tree has non-empty left
and right subtrees, the tree is termed a strictly binary
tree.
A
B
D
H
C
E F
G I
J
K
Level of a Binary Tree Node
• The level of a node in a binary tree is
defined as follows:
 Root has level 0,
 Level of any other node is one more than the
level its parent (father).
• The depth of a binary tree is the maximum
level of any leaf in the tree.
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
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 3L
F
M
2
3 3H
D
I
2
3 J
E
K
2
3
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
Complete Binary Tree
• At level k, there are 2k nodes.
• Total number of nodes in the tree of depth
d:
20+ 21+ 22 + ………. + 2d =  2j = 2d+1 – 1
• In a complete binary tree, there are 2d leaf
nodes and (2d - 1) non-leaf (inner) nodes.
j=0
d
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.
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’.
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
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
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.
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.
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.
Searching for Duplicates
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
Searching for Duplicates
15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
1415
Searching for Duplicates
15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
Searching for Duplicates
4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
15
4
Searching for Duplicates
4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
154
Searching for Duplicates
9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
154
9
Searching for Duplicates
9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
154
9
Searching for Duplicates
7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
154
9
7
Searching for Duplicates
7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
154
9
7
Searching for Duplicates
18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
154
9
7
18
Searching for Duplicates
18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14
154
9
7
18
Searching for Duplicates
3, 5, 16, 4, 20, 17, 9, 14, 5
14
154
9
7
18
3
Searching for Duplicates
3, 5, 16, 4, 20, 17, 9, 14, 5
14
154
9
7
183
Searching for Duplicates
5, 16, 4, 20, 17, 9, 14, 5
14
154
9
7
183
5
Searching for Duplicates
5, 16, 4, 20, 17, 9, 14, 5
14
154
9
7
183
5
Searching for Duplicates
16, 4, 20, 17, 9, 14, 5
14
154
9
7
183
5
16
Searching for Duplicates
16, 4, 20, 17, 9, 14, 5
14
154
9
7
183
5
16
Searching for Duplicates
4, 20, 17, 9, 14, 5
14
154
9
7
183
5
16
4
Searching for Duplicates
20, 17, 9, 14, 5
14
154
9
7
183
5
16
20
Searching for Duplicates
20, 17, 9, 14, 5
14
154
9
7
183
5
16 20
Searching for Duplicates
17, 9, 14, 5
14
154
9
7
183
5
16 20
17
Searching for Duplicates
17, 9, 14, 5
14
154
9
7
183
5
16 20
17
Searching for Duplicates
9, 14, 5
14
154
9
7
183
5
16 20
17
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.
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.
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?
Traversing a Binary Tree
• Ways to print a 3 node tree:
14
154
(4, 14, 15), (4,15,14)
(14,4,15), (14,15,4)
(15,4,14), (15,14,4)
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
subtr
ee
L
N
R
Traversing a Binary Tree
• Three common ways
node
Preorder: (N,L,R)
Inorder: (L,N,R)
Postorder: (L,R,N)
left
subtree
right
subtre
e
L
N
R
Traversing a Binary Tree
void preorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
cout << *(treeNode->getInfo())<<" ";
preorder(treeNode->getLeft());
preorder(treeNode->getRight());
}
}
Traversing a Binary Tree
void inorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
inorder(treeNode->getLeft());
cout << *(treeNode->getInfo())<<" ";
inorder(treeNode->getRight());
}
}
Traversing a Binary Tree
void postorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
postorder(treeNode->getLeft());
postorder(treeNode->getRight());
cout << *(treeNode->getInfo())<<" ";
}
}
Traversing a Binary Tree
cout << "inorder: "; preorder( root);
cout << "inorder: "; inorder( root );
cout << "postorder: "; postorder( root );
Traversing a Binary Tree
Preorder: 14 4 3 9 7 5 15 18 16 17 20
14
154
9
7
183
5
16 20
17
Traversing a Binary Tree
Inorder: 3 4 5 7 9 14 15 16 17 18 20
14
154
9
7
183
5
16 20
17
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?
Traversing a Binary Tree
Preorder: 14 4 3 9 7 5 15 18 16 17 20
14
154
9
7
183
5
16 20
17
Traversing a Binary Tree
Inorder: 3 4 5 7 9 14 15 16 17 18 20
14
154
9
7
183
5
16 20
17
Deleting a node in BST
• As is common with many data structures,
the hardest operation is deletion.
• Once we have found the node to be
deleted, we need to consider several
possibilities.
• If the node is a leaf, it can be deleted
immediately.
Deleting a node in BST
• If the node has one child, the node can be
deleted after its parent adjusts a pointer to
bypass the node and connect to inorder
successor.
6
2
4
3
1
8
Deleting a node in BST
• The inorder traversal order has to be
maintained after the delete.
6
2
4
3
1
8
6
2
4
3
1
8

Deleting a node in BST
• The inorder traversal order has to be
maintained after the delete.
6
2
4
3
1
8
6
2
4
3
1
8
6
2
31
8
 
Deleting a node in BST
• The complicated case is when the node to
be deleted has both left and right subtrees.
• The strategy is to replace the data of this
node with the smallest data of the right
subtree and recursively delete that node.
Deleting a node in BST
Delete(2): locate inorder successor
6
2
5
3
1
8
4
Inorder
successor
Deleting a node in BST
Delete(2): locate inorder successor
6
2
5
3
1
8
4
Inorder
successor
 Inorder successor will be the left-most
node in the right subtree of 2.
 The inorder successor will not have a left
child because if it did, that child would be
the left-most node.
Deleting a node in BST
Delete(2): copy data from inorder successor
6
2
5
3
1
8
4
 6
3
5
3
1
8
4
Deleting a node in BST
Delete(2): remove the inorder successor
6
2
5
3
1
8
4
 6
3
5
3
1
8
4
 6
3
5
3
1
8
4
Deleting a node in BST
Delete(2)
 6
3
5
4
1
8
 6
3
5
3
1
8
4

Contenu connexe

Tendances

Tendances (20)

Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
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
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search tree
 
binary search tree
binary search treebinary search tree
binary search tree
 
Trees
TreesTrees
Trees
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
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
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
binary tree
binary treebinary tree
binary tree
 
Binary tree
Binary tree Binary tree
Binary tree
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 

En vedette (6)

Traversals | Data Structures
Traversals | Data StructuresTraversals | Data Structures
Traversals | Data Structures
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 

Similaire à 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 treezia eagle
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
Lecture 09 - Binary Search Trees.pptx mission Sk it
Lecture 09 - Binary Search Trees.pptx mission Sk itLecture 09 - Binary Search Trees.pptx mission Sk it
Lecture 09 - Binary Search Trees.pptx mission Sk itAmazingWorld37
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeINAM352782
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptxRaaviKapoor
 
Binary trees
Binary treesBinary trees
Binary treesAmit Vats
 
4a searching-more
4a searching-more4a searching-more
4a searching-moreShahzad Ali
 
Binary Search Tree Traversal.ppt
Binary Search Tree Traversal.pptBinary Search Tree Traversal.ppt
Binary Search Tree Traversal.pptRiannel Tecson
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana Shaikh
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREESTABISH HAMID
 

Similaire à Week 8 (trees) (20)

6_1 (1).ppt
6_1 (1).ppt6_1 (1).ppt
6_1 (1).ppt
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
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-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Lecture 09 - Binary Search Trees.pptx mission Sk it
Lecture 09 - Binary Search Trees.pptx mission Sk itLecture 09 - Binary Search Trees.pptx mission Sk it
Lecture 09 - Binary Search Trees.pptx mission Sk it
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
DAA PPT.pptx
DAA PPT.pptxDAA PPT.pptx
DAA PPT.pptx
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
 
Lecture10
Lecture10Lecture10
Lecture10
 
Binary Search Tree Traversal.ppt
Binary Search Tree Traversal.pptBinary Search Tree Traversal.ppt
Binary Search Tree Traversal.ppt
 
Binary tree
Binary treeBinary tree
Binary tree
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 

Plus de amna izzat

Plus de amna izzat (6)

1 introduction ddbms
1 introduction ddbms1 introduction ddbms
1 introduction ddbms
 
Byte division
Byte divisionByte division
Byte division
 
Avl trees
Avl treesAvl trees
Avl trees
 
C++basics
C++basicsC++basics
C++basics
 
Selection sort
Selection sortSelection sort
Selection sort
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 

Dernier

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
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
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Dernier (20)

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
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...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Week 8 (trees)

  • 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 SaadHaaris Qasim Asim Fahd Ahmad Sara Omer
  • 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 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.
  • 5. Binary Tree • Binary tree with 9 nodes. A B D H C E F G I
  • 6. Binary Tree A B D H C E F G I Left subtree root Right subtree
  • 7. Binary Tree • Recursive definition A B D H C E F G ILeft subtree root Right subtree
  • 8. Binary Tree • Recursive definition A B D H C E F G I Left subtree root
  • 9. Binary Tree • Recursive definition A B D H C E F G I root
  • 10. Binary Tree • Recursive definition A B D H C E F G I root Right subtree
  • 11. Binary Tree • Recursive definition A B D H C E F G I root Right subtreeLeft subtree
  • 12. Not a Tree • Structures that are not trees. A B D H C E F G I
  • 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. Binary Tree: Terminology A B D H C E F G I parent Left descendant Right descendant Leaf nodes Leaf nodes
  • 16. Binary Tree • If every non-leaf node in a binary tree has non-empty left and right subtrees, the tree is termed a strictly binary tree. A B D H C E F G I J K
  • 17. Level of a Binary Tree Node • The level of a node in a binary tree is defined as follows:  Root has level 0,  Level of any other node is one more than the level its parent (father). • The depth of a binary tree is the maximum level of any leaf in the tree.
  • 18. 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
  • 19. 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 3L F M 2 3 3H D I 2 3 J E K 2 3
  • 20. 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
  • 21. Complete Binary Tree • At level k, there are 2k nodes. • Total number of nodes in the tree of depth d: 20+ 21+ 22 + ………. + 2d =  2j = 2d+1 – 1 • In a complete binary tree, there are 2d leaf nodes and (2d - 1) non-leaf (inner) nodes. j=0 d
  • 22. 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.
  • 23. 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’.
  • 24. 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
  • 25. 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
  • 26. 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.
  • 27. 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.
  • 28. 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.
  • 29. Searching for Duplicates 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14
  • 30. Searching for Duplicates 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 1415
  • 31. Searching for Duplicates 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15
  • 32. Searching for Duplicates 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4
  • 33. Searching for Duplicates 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 154
  • 34. Searching for Duplicates 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 154 9
  • 35. Searching for Duplicates 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 154 9
  • 36. Searching for Duplicates 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 154 9 7
  • 37. Searching for Duplicates 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 154 9 7
  • 38. Searching for Duplicates 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 154 9 7 18
  • 39. Searching for Duplicates 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 154 9 7 18
  • 40. Searching for Duplicates 3, 5, 16, 4, 20, 17, 9, 14, 5 14 154 9 7 18 3
  • 41. Searching for Duplicates 3, 5, 16, 4, 20, 17, 9, 14, 5 14 154 9 7 183
  • 42. Searching for Duplicates 5, 16, 4, 20, 17, 9, 14, 5 14 154 9 7 183 5
  • 43. Searching for Duplicates 5, 16, 4, 20, 17, 9, 14, 5 14 154 9 7 183 5
  • 44. Searching for Duplicates 16, 4, 20, 17, 9, 14, 5 14 154 9 7 183 5 16
  • 45. Searching for Duplicates 16, 4, 20, 17, 9, 14, 5 14 154 9 7 183 5 16
  • 46. Searching for Duplicates 4, 20, 17, 9, 14, 5 14 154 9 7 183 5 16 4
  • 47. Searching for Duplicates 20, 17, 9, 14, 5 14 154 9 7 183 5 16 20
  • 48. Searching for Duplicates 20, 17, 9, 14, 5 14 154 9 7 183 5 16 20
  • 49. Searching for Duplicates 17, 9, 14, 5 14 154 9 7 183 5 16 20 17
  • 50. Searching for Duplicates 17, 9, 14, 5 14 154 9 7 183 5 16 20 17
  • 51. Searching for Duplicates 9, 14, 5 14 154 9 7 183 5 16 20 17
  • 52. 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.
  • 53. 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.
  • 54. 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?
  • 55. Traversing a Binary Tree • Ways to print a 3 node tree: 14 154 (4, 14, 15), (4,15,14) (14,4,15), (14,15,4) (15,4,14), (15,14,4)
  • 56. 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 subtr ee L N R
  • 57. Traversing a Binary Tree • Three common ways node Preorder: (N,L,R) Inorder: (L,N,R) Postorder: (L,R,N) left subtree right subtre e L N R
  • 58. Traversing a Binary Tree void preorder(TreeNode<int>* treeNode) { if( treeNode != NULL ) { cout << *(treeNode->getInfo())<<" "; preorder(treeNode->getLeft()); preorder(treeNode->getRight()); } }
  • 59. Traversing a Binary Tree void inorder(TreeNode<int>* treeNode) { if( treeNode != NULL ) { inorder(treeNode->getLeft()); cout << *(treeNode->getInfo())<<" "; inorder(treeNode->getRight()); } }
  • 60. Traversing a Binary Tree void postorder(TreeNode<int>* treeNode) { if( treeNode != NULL ) { postorder(treeNode->getLeft()); postorder(treeNode->getRight()); cout << *(treeNode->getInfo())<<" "; } }
  • 61. Traversing a Binary Tree cout << "inorder: "; preorder( root); cout << "inorder: "; inorder( root ); cout << "postorder: "; postorder( root );
  • 62. Traversing a Binary Tree Preorder: 14 4 3 9 7 5 15 18 16 17 20 14 154 9 7 183 5 16 20 17
  • 63. Traversing a Binary Tree Inorder: 3 4 5 7 9 14 15 16 17 18 20 14 154 9 7 183 5 16 20 17
  • 64. 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?
  • 65. Traversing a Binary Tree Preorder: 14 4 3 9 7 5 15 18 16 17 20 14 154 9 7 183 5 16 20 17
  • 66. Traversing a Binary Tree Inorder: 3 4 5 7 9 14 15 16 17 18 20 14 154 9 7 183 5 16 20 17
  • 67. Deleting a node in BST • As is common with many data structures, the hardest operation is deletion. • Once we have found the node to be deleted, we need to consider several possibilities. • If the node is a leaf, it can be deleted immediately.
  • 68. Deleting a node in BST • If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node and connect to inorder successor. 6 2 4 3 1 8
  • 69. Deleting a node in BST • The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8 
  • 70. Deleting a node in BST • The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8 6 2 31 8  
  • 71. Deleting a node in BST • The complicated case is when the node to be deleted has both left and right subtrees. • The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node.
  • 72. Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder successor
  • 73. Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder successor  Inorder successor will be the left-most node in the right subtree of 2.  The inorder successor will not have a left child because if it did, that child would be the left-most node.
  • 74. Deleting a node in BST Delete(2): copy data from inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4
  • 75. Deleting a node in BST Delete(2): remove the inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4  6 3 5 3 1 8 4
  • 76. Deleting a node in BST Delete(2)  6 3 5 4 1 8  6 3 5 3 1 8 4