SlideShare a Scribd company logo
Analysis of Algorithms
Binary Trees
Andres Mendez-Vazquez
August 26, 2014
1 / 15
Images/cinvestav-
Outline
1 Binary search trees
Binary search trees: Concepts
Binary search trees: Operations
2 Excercises
2 / 15
Images/cinvestav-
Outline
1 Binary search trees
Binary search trees: Concepts
Binary search trees: Operations
2 Excercises
3 / 15
Images/cinvestav-
Binary search trees: Concepts
Definition
A binary search tree (BST) is a data structure where each node posses
three fields left, right and p.
They represent its left child, right child and parent.
In addition, each node has the field key.
Property
Let x be a node in a binary search tree. If y is a node in the left subtree of
x, then key[y] ≤ key[x]. Similarly, if y is a node in the right subree of x,
then key[x] ≤ key[y].
4 / 15
Images/cinvestav-
Binary search trees: Concepts
Definition
A binary search tree (BST) is a data structure where each node posses
three fields left, right and p.
They represent its left child, right child and parent.
In addition, each node has the field key.
Property
Let x be a node in a binary search tree. If y is a node in the left subtree of
x, then key[y] ≤ key[x]. Similarly, if y is a node in the right subree of x,
then key[x] ≤ key[y].
4 / 15
Images/cinvestav-
Outline
1 Binary search trees
Binary search trees: Concepts
Binary search trees: Operations
2 Excercises
5 / 15
Images/cinvestav-
Binary search trees: Operations
This property allows to print the keys in sorted order!
Inorder-tree-walk(x)
1 if x= NIL
2 Inorder-tree-walk(x.left)
3 print x.key
4 Inorder-tree-walk(x.right)
6 / 15
Images/cinvestav-
Cost of inorder walk
Theorem 12.1
If x is the root of an n-node subtree, then the call Inorder-tree-walk(x)
takes Θ (n) time.
Proof:
Let T(n) denote the time taken by Inorder-tree-walk(x) when called at the
root.
First
Since Inorder-tree-walk(x) visit all the nodes then we have that
T (n) = Ω (n).
Thus, you need to prove T (n) = O (n)?
7 / 15
Images/cinvestav-
Cost of inorder walk
Theorem 12.1
If x is the root of an n-node subtree, then the call Inorder-tree-walk(x)
takes Θ (n) time.
Proof:
Let T(n) denote the time taken by Inorder-tree-walk(x) when called at the
root.
First
Since Inorder-tree-walk(x) visit all the nodes then we have that
T (n) = Ω (n).
Thus, you need to prove T (n) = O (n)?
7 / 15
Images/cinvestav-
Cost of inorder walk
Theorem 12.1
If x is the root of an n-node subtree, then the call Inorder-tree-walk(x)
takes Θ (n) time.
Proof:
Let T(n) denote the time taken by Inorder-tree-walk(x) when called at the
root.
First
Since Inorder-tree-walk(x) visit all the nodes then we have that
T (n) = Ω (n).
Thus, you need to prove T (n) = O (n)?
7 / 15
Images/cinvestav-
Proof of inorder walk
First
For n = 0, the method takes a constant time T(0) = c for some c > 0.
Now for n > 0
We have the following situation:
1 Left subtree has k nodes
2 Right subtree has n − k − 1 nodes
8 / 15
Images/cinvestav-
Proof of inorder walk
First
For n = 0, the method takes a constant time T(0) = c for some c > 0.
Now for n > 0
We have the following situation:
1 Left subtree has k nodes
2 Right subtree has n − k − 1 nodes
8 / 15
Images/cinvestav-
Substitution Method
We have finally
T(n) = T(k) + T(n − k − 1) + d
1 T(k) is the amount of work done in the left
2 T(n − k − 1) is the amount of work done in the right
3 d > 0 reflects an upper bound for the in-between work done for the
print.
We use the substitution method to prove that T(n) = O(n)
This can be done if we can bound T(n) by bounding it by
(c + d)n + c (1)
Thus for n = 0
T (0) = c = (c + d) × 0 + c (2)
9 / 15
Images/cinvestav-
Substitution Method
We have finally
T(n) = T(k) + T(n − k − 1) + d
1 T(k) is the amount of work done in the left
2 T(n − k − 1) is the amount of work done in the right
3 d > 0 reflects an upper bound for the in-between work done for the
print.
We use the substitution method to prove that T(n) = O(n)
This can be done if we can bound T(n) by bounding it by
(c + d)n + c (1)
Thus for n = 0
T (0) = c = (c + d) × 0 + c (2)
9 / 15
Images/cinvestav-
Substitution Method
We have finally
T(n) = T(k) + T(n − k − 1) + d
1 T(k) is the amount of work done in the left
2 T(n − k − 1) is the amount of work done in the right
3 d > 0 reflects an upper bound for the in-between work done for the
print.
We use the substitution method to prove that T(n) = O(n)
This can be done if we can bound T(n) by bounding it by
(c + d)n + c (1)
Thus for n = 0
T (0) = c = (c + d) × 0 + c (2)
9 / 15
Images/cinvestav-
Substitution Method
For n > 0
T(n) ≤ T(k) + T(n − k − 1) + d
= ((c + d) k + c) + ((c + d) (n − k − 1) + c) + d
= (c + d) n + c − (c + d) + c + d
= (c + d) n + c
Thus
T (n) = Θ (n) (3)
10 / 15
Images/cinvestav-
Substitution Method
For n > 0
T(n) ≤ T(k) + T(n − k − 1) + d
= ((c + d) k + c) + ((c + d) (n − k − 1) + c) + d
= (c + d) n + c − (c + d) + c + d
= (c + d) n + c
Thus
T (n) = Θ (n) (3)
10 / 15
Images/cinvestav-
Binary search trees: Operations
Searching
Tree-search(x, k)
1 if x == NIL or k == x.key
2 return x
3 if k < x.key
4 return Tree-search(x.left, k)
5 else return Tree-search(x.right, k)
Complexity
O (h) (4)
where h is the height of the tree ⇒ we look for well balanced trees.
11 / 15
Images/cinvestav-
Binary search trees: Operations
Searching
Tree-search(x, k)
1 if x == NIL or k == x.key
2 return x
3 if k < x.key
4 return Tree-search(x.left, k)
5 else return Tree-search(x.right, k)
Complexity
O (h) (4)
where h is the height of the tree ⇒ we look for well balanced trees.
11 / 15
Images/cinvestav-
Binary search trees: Operations
Minimum and maximum
Tree-minimum(x)
1 while x.left =NIL
2 x = x.left
3 return x
Complexity
O (h) (5)
where h is the height of the tree ⇒ we look for well balanced trees.
12 / 15
Images/cinvestav-
Binary search trees: Operations
Minimum and maximum
Tree-minimum(x)
1 while x.left =NIL
2 x = x.left
3 return x
Complexity
O (h) (5)
where h is the height of the tree ⇒ we look for well balanced trees.
12 / 15
Images/cinvestav-
Binary search trees: Operations
TREE-DELETE(T, z)
1 if z.left == NIL
2 Transplant(T, z, z.right)
3 elseif z.right == NIL
4 Transplant(T, z, z.left)
5 else
6 y=Tree-minimum(z.right)
7 if y.p = z
8 Transplant(T, y, y.right)
9 y.right = z.right
10 y.right.p = y
11 Transplant(T, z, y)
12 y.left = z.left
13 y.left.p = y
Case 1
Basically if the element z to be
deleted has a NIL left child
simply replace z with that
child!!!
13 / 15
Images/cinvestav-
Binary search trees: Operations
TREE-DELETE(T, z)
1 if z.left == NIL
2 Transplant(T, z, z.right)
3 elseif z.right == NIL
4 Transplant(T, z, z.left)
5 else
6 y=Tree-minimum(z.right)
7 if y.p = z
8 Transplant(T, y, y.right)
9 y.right = z.right
10 y.right.p = y
11 Transplant(T, z, y)
12 y.left = z.left
13 y.left.p = y
Case 2
Basically if the element z to be
deleted has a NIL right child
simply replace z with that
child!!!
13 / 15
Images/cinvestav-
Binary search trees: Operations
TREE-DELETE(T, z)
1 if z.left == NIL
2 Transplant(T, z, z.right)
3 elseif z.right == NIL
4 Transplant(T, z, z.left)
5 else
6 y=Tree-minimum(z.right)
7 if y.p = z
8 Transplant(T, y, y.right)
9 y.right = z.right
10 y.right.p = y
11 Transplant(T, z, y)
12 y.left = z.left
13 y.left.p = y
Case 3
The z element has not empty
children you need to find the
successor of it.
13 / 15
Images/cinvestav-
Binary search trees: Operations
TREE-DELETE(T, z)
1 if z.left == NIL
2 Transplant(T, z, z.right)
3 elseif z.right == NIL
4 Transplant(T, z, z.left)
5 else
6 y=Tree-minimum(z.right)
7 if y.p = z
8 Transplant(T, y, y.right)
9 y.right = z.right
10 y.right.p = y
11 Transplant(T, z, y)
12 y.left = z.left
13 y.left.p = y
Case 4
if y.p = z then y.right takes the
position of y after all y.left ==
NIL
take z.right and make it the
new right of y
make the
(y.right == z.right).p equal
to y
13 / 15
Images/cinvestav-
Binary search trees: Operations
TREE-DELETE(T, z)
1 if z.left == NIL
2 Transplant(T, z, z.right)
3 elseif z.right == NIL
4 Transplant(T, z, z.left)
5 else
6 y=Tree-minimum(z.right)
7 if y.p = z
8 Transplant(T, y, y.right)
9 y.right = z.right
10 y.right.p = y
11 Transplant(T, z, y)
12 y.left = z.left
13 y.left.p = y
Case 4
put y in the position of z
make y.left equal to z.left
make the (y.left == z.left).p
equal to y
13 / 15
Images/cinvestav-
Support Operations
Transplant(T, u, v)
1 if u.p == NIL
2 T.root = v
3 elseif u == u.p.left
4 u.p.left = v
5 else u.p.right = v
6 if v =NIL
7 v.p = u.p
Case 1
If u is the root then make the
root equal to v
14 / 15
Images/cinvestav-
Support Operations
Transplant(T, u, v)
1 if u.p == NIL
2 T.root = v
3 elseif u == u.p.left
4 u.p.left = v
5 else u.p.right = v
6 if v =NIL
7 v.p = u.p
Case 2
if u is the left child make the
left child of the parent of u
equal to v
14 / 15
Images/cinvestav-
Support Operations
Transplant(T, u, v)
1 if u.p == NIL
2 T.root = v
3 elseif u == u.p.left
4 u.p.left = v
5 else u.p.right = v
6 if v =NIL
7 v.p = u.p
Case 3
Similar to the second case, but
for right child
14 / 15
Images/cinvestav-
Support Operations
Transplant(T, u, v)
1 if u.p == NIL
2 T.root = v
3 elseif u == u.p.left
4 u.p.left = v
5 else u.p.right = v
6 if v =NIL
7 v.p = u.p
Case 4
If v = NIL then make the parent
of v the parent of u
14 / 15
Images/cinvestav-
Excercises
From Cormen’s book, chapters 11 and 12
11.1-2
11.2-1
11.2-2
11.2-3
11.3-1
11.3-3
12.1-3
12.1-5
12.2-5
12.2-7
12.2-9
12.3-3
15 / 15

