SlideShare a Scribd company logo
Trees 
(Data Structure) 
Trupti agrawal 1
Trees Data Structures 
 Tree 
 Nodes 
 Each node can have 0 or more children 
 A node can have at most one parent 
 Binary tree 
 Tree with 0–2 children per node 
Tree Binary Tree Trupti agrawal 2
Trees 
 Terminology 
 Root  no parent 
 Leaf  no child 
 Interior  non-leaf 
 Height  distance from root to leaf 
Root node 
Interior nodes Height 
Leaf nodes 
Trupti agrawal 3
Binary Search Trees 
 Key property 
 Value at node 
 Smaller values in left subtree 
 Larger values in right subtree 
 Example 
 X > Y 
 X < Z 
Y 
X 
Z 
Trupti agrawal 4
Binary Search Trees 
 Examples 
Binary 
search trees 
Not a binary 
search tree 
5 
10 
30 
2 25 45 
5 
10 
45 
2 25 30 
5 
10 
30 
2 
25 
45 
Trupti agrawal 5
Binary Tree Implementation 
Class Node { 
int data; // Could be int, a class, etc 
Node *left, *right; // null if empty 
void insert ( int data ) { … } 
void delete ( int data ) { … } 
Node *find ( int data ) { … } 
… 
} 
Trupti agrawal 6
Iterative Search of Binary Tree 
Node *Find( Node *n, int key) { 
while (n != NULL) { 
if (n->data == key) // Found it 
return n; 
if (n->data > key) // In left subtree 
n = n->left; 
else // In right subtree 
n = n->right; 
} 
return null; 
} 
Node * n = Find( root, 5); 
Trupti agrawal 7
Recursive Search of Binary Tree 
Node *Find( Node *n, int key) { 
if (n == NULL) // Not found 
return( n ); 
else if (n->data == key) // Found it 
return( n ); 
else if (n->data > key) // In left subtree 
return Find( n->left, key ); 
else // In right subtree 
return Find( n->right, key ); 
} 
Node * n = Find( root, 5); 
Trupti agrawal 8
Example Binary Searches 
 Find ( root, 2 ) 
5 
10 
30 
2 25 45 
5 
10 
30 
2 
25 
45 
10 > 2, left 
5 > 2, left 
2 = 2, found 
5 > 2, left 
2 = 2, found 
root 
Trupti agrawal 9
Example Binary Searches 
 Find (root, 25 ) 
5 
10 
30 
2 25 45 
5 
10 
30 
2 
25 
45 
10 < 25, right 
30 > 25, left 
25 = 25, found 
5 < 25, right 
45 > 25, left 
30 > 25, left 
10 < 25, right 
25 = 25, found 
Trupti agrawal 10
Types of Binary Trees 
 Degenerate – only one child 
 Complete – always two children 
 Balanced – “mostly” two children 
 more formal definitions exist, above are intuitive ideas 
Degenerate 
binary tree 
Balanced 
binary tree 
Complete 
binary tree 
Trupti agrawal 11
Binary Trees Properties 
 Degenerate 
 Height = O(n) for n 
nodes 
 Similar to linked list 
 Balanced 
 Height = O( log(n) ) 
for n nodes 
 Useful for searches 
Degenerate 
binary tree 
Balanced 
binary tree 
Trupti agrawal 12
Binary Search Properties 
 Time of search 
 Proportional to height of tree 
 Balanced binary tree 
 O( log(n) ) time 
 Degenerate tree 
 O( n ) time 
 Like searching linked list / unsorted array 
Trupti agrawal 13
Binary Search Tree Construction 
 How to build & maintain binary trees? 
 Insertion 
 Deletion 
 Maintain key property (invariant) 
 Smaller values in left subtree 
 Larger values in right subtree 
Trupti agrawal 14
Binary Search Tree – Insertion 
 Algorithm 
