SlideShare a Scribd company logo
1 of 28
Tree Data Structures
Gaurav Trivedi
Adapted from S. Sudarsan Slides
Based partly on material from
Fawzi Emad & Chau-Wen Tseng
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
Trees
 Terminology
 Root ⇒ no parent
 Leaf ⇒ no child
 Interior ⇒ non-leaf
 Height ⇒ distance from root to leaf
Root node
Leaf nodes
Interior nodes Height
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
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
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 ) { … }
…
}
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);
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);
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
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
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
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
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
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
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
Example Insertion
 Insert ( 20 )
5
10
30
2 25 45
10 < 20, right
30 > 20, left
25 > 20, left
Insert 20 on left
20
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
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
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
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
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
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
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
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
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;
}
Output: + - A * / C 5 2 % * D 5 4
Output: A C 5 / 2 * - D 5 * 4 % +
+
- %
A * * 4
/ 2 D 5
C 5
XML
 Data Representation
 E.g.
<dependency>
<object>sample1.o</object>
<depends>sample1.cpp</depends>
<depends>sample1.h</depends>
<rule>g++ -c sample1.cpp</rule>
</dependency>
 Tree representation
dependency
object depends
sample1.o sample1.cpp
depends
sample1.h
rule
g++ -c …
Graph Data Structures
 E.g: Airline networks, road networks, electrical circuits
 Nodes and Edges
 E.g. representation: class Node
 Stores name
 stores pointers to all adjacent nodes
 i,e. edge == pointer
 To store multiple pointers: use array or linked list
Ahm’bad
Delhi
Mumbai
Calcutta
Chennai
Madurai
End of Chapter

More Related Content

What's hot

Jan. 12 Binomial Factor Theorm
Jan. 12 Binomial Factor TheormJan. 12 Binomial Factor Theorm
Jan. 12 Binomial Factor Theorm
RyanWatt
 
Polynomial functions
Polynomial functionsPolynomial functions
Polynomial functions
emily321
 
5 4 function notation
5 4 function notation5 4 function notation
5 4 function notation
hisema01
 
02-04 Relations Functions
02-04 Relations Functions02-04 Relations Functions
02-04 Relations Functions
Bitsy Griffin
 
Most useful queries
Most useful queriesMost useful queries
Most useful queries
Sam Depp
 
2.2.2 the graph of a function
2.2.2 the graph of a function2.2.2 the graph of a function
2.2.2 the graph of a function
Northside ISD
 

What's hot (20)

Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Bst(Binary Search Tree)
Bst(Binary Search Tree)Bst(Binary Search Tree)
Bst(Binary Search Tree)
 
Chapter 9 ds
Chapter 9 dsChapter 9 ds
Chapter 9 ds
 
Jan. 12 Binomial Factor Theorm
Jan. 12 Binomial Factor TheormJan. 12 Binomial Factor Theorm
Jan. 12 Binomial Factor Theorm
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci Heaps
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Polynomial functions
Polynomial functionsPolynomial functions
Polynomial functions
 
Hashing
HashingHashing
Hashing
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
5 4 function notation
5 4 function notation5 4 function notation
5 4 function notation
 
02-04 Relations Functions
02-04 Relations Functions02-04 Relations Functions
02-04 Relations Functions
 
Most useful queries
Most useful queriesMost useful queries
Most useful queries
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
2.2.2 the graph of a function
2.2.2 the graph of a function2.2.2 the graph of a function
2.2.2 the graph of a function
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for Strings
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

Similar to Trees gt(1)

BinarySearchTrees (1).ppt
BinarySearchTrees (1).pptBinarySearchTrees (1).ppt
BinarySearchTrees (1).ppt
plagcheck
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.ppt
plagcheck
 
data structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.pptdata structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.ppt
DharmannaRathod1
 

Similar to Trees gt(1) (20)