More Related Content

PDF
Lesson 2: A Catalog of Essential Functions (slides)
PDF
slides CIRM copulas, extremes and actuarial science
PDF
Inequalities #2
PDF
Inequality #4
PDF
parent functons
PDF
About RNN
PDF
About RNN
PDF
Multiattribute utility copula
Lesson 2: A Catalog of Essential Functions (slides)
slides CIRM copulas, extremes and actuarial science
Inequalities #2
Inequality #4
parent functons
About RNN
About RNN
Multiattribute utility copula

What's hot (18)

PDF
Lesson 27: Integration by Substitution (slides)
PDF
Murphy: Machine learning A probabilistic perspective: Ch.9
PDF
slides tails copulas
PDF
Sildes buenos aires
PDF
Lesson 10: The Chain Rule (slides)
PDF
Lundi 16h15-copules-charpentier
PDF
Introduccio al calculo vectorial
PDF
Lesson 26: The Fundamental Theorem of Calculus (slides)
PDF
Slides astin
PDF
Slides ensae-2016-8
DOCX
Rasterisation of a circle by the bresenham algorithm
PDF
Fougeres Besancon Archimax
PDF
Matrix calculus
PDF
Harmonic Analysis and Deep Learning
PDF
Reading Seminar (140515) Spectral Learning of L-PCFGs
PDF
Lesson 24: Implicit Differentiation
PDF
Deep generative model.pdf
PDF
Lesson 21: Curve Sketching (slides)
Lesson 27: Integration by Substitution (slides)
Murphy: Machine learning A probabilistic perspective: Ch.9
slides tails copulas
Sildes buenos aires
Lesson 10: The Chain Rule (slides)
Lundi 16h15-copules-charpentier
Introduccio al calculo vectorial
Lesson 26: The Fundamental Theorem of Calculus (slides)
Slides astin
Slides ensae-2016-8
Rasterisation of a circle by the bresenham algorithm
Fougeres Besancon Archimax
Matrix calculus
Harmonic Analysis and Deep Learning
Reading Seminar (140515) Spectral Learning of L-PCFGs
Lesson 24: Implicit Differentiation
Deep generative model.pdf
Lesson 21: Curve Sketching (slides)
Ad