1. Perform search for value X 
2. Search will end at node Y (if X not in tree) 
3. If X < Y, insert new leaf X as new left subtree for Y 
4. If X > Y, insert new leaf X as new right subtree for Y 
 Observations 
 O( log(n) ) operation for balanced tree 
 Insertions may unbalance tree 
Trupti agrawal 15
Example Insertion 
 Insert ( 20 ) 
5 
10 
30 
2 25 45 
10 < 20, right 
30 > 20, left 
25 > 20, left 
Insert 20 on left 
20 
Trupti agrawal 16
Binary Search Tree – Deletion 
 Algorithm 
1. Perform search for value X 
2. If X is a leaf, delete X 
3. Else // must delete internal node 
a) Replace with largest value Y on left subtree 
OR smallest value Z on right subtree 
b) Delete replacement value (Y or Z) from subtree 
Observation 
 O( log(n) ) operation for balanced tree 
 Deletions may unbalance tree 
Trupti agrawal 17
Example Deletion (Leaf) 
 Delete ( 25 ) 
5 
10 
30 
2 25 45 
10 < 25, right 
30 > 25, left 
25 = 25, delete 
5 
10 
30 
2 45 
Trupti agrawal 18
Example Deletion (Internal Node) 
 Delete ( 10 ) 
5 
10 
30 
2 25 45 
5 
5 
30 
2 25 45 
2 
5 
30 
2 25 45 
Replacing 10 
with largest 
value in left 
subtree 
Replacing 5 
with largest 
value in left 
subtree 
Deleting leaf 
Trupti agrawal 19
Example Deletion (Internal Node) 
 Delete ( 10 ) 
5 
10 
30 
2 25 45 
5 
25 
30 
2 25 45 
5 
25 
30 
2 45 
Replacing 10 
with smallest 
value in right 
subtree 
Deleting leaf Resulting tree 
Trupti agrawal 20
Balanced Search Trees 
 Kinds of balanced binary search trees 
 height balanced vs. weight balanced 
 “Tree rotations” used to maintain balance on insert/delete 
 Non-binary search trees 
 2/3 trees 
 each internal node has 2 or 3 children 
 all leaves at same depth (height balanced) 
 B-trees 
 Generalization of 2/3 trees 
 Each internal node has between k/2 and k children 
 Each node has an array of pointers to children 
 Widely used in databases 
Trupti agrawal 21
Other (Non-Search) Trees 
 Parse trees 
 Convert from textual representation to tree 
representation 
 Textual program to tree 
 Used extensively in compilers 
 Tree representation of data 
 E.g. HTML data can be represented as a tree 
 called DOM (Document Object Model) tree 
 XML 
 Like HTML, but used to represent data 
 Tree structured 
Trupti agrawal 22
Parse Trees 
 Expressions, programs, etc can be represented by tree 
structures 
 E.g. Arithmetic Expression Tree 
 A-(C/5 * 2) + (D*5 % 4) 
+ 
- % 
A * * 4 
/ 2 D 5 
C 5 
Trupti agrawal 23
Tree Traversal 
 Goal: visit every node of a tree 
 in-order traversal 
void Node::inOrder () { 
if (left != NULL) { 
cout << “(“; left->inOrder(); cout << “)”; 
} 
cout << data << endl; 
if (right != NULL) right->inOrder() 
}Output: A – C / 5 * 2 + D * 5 % 4 
To disambiguate: print brackets 
+ 
- % 
A * * 4 
/ 2 D 5 
C 5 
Trupti agrawal 24
Tree Traversal (contd.) 
 pre-order and post-order: 
void Node::preOrder () { 
cout << data << endl; 
if (left != NULL) left->preOrder (); 
if (right != NULL) right->preOrder (); 
} 
void Node::postOrder () { 
if (left != NULL) left->preOrder (); 
if (right != NULL) right->preOrder (); 
cout << data << endl; 
} 
+ 
- % 
A * * 4 
/ 2 D 5 
C 5 
Output: + - A * / C 5 2 % * D 5 4 
Output: A C 5 / 2 * - D 5 * 4 % + 
Trupti agrawal 25
THANK YOU…!!! 
Trupti agrawal 26