1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Search data structures
Search data structuresSearch data structures
Search data structures
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
mitochondria moment and super computer integration.ppt
mitochondria moment and super computer integration.pptmitochondria moment and super computer integration.ppt
mitochondria moment and super computer integration.ppt
 
bst.ppt
bst.pptbst.ppt
bst.ppt
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.ppt
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.ppt
 
BinarySearchTrees (1).ppt
BinarySearchTrees (1).pptBinarySearchTrees (1).ppt
BinarySearchTrees (1).ppt
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.ppt
 
data structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.pptdata structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.ppt
 
Binary tree
Binary treeBinary tree
Binary 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
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 

More from Gopi Saiteja (20)

Topic11 sortingandsearching
Topic11 sortingandsearchingTopic11 sortingandsearching
Topic11 sortingandsearching
 
Heapsort
HeapsortHeapsort
Heapsort
 
Hashing gt1
Hashing gt1Hashing gt1
Hashing gt1
 
Ee693 sept2014quizgt2
Ee693 sept2014quizgt2Ee693 sept2014quizgt2
Ee693 sept2014quizgt2
 
Ee693 sept2014quizgt1
Ee693 sept2014quizgt1Ee693 sept2014quizgt1
Ee693 sept2014quizgt1
 
Ee693 sept2014quiz1
Ee693 sept2014quiz1Ee693 sept2014quiz1
Ee693 sept2014quiz1
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsem
 
Ee693 questionshomework
Ee693 questionshomeworkEe693 questionshomework
Ee693 questionshomework
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Cs105 l15-bucket radix
Cs105 l15-bucket radixCs105 l15-bucket radix
Cs105 l15-bucket radix
 
Chapter11 sorting algorithmsefficiency
Chapter11 sorting algorithmsefficiencyChapter11 sorting algorithmsefficiency
Chapter11 sorting algorithmsefficiency
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanations
 
Sorting
SortingSorting
Sorting
 
Solution(1)
Solution(1)Solution(1)
Solution(1)
 
Pthread
PthreadPthread
Pthread
 
Open mp
Open mpOpen mp
Open mp
 
Introduction
IntroductionIntroduction
Introduction
 
Cuda
CudaCuda
Cuda
 
Vector space interpretation_of_random_variables
Vector space interpretation_of_random_variablesVector space interpretation_of_random_variables
Vector space interpretation_of_random_variables
 
Statistical signal processing(1)
Statistical signal processing(1)Statistical signal processing(1)
Statistical signal processing(1)
 

Recently uploaded

21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
rahulmanepalli02
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
MaherOthman7
 

Recently uploaded (20)

Working Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdfWorking Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdf
 
What is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, FunctionsWhat is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, Functions
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdf
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Students
 

Trees gt(1)

  • 1. Tree Data Structures Gaurav Trivedi Adapted from S. Sudarsan Slides Based partly on material from Fawzi Emad & Chau-Wen Tseng
  • 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
  • 3. Trees  Terminology  Root ⇒ no parent  Leaf ⇒ no child  Interior ⇒ non-leaf  Height ⇒ distance from root to leaf Root node Leaf nodes Interior nodes Height
  • 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
  • 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
  • 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 ) { … } … }
  • 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);
  • 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);
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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; } Output: + - A * / C 5 2 % * D 5 4 Output: A C 5 / 2 * - D 5 * 4 % + + - % A * * 4 / 2 D 5 C 5
  • 26. XML  Data Representation  E.g. <dependency> <object>sample1.o</object> <depends>sample1.cpp</depends> <depends>sample1.h</depends> <rule>g++ -c sample1.cpp</rule> </dependency>  Tree representation dependency object depends sample1.o sample1.cpp depends sample1.h rule g++ -c …
  • 27. Graph Data Structures  E.g: Airline networks, road networks, electrical circuits  Nodes and Edges  E.g. representation: class Node  Stores name  stores pointers to all adjacent nodes  i,e. edge == pointer  To store multiple pointers: use array or linked list Ahm’bad Delhi Mumbai Calcutta Chennai Madurai