Viewers also liked (6)

PDF
07.2 Holland's Genetic Algorithms Schema Theorem
PPTX
Experimental Design | Statistics
PPTX
Compression Springs | Mechenical Engineering
PPT
4.4 hashing
PPT
Hashing
PPT
12. Indexing and Hashing in DBMS
07.2 Holland's Genetic Algorithms Schema Theorem
Experimental Design | Statistics
Compression Springs | Mechenical Engineering
4.4 hashing
Hashing
12. Indexing and Hashing in DBMS
Ad

Similar to 09 binary trees (20)

PPTX
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
PPTX
Data structures and Algorithm analysis_Lecture4.pptx
PPT
6_1 (1).ppt
PPTX
Lecture 9 data structures and algorithms
PDF
Red black tree
PPTX
8.binry search tree
PDF
PPTX
Week 8 (trees)
PPTX
UNIT-4 (1).pptx ok thanks for the party o
PPT
4a searching-more
PDF
CS-102 BST_27_3_14v2.pdf
PDF
Trees second part in data structures with examples
PPT
Binary search trees
PPT
Cinterviews Binarysearch Tree
PPT
Chapter 9 ds
PPTX
Lecture_10 - Revised.pptx
PDF
LEC 5-DS ALGO(updated).pdf
PPTX
BST.pptx this is Good for data structure
PPTX
BST.pptx this isp used for learning binary search trees
DOCX
5220191CS146 Data Structures and AlgorithmsC.docx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
Data structures and Algorithm analysis_Lecture4.pptx
6_1 (1).ppt
Lecture 9 data structures and algorithms
Red black tree
8.binry search tree
Week 8 (trees)
UNIT-4 (1).pptx ok thanks for the party o
4a searching-more
CS-102 BST_27_3_14v2.pdf
Trees second part in data structures with examples
Binary search trees
Cinterviews Binarysearch Tree
Chapter 9 ds
Lecture_10 - Revised.pptx
LEC 5-DS ALGO(updated).pdf
BST.pptx this is Good for data structure
BST.pptx this isp used for learning binary search trees
5220191CS146 Data Structures and AlgorithmsC.docx