More Related Content

What's hot (20)

PPT
Binary search tree(bst)
Hossain Md Shakhawat
 
PPT
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
PPTX
Binary Tree in Data Structure
Meghaj Mallick
 
PPTX
Tree in data structure
ghhgj jhgh
 
PPTX
Binary Search Tree
Abhishek L.R
 
PPTX
Trees
Burhan Ahmed
 
PPTX
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
PPTX
Threaded Binary Tree
khabbab_h
 
PPT
Tree and Binary Search tree
Muhazzab Chouhadry
 
PPTX
Binary Tree Traversal
Dhrumil Panchal
 
PPT
Binary search tree in data structures
chauhankapil
 
PPTX
Binary trees1
Saurabh Mishra
 
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
PPT
Queue data structure
anooppjoseph
 
PPTX
Sparse matrix and its representation data structure
Vardhil Patel
 
PDF
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
PPTX
Binary Search Tree in Data Structure
Dharita Chokshi
 
PPT
Circular linked list
chauhankapil
 
PPTX
sorting and its types
SIVASHANKARIRAJAN
 
PPTX
Searching Techniques and Analysis
AkashBorse2
 
Binary search tree(bst)
Hossain Md Shakhawat
 
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Binary Tree in Data Structure
Meghaj Mallick
 
Tree in data structure
ghhgj jhgh
 
Binary Search Tree
Abhishek L.R
 
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Threaded Binary Tree
khabbab_h
 
Tree and Binary Search tree
Muhazzab Chouhadry
 
Binary Tree Traversal
Dhrumil Panchal
 
Binary search tree in data structures
chauhankapil
 
Binary trees1
Saurabh Mishra
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Queue data structure
anooppjoseph
 
Sparse matrix and its representation data structure
Vardhil Patel
 
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
Binary Search Tree in Data Structure
Dharita Chokshi
 
Circular linked list
chauhankapil
 
sorting and its types
SIVASHANKARIRAJAN
 
Searching Techniques and Analysis
AkashBorse2
 

Similar to Trees (data structure) (20)

PPT
Trees gt(1)
Gopi Saiteja
 
PPT
1.5 binary search tree
Krish_ver2
 
PPT
4.2 bst 03
Krish_ver2
 
PDF
CS-102 BST_27_3_14v2.pdf
ssuser034ce1
 
PPT
Mca admission in india
Edhole.com
 
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
VISWANATHAN R V
 
PDF
Trees
Amin shokrzade
 
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
PPTX
data structures module III & IV.pptx
rani marri
 
PPSX
data structure(tree operations)
Waheed Khalid
 
PPTX
Unit 3 trees
LavanyaJ28
 
PPTX
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
mohanrajm63
 
PPT
data structure on bca.
invertis university
 
PPTX
Week 8 (trees)
amna izzat
 
PPTX
Data Structures using Python(generic elective).pptx
mastimajakDKS
 
PPTX
Data structures and Algorithm analysis_Lecture4.pptx
AhmedEldesoky24
 
PPT
bst.ppt
plagcheck
 
PPTX
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
PPT
Trees - Non Linear Data Structure
Priyanka Rana
 
PPTX
Data structures 3
Parthipan Parthi
 
Trees gt(1)
Gopi Saiteja
 
1.5 binary search tree
Krish_ver2
 
4.2 bst 03
Krish_ver2
 
CS-102 BST_27_3_14v2.pdf
ssuser034ce1
 
Mca admission in india
Edhole.com
 
UNIT III Non Linear Data Structures - Trees.pptx
VISWANATHAN R V
 
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
data structures module III & IV.pptx
rani marri
 
data structure(tree operations)
Waheed Khalid
 
Unit 3 trees
LavanyaJ28
 
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
mohanrajm63
 
data structure on bca.
invertis university
 
Week 8 (trees)
amna izzat
 
Data Structures using Python(generic elective).pptx
mastimajakDKS
 
Data structures and Algorithm analysis_Lecture4.pptx
AhmedEldesoky24
 
bst.ppt
plagcheck
 
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
Trees - Non Linear Data Structure
Priyanka Rana
 
Data structures 3
Parthipan Parthi
 
Ad

More from Trupti Agrawal (7)

PPTX
Sorting algorithms
Trupti Agrawal
 
PPT
Searching algorithms
Trupti Agrawal
 
PPT
Searching algorithms
Trupti Agrawal
 
PPT
Linked list
Trupti Agrawal
 
PPT
Stacks and queues
Trupti Agrawal
 
PPTX
Arrays
Trupti Agrawal
 
PPTX
Data structure and algorithm
Trupti Agrawal
 
Sorting algorithms
Trupti Agrawal
 
Searching algorithms
Trupti Agrawal
 
Searching algorithms
Trupti Agrawal
 
Linked list
Trupti Agrawal
 
Stacks and queues
Trupti Agrawal
 
Data structure and algorithm
Trupti Agrawal
 
Ad

Recently uploaded (20)

PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
community health nursing question paper 2.pdf
Prince kumar
 
Dimensions of Societal Planning in Commonism
StefanMz
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 