More from Andres Mendez-Vazquez (20)

PDF
2.03 bayesian estimation
PDF
05 linear transformations
PDF
01.04 orthonormal basis_eigen_vectors
PDF
01.03 squared matrices_and_other_issues
PDF
01.02 linear equations
PDF
01.01 vector spaces
PDF
06 recurrent neural_networks
PDF
05 backpropagation automatic_differentiation
PDF
Zetta global
PDF
01 Introduction to Neural Networks and Deep Learning
PDF
25 introduction reinforcement_learning
PDF
Neural Networks and Deep Learning Syllabus
PDF
Introduction to artificial_intelligence_syllabus
PDF
Ideas 09 22_2018
PDF
Ideas about a Bachelor in Machine Learning/Data Sciences
PDF
Analysis of Algorithms Syllabus
PDF
20 k-means, k-center, k-meoids and variations
PDF
18.1 combining models
PDF
17 vapnik chervonenkis dimension
PDF
A basic introduction to learning
2.03 bayesian estimation
05 linear transformations
01.04 orthonormal basis_eigen_vectors
01.03 squared matrices_and_other_issues
01.02 linear equations
01.01 vector spaces
06 recurrent neural_networks
05 backpropagation automatic_differentiation
Zetta global
01 Introduction to Neural Networks and Deep Learning
25 introduction reinforcement_learning
Neural Networks and Deep Learning Syllabus
Introduction to artificial_intelligence_syllabus
Ideas 09 22_2018
Ideas about a Bachelor in Machine Learning/Data Sciences
Analysis of Algorithms Syllabus
20 k-means, k-center, k-meoids and variations
18.1 combining models
17 vapnik chervonenkis dimension
A basic introduction to learning