Trees (data structure)

  • 1. Trees (Data Structure) Trupti agrawal 1
  • 2. Trees Data Structures  Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent  Binary tree  Tree with 0–2 children per node Tree Binary Tree Trupti agrawal 2
  • 3. Trees  Terminology  Root  no parent  Leaf  no child  Interior  non-leaf  Height  distance from root to leaf Root node Interior nodes Height Leaf nodes Trupti agrawal 3
  • 4. Binary Search Trees  Key property  Value at node  Smaller values in left subtree  Larger values in right subtree  Example  X > Y  X < Z Y X Z Trupti agrawal 4
  • 5. Binary Search Trees  Examples Binary search trees Not a binary search tree 5 10 30 2 25 45 5 10 45 2 25 30 5 10 30 2 25 45 Trupti agrawal 5
  • 6. Binary Tree Implementation Class Node { int data; // Could be int, a class, etc Node *left, *right; // null if empty void insert ( int data ) { … } void delete ( int data ) { … } Node *find ( int data ) { … } … } Trupti agrawal 6
  • 7. Iterative Search of Binary Tree Node *Find( Node *n, int key) { while (n != NULL) { if (n->data == key) // Found it return n; if (n->data > key) // In left subtree n = n->left; else // In right subtree n = n->right; } return null; } Node * n = Find( root, 5); Trupti agrawal 7
  • 8. Recursive Search of Binary Tree Node *Find( Node *n, int key) { if (n == NULL) // Not found return( n ); else if (n->data == key) // Found it return( n ); else if (n->data > key) // In left subtree return Find( n->left, key ); else // In right subtree return Find( n->right, key ); } Node * n = Find( root, 5); Trupti agrawal 8
  • 9. Example Binary Searches  Find ( root, 2 ) 5 10 30 2 25 45 5 10 30 2 25 45 10 > 2, left 5 > 2, left 2 = 2, found 5 > 2, left 2 = 2, found root Trupti agrawal 9
  • 10. Example Binary Searches  Find (root, 25 ) 5 10 30 2 25 45 5 10 30 2 25 45 10 < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found Trupti agrawal 10
  • 11. Types of Binary Trees  Degenerate – only one child  Complete – always two children  Balanced – “mostly” two children  more formal definitions exist, above are intuitive ideas Degenerate binary tree Balanced binary tree Complete binary tree Trupti agrawal 11
  • 12. Binary Trees Properties  Degenerate  Height = O(n) for n nodes  Similar to linked list  Balanced  Height = O( log(n) ) for n nodes  Useful for searches Degenerate binary tree Balanced binary tree Trupti agrawal 12
  • 13. Binary Search Properties  Time of search  Proportional to height of tree  Balanced binary tree  O( log(n) ) time  Degenerate tree  O( n ) time  Like searching linked list / unsorted array Trupti agrawal 13
  • 14. Binary Search Tree Construction  How to build & maintain binary trees?  Insertion  Deletion  Maintain key property (invariant)  Smaller values in left subtree  Larger values in right subtree Trupti agrawal 14
  • 15. Binary Search Tree – Insertion  Algorithm 1. Perform search for value X 2. Search will end at node Y (if X not in tree) 3. If X < Y, insert new leaf X as new left subtree for Y 4. If X > Y, insert new leaf X as new right subtree for Y  Observations  O( log(n) ) operation for balanced tree  Insertions may unbalance tree Trupti agrawal 15
  • 16. Example Insertion  Insert ( 20 ) 5 10 30 2 25 45 10 < 20, right 30 > 20, left 25 > 20, left Insert 20 on left 20 Trupti agrawal 16
  • 17. Binary Search Tree – Deletion  Algorithm 1. Perform search for value X 2. If X is a leaf, delete X 3. Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree Observation  O( log(n) ) operation for balanced tree  Deletions may unbalance tree Trupti agrawal 17
  • 18. Example Deletion (Leaf)  Delete ( 25 ) 5 10 30 2 25 45 10 < 25, right 30 > 25, left 25 = 25, delete 5 10 30 2 45 Trupti agrawal 18
  • 19. Example Deletion (Internal Node)  Delete ( 10 ) 5 10 30 2 25 45 5 5 30 2 25 45 2 5 30 2 25 45 Replacing 10 with largest value in left subtree Replacing 5 with largest value in left subtree Deleting leaf Trupti agrawal 19
  • 20. Example Deletion (Internal Node)  Delete ( 10 ) 5 10 30 2 25 45 5 25 30 2 25 45 5 25 30 2 45 Replacing 10 with smallest value in right subtree Deleting leaf Resulting tree Trupti agrawal 20
  • 21. Balanced Search Trees  Kinds of balanced binary search trees  height balanced vs. weight balanced  “Tree rotations” used to maintain balance on insert/delete  Non-binary search trees  2/3 trees  each internal node has 2 or 3 children  all leaves at same depth (height balanced)  B-trees  Generalization of 2/3 trees  Each internal node has between k/2 and k children  Each node has an array of pointers to children  Widely used in databases Trupti agrawal 21
  • 22. Other (Non-Search) Trees  Parse trees  Convert from textual representation to tree representation  Textual program to tree  Used extensively in compilers  Tree representation of data  E.g. HTML data can be represented as a tree  called DOM (Document Object Model) tree  XML  Like HTML, but used to represent data  Tree structured Trupti agrawal 22
  • 23. Parse Trees  Expressions, programs, etc can be represented by tree structures  E.g. Arithmetic Expression Tree  A-(C/5 * 2) + (D*5 % 4) + - % A * * 4 / 2 D 5 C 5 Trupti agrawal 23
  • 24. Tree Traversal  Goal: visit every node of a tree  in-order traversal void Node::inOrder () { if (left != NULL) { cout << “(“; left->inOrder(); cout << “)”; } cout << data << endl; if (right != NULL) right->inOrder() }Output: A – C / 5 * 2 + D * 5 % 4 To disambiguate: print brackets + - % A * * 4 / 2 D 5 C 5 Trupti agrawal 24
  • 25. Tree Traversal (contd.)  pre-order and post-order: void Node::preOrder () { cout << data << endl; if (left != NULL) left->preOrder (); if (right != NULL) right->preOrder (); } void Node::postOrder () { if (left != NULL) left->preOrder (); if (right != NULL) right->preOrder (); cout << data << endl; } + - % A * * 4 / 2 D 5 C 5 Output: + - A * / C 5 2 % * D 5 4 Output: A C 5 / 2 * - D 5 * 4 % + Trupti agrawal 25