Recently uploaded (20)

PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
additive manufacturing of ss316l using mig welding
PDF
composite construction of structures.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
DOCX
573137875-Attendance-Management-System-original
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Lecture Notes Electrical Wiring System Components
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Mechanical Engineering MATERIALS Selection
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Model Code of Practice - Construction Work - 21102022 .pdf
CH1 Production IntroductoryConcepts.pptx
Digital Logic Computer Design lecture notes
additive manufacturing of ss316l using mig welding
composite construction of structures.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Foundation to blockchain - A guide to Blockchain Tech
573137875-Attendance-Management-System-original
bas. eng. economics group 4 presentation 1.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
R24 SURVEYING LAB MANUAL for civil enggi
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf

09 binary trees

  • 1. Analysis of Algorithms Binary Trees Andres Mendez-Vazquez August 26, 2014 1 / 15
  • 2. Images/cinvestav- Outline 1 Binary search trees Binary search trees: Concepts Binary search trees: Operations 2 Excercises 2 / 15
  • 3. Images/cinvestav- Outline 1 Binary search trees Binary search trees: Concepts Binary search trees: Operations 2 Excercises 3 / 15
  • 4. Images/cinvestav- Binary search trees: Concepts Definition A binary search tree (BST) is a data structure where each node posses three fields left, right and p. They represent its left child, right child and parent. In addition, each node has the field key. Property Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] ≤ key[x]. Similarly, if y is a node in the right subree of x, then key[x] ≤ key[y]. 4 / 15
  • 5. Images/cinvestav- Binary search trees: Concepts Definition A binary search tree (BST) is a data structure where each node posses three fields left, right and p. They represent its left child, right child and parent. In addition, each node has the field key. Property Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] ≤ key[x]. Similarly, if y is a node in the right subree of x, then key[x] ≤ key[y]. 4 / 15
  • 6. Images/cinvestav- Outline 1 Binary search trees Binary search trees: Concepts Binary search trees: Operations 2 Excercises 5 / 15
  • 7. Images/cinvestav- Binary search trees: Operations This property allows to print the keys in sorted order! Inorder-tree-walk(x) 1 if x= NIL 2 Inorder-tree-walk(x.left) 3 print x.key 4 Inorder-tree-walk(x.right) 6 / 15
  • 8. Images/cinvestav- Cost of inorder walk Theorem 12.1 If x is the root of an n-node subtree, then the call Inorder-tree-walk(x) takes Θ (n) time. Proof: Let T(n) denote the time taken by Inorder-tree-walk(x) when called at the root. First Since Inorder-tree-walk(x) visit all the nodes then we have that T (n) = Ω (n). Thus, you need to prove T (n) = O (n)? 7 / 15
  • 9. Images/cinvestav- Cost of inorder walk Theorem 12.1 If x is the root of an n-node subtree, then the call Inorder-tree-walk(x) takes Θ (n) time. Proof: Let T(n) denote the time taken by Inorder-tree-walk(x) when called at the root. First Since Inorder-tree-walk(x) visit all the nodes then we have that T (n) = Ω (n). Thus, you need to prove T (n) = O (n)? 7 / 15
  • 10. Images/cinvestav- Cost of inorder walk Theorem 12.1 If x is the root of an n-node subtree, then the call Inorder-tree-walk(x) takes Θ (n) time. Proof: Let T(n) denote the time taken by Inorder-tree-walk(x) when called at the root. First Since Inorder-tree-walk(x) visit all the nodes then we have that T (n) = Ω (n). Thus, you need to prove T (n) = O (n)? 7 / 15
  • 11. Images/cinvestav- Proof of inorder walk First For n = 0, the method takes a constant time T(0) = c for some c > 0. Now for n > 0 We have the following situation: 1 Left subtree has k nodes 2 Right subtree has n − k − 1 nodes 8 / 15
  • 12. Images/cinvestav- Proof of inorder walk First For n = 0, the method takes a constant time T(0) = c for some c > 0. Now for n > 0 We have the following situation: 1 Left subtree has k nodes 2 Right subtree has n − k − 1 nodes 8 / 15
  • 13. Images/cinvestav- Substitution Method We have finally T(n) = T(k) + T(n − k − 1) + d 1 T(k) is the amount of work done in the left 2 T(n − k − 1) is the amount of work done in the right 3 d > 0 reflects an upper bound for the in-between work done for the print. We use the substitution method to prove that T(n) = O(n) This can be done if we can bound T(n) by bounding it by (c + d)n + c (1) Thus for n = 0 T (0) = c = (c + d) × 0 + c (2) 9 / 15
  • 14. Images/cinvestav- Substitution Method We have finally T(n) = T(k) + T(n − k − 1) + d 1 T(k) is the amount of work done in the left 2 T(n − k − 1) is the amount of work done in the right 3 d > 0 reflects an upper bound for the in-between work done for the print. We use the substitution method to prove that T(n) = O(n) This can be done if we can bound T(n) by bounding it by (c + d)n + c (1) Thus for n = 0 T (0) = c = (c + d) × 0 + c (2) 9 / 15
  • 15. Images/cinvestav- Substitution Method We have finally T(n) = T(k) + T(n − k − 1) + d 1 T(k) is the amount of work done in the left 2 T(n − k − 1) is the amount of work done in the right 3 d > 0 reflects an upper bound for the in-between work done for the print. We use the substitution method to prove that T(n) = O(n) This can be done if we can bound T(n) by bounding it by (c + d)n + c (1) Thus for n = 0 T (0) = c = (c + d) × 0 + c (2) 9 / 15
  • 16. Images/cinvestav- Substitution Method For n > 0 T(n) ≤ T(k) + T(n − k − 1) + d = ((c + d) k + c) + ((c + d) (n − k − 1) + c) + d = (c + d) n + c − (c + d) + c + d = (c + d) n + c Thus T (n) = Θ (n) (3) 10 / 15
  • 17. Images/cinvestav- Substitution Method For n > 0 T(n) ≤ T(k) + T(n − k − 1) + d = ((c + d) k + c) + ((c + d) (n − k − 1) + c) + d = (c + d) n + c − (c + d) + c + d = (c + d) n + c Thus T (n) = Θ (n) (3) 10 / 15
  • 18. Images/cinvestav- Binary search trees: Operations Searching Tree-search(x, k) 1 if x == NIL or k == x.key 2 return x 3 if k < x.key 4 return Tree-search(x.left, k) 5 else return Tree-search(x.right, k) Complexity O (h) (4) where h is the height of the tree ⇒ we look for well balanced trees. 11 / 15
  • 19. Images/cinvestav- Binary search trees: Operations Searching Tree-search(x, k) 1 if x == NIL or k == x.key 2 return x 3 if k < x.key 4 return Tree-search(x.left, k) 5 else return Tree-search(x.right, k) Complexity O (h) (4) where h is the height of the tree ⇒ we look for well balanced trees. 11 / 15
  • 20. Images/cinvestav- Binary search trees: Operations Minimum and maximum Tree-minimum(x) 1 while x.left =NIL 2 x = x.left 3 return x Complexity O (h) (5) where h is the height of the tree ⇒ we look for well balanced trees. 12 / 15
  • 21. Images/cinvestav- Binary search trees: Operations Minimum and maximum Tree-minimum(x) 1 while x.left =NIL 2 x = x.left 3 return x Complexity O (h) (5) where h is the height of the tree ⇒ we look for well balanced trees. 12 / 15
  • 22. Images/cinvestav- Binary search trees: Operations TREE-DELETE(T, z) 1 if z.left == NIL 2 Transplant(T, z, z.right) 3 elseif z.right == NIL 4 Transplant(T, z, z.left) 5 else 6 y=Tree-minimum(z.right) 7 if y.p = z 8 Transplant(T, y, y.right) 9 y.right = z.right 10 y.right.p = y 11 Transplant(T, z, y) 12 y.left = z.left 13 y.left.p = y Case 1 Basically if the element z to be deleted has a NIL left child simply replace z with that child!!! 13 / 15
  • 23. Images/cinvestav- Binary search trees: Operations TREE-DELETE(T, z) 1 if z.left == NIL 2 Transplant(T, z, z.right) 3 elseif z.right == NIL 4 Transplant(T, z, z.left) 5 else 6 y=Tree-minimum(z.right) 7 if y.p = z 8 Transplant(T, y, y.right) 9 y.right = z.right 10 y.right.p = y 11 Transplant(T, z, y) 12 y.left = z.left 13 y.left.p = y Case 2 Basically if the element z to be deleted has a NIL right child simply replace z with that child!!! 13 / 15
  • 24. Images/cinvestav- Binary search trees: Operations TREE-DELETE(T, z) 1 if z.left == NIL 2 Transplant(T, z, z.right) 3 elseif z.right == NIL 4 Transplant(T, z, z.left) 5 else 6 y=Tree-minimum(z.right) 7 if y.p = z 8 Transplant(T, y, y.right) 9 y.right = z.right 10 y.right.p = y 11 Transplant(T, z, y) 12 y.left = z.left 13 y.left.p = y Case 3 The z element has not empty children you need to find the successor of it. 13 / 15
  • 25. Images/cinvestav- Binary search trees: Operations TREE-DELETE(T, z) 1 if z.left == NIL 2 Transplant(T, z, z.right) 3 elseif z.right == NIL 4 Transplant(T, z, z.left) 5 else 6 y=Tree-minimum(z.right) 7 if y.p = z 8 Transplant(T, y, y.right) 9 y.right = z.right 10 y.right.p = y 11 Transplant(T, z, y) 12 y.left = z.left 13 y.left.p = y Case 4 if y.p = z then y.right takes the position of y after all y.left == NIL take z.right and make it the new right of y make the (y.right == z.right).p equal to y 13 / 15
  • 26. Images/cinvestav- Binary search trees: Operations TREE-DELETE(T, z) 1 if z.left == NIL 2 Transplant(T, z, z.right) 3 elseif z.right == NIL 4 Transplant(T, z, z.left) 5 else 6 y=Tree-minimum(z.right) 7 if y.p = z 8 Transplant(T, y, y.right) 9 y.right = z.right 10 y.right.p = y 11 Transplant(T, z, y) 12 y.left = z.left 13 y.left.p = y Case 4 put y in the position of z make y.left equal to z.left make the (y.left == z.left).p equal to y 13 / 15
  • 27. Images/cinvestav- Support Operations Transplant(T, u, v) 1 if u.p == NIL 2 T.root = v 3 elseif u == u.p.left 4 u.p.left = v 5 else u.p.right = v 6 if v =NIL 7 v.p = u.p Case 1 If u is the root then make the root equal to v 14 / 15
  • 28. Images/cinvestav- Support Operations Transplant(T, u, v) 1 if u.p == NIL 2 T.root = v 3 elseif u == u.p.left 4 u.p.left = v 5 else u.p.right = v 6 if v =NIL 7 v.p = u.p Case 2 if u is the left child make the left child of the parent of u equal to v 14 / 15
  • 29. Images/cinvestav- Support Operations Transplant(T, u, v) 1 if u.p == NIL 2 T.root = v 3 elseif u == u.p.left 4 u.p.left = v 5 else u.p.right = v 6 if v =NIL 7 v.p = u.p Case 3 Similar to the second case, but for right child 14 / 15
  • 30. Images/cinvestav- Support Operations Transplant(T, u, v) 1 if u.p == NIL 2 T.root = v 3 elseif u == u.p.left 4 u.p.left = v 5 else u.p.right = v 6 if v =NIL 7 v.p = u.p Case 4 If v = NIL then make the parent of v the parent of u 14 / 15
  • 31. Images/cinvestav- Excercises From Cormen’s book, chapters 11 and 12 11.1-2 11.2-1 11.2-2 11.2-3 11.3-1 11.3-3 12.1-3 12.1-5 12.2-5 12.2-7 12.2-9 12.3-3 15 / 15