Transcript Document

Chapter 6 Relations
• Relationships are dealed with every day: businesses and
their telephone numbers, employees and their salaries, ….
• Relationships can be used to solve problems such as:
determining which pairs of cities are linked by airline flights
in a network, finding a viable order for the different phases of
a complicated project.
6.1 Relations and Their Properties
Definition1. Let A and B be sets. A binary relationR from A to B
is a subset of A  B.
• From Definition 1, the binary relation R is a subset of
{(a, b) | a  A, b  B}
When(a,b) R, a is said to be relatedtob by R.
Sometimes,we use aRb to denote(a,b) R, and aRb to denote(a,b) R.
Example 1 Let A be the set of students, and let B the set of
courses. Define R to be the relation that consists of those pairs
(a,b), where a is a student enrolled in course b.
T hatis, R  {(a, b) | a  A, b  B, student a enrolledin courseb}
Example 2 Let A be a set of all cities, and let B be the set of the 50
states in the United States. Define the relation R by specifying that
(a,b) belongs to R if city a is in state b.
Solution:
R={(Boulder,Colorado),(Bangor,Maine),(Ann Arbor,Michigan),
(Cupertino,California), (Red Bank,New Jersey),…}
Example 3 Let A={0,1,2} and B={a,b}. R={(0,a),(0,b),(1,a),(2,b)}
is a relation from A to B.
0
1
2
a
b
Functions as Relations
•A function f from a set A to a set B assigns a unique element of B
to each element of A.
T hatis, for each a  A, f assign a unique b  B to a. It is denotedas f(a)  b.
f can be consideredas a relation: f  {(a, b) | a  A, b  B, f (a)  b}.
But a relation does not have to be a function.
Example Let A={0,1,2} and B={a,b}. Let R={(0,a),(0,b),(1,a),(2,b)}
and R’={(0,a),(1,a),(2,b)} be two relations from A to B.
0
a
1
b
2
R
0
a
1
b
2
R’
R’ is a function, but R is not a function.
Relations on A Set
Definition 2. A relation R on the set A is a relation from A to A.
T hatis, R is a subset of {(a,b) |a A, b A}.
Example 4. Let A be the set {1,2,3,4}. Which ordered pairs
are in the relation R={(a,b) | a divides b}?
Solution R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,4),(3,3),(4,4)}.
Example 5. Consider the following relations on the set of integers.
R1  {( a, b) | a  b}
R2  {( a, b) | a  b}
R3  {( a, b) | a  b or a  b}
R4  {( a, b) | a  b}
R5  {( a, b) | a  b  1}
R6  {( a, b) | a  b  3}
Whichof theserelationscontaineach of (1,1),(1,2),(2,1),(1,-1)and (2,2)?
Solution : (1,1) R1,R3 ,R4 ,R6 ; (1,2) R1 ,R6 ; (2,1) R2 ,R5 ,R6 ;
(1,1)  R2 ,R3 ,R6 ; (2,2)  R1,R3 ,R4 .
Properties of Relations
Definition3. A relationR on a set A is called reflexiveif (a,a) R
for everyelementa  A.
Example 6. Consider the following relations on {1,2,3,4}
R1  {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)}
R2  {(1,1), (1,2), (2,1)}
R3  {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (4,1)(4,4)}
R4  {( 2,1), (3,1), (3,2), (4,1), (4,2), (4,3)}
R5  {(1,1), (1,2), (1,3), (1,4), (2,2)(2,3), (2,4), (3,3), (3,4), (4,4)}
R6  {(3,4)}
Whichof t heserelat ionsare reflext ive?
Solution: R3 and R5 are reflexitive. Othersare not reflexive.
Example 7. Consider the following relations on the set of integers.
R1  {( a, b) | a  b}
R2  {( a, b) | a  b}
R3  {( a, b) | a  b or a  b}
R4  {( a, b) | a  b}
R5  {( a, b) | a  b  1}
R6  {( a, b) | a  b  3}
Whichof t heserelat ionsare reflexive?
Solution : R1 , R3 and R4 are reflextive,
since for and integera, (a,a) R1 , R3 and R4 .
Example 8. Is the “divides” relation on the set of a positive
integers reflexive?
Solution: It is reflexive, since a|a whenever a is a positive integer.
Definition4.
A relationR on a set A is called sym m etricif (b,a)  R whenever(a,b)  R,
for any a,b A.
A relationR on a set A such that(a,b)  R and (b,a)  R onlyif a  b,
for any a,b A, is called antisym m etric.
Example 9. Consider the following relations on {1,2,3,4}
R1  {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)}
R2  {(1,1), (1,2), (2,1)}
R3  {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (4,1)(4,4)}
R4  {( 2,1), (3,1), (3,2), (4,1), (4,2), (4,3)}
R5  {(1,1), (1,2), (1,3), (1,4), (2,2)(2,3), (2,4), (3,3), (3,4), (4,4)}
R6  {(3,4)}
Whichof theserelationsare symmetricand which are antisymmetric?
Solution: R2 and R3 are symmetric,R4 , R 5 and R6 are antisymmet
ric.
Example 10. Consider the following relations on the set of integers.
R1  {( a, b) | a  b}
R2  {( a, b) | a  b}
R3  {( a, b) | a  b or a  b}
R4  {( a, b) | a  b}
R5  {( a, b) | a  b  1}
R6  {( a, b) | a  b  3}
Whichof theserelationsare symmetricand which are antisymmetric?
Solution : R3 , R4 and R6 are symmetric,and R1 , R2 , R4 and R5
are antisymmetric.
Example 11. Is the “divides” relation on the set of a positive
integers symmetric? Is it antisymmetric?
Solution: It is not symmetric, since 1|2, but 2|1. It is antisymmetric,
since for any positive integers a and b with a|b and b|a, then a=b.
Definition5. A relationR on a set A is called transitive
if whenever(a,b)  R and (b,c)  R, then(a,c)  R, for any a,b,c A.
Example 12. Consider the following relations on {1,2,3,4}
R1  {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)}
R2  {(1,1), (1,2), (2,1)}
R3  {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (4,1)(4,4)}
R4  {( 2,1), (3,1), (3,2), (4,1), (4,2), (4,3)}
R5  {(1,1), (1,2), (1,3), (1,4), (2,2)(2,3), (2,4), (3,3), (3,4), (4,4)}
R6  {(3,4)}
Whichof t heserelat ionsare t ransit ive?
Solution: R4 , R5 and R6 are transitive. Theothersare not.
Example 13. Consider the following relations on the set of integers.
R1  {( a, b) | a  b}
R2  {( a, b) | a  b}
R3  {( a, b) | a  b or a  b}
R4  {( a, b) | a  b}
R5  {( a, b) | a  b  1}
R6  {( a, b) | a  b  3}
W hichof t heserelat ionsare t ransit ive?
Solution: R1, R2 , R3 and R4 are transitive, and othersare not.
Example 14. Is the “divides” relation on the set of a positive
integers transitive?
Solution: It is transitive, since a|b and b|c, then a|c, for any positive
integers a,b,c.
6.5 Equivalence Relations
Definition 1. A relation on a set A is called an equivalence
relation if it is reflexive, symmetric and transitive.
•Two elements are related by an equivalence relation are called
equivalent.
Example1 Let A be a set of stringsof English lettersand l(x) be thelength
of thestring x. Let a relationR  {(a,b) | a,b A and l(a) l(b)}.
Is R an equivalence relation?
Solution: R is reflexive, symmetric, and transitive, it is an
equivalence relation.
Example 2 Let R be the relation on the set of real numbers such
that aRb if and only if a=b or a=-b. In Section 6.1, we showed that
R is reflexive, symmetric, and transitive. It follows that R is an
equivalence relation.
Example 3. Let R be the relation on the set of real numbers such that
aRb if and only if a-b is an integer. Is R an equivalence relation?
Solution: R is reflexive, symmetric and transitive. It is an equivalence
relation.
Example4. CongruenceModulo m
Let m be a positiveintegergreater than 1.Show that therelation
R  {(a,b) | a b(mod m)}
is an equivalence relationon theset of integers.
Solution :
It is reflexive,since a  a(mod m).
It is symmetric,since if a  b(modm) thenb  a(modm).
It is transitive, since if a  b(modm) and b  c(modm), thena  c(modm).
6.6 Partial Orderings
Definition 1 A relation R on a set S is called a partial ordering or partial
order if it is reflexive, antisymmetric, and transitive. A set S together with
a partial ordering R is called a partially ordered set, and denoted by (S,R).
A partial order R is also denoted as .
Example 1
Show that the “greater than or equal” relation ( ) is a partial ordering
one the set of integers.
Example 2
Show that the divisibility relation (|) is a partial ordering on the set
of positive integers.
Example 3
Show that the inclusion relation (  ) is a partial ordering on the
power set of a set S.
Definition 2 The elements a and b of a partial ordering set (S, ) is
called comparable if either a b or b a. When a and b are elements
of S such that neither a b nor b a, a and b are called
incomparable.
Example 4
In thepartialorderingset (Z , |), are theintegers3 and 9 comparable?
Are 5 and 7 comparable?
Definition 3 If (S, ) is a partial ordering set and every two elements of S
are comparable, S is called a totally ordered or linearly ordered set, and
is called a total order or a linear order.
Example 5
T hepartialorderingset (Z, ) is totallyordered.
Example 6
Thepartialorderingset (Z , |) is not totall
y ordered.
6.7 n-ary Relations and Database
Definition 1
Let A1 , A2 ,..., An be the sets. An n-ary relation on these sets is a subset of
A1  A2  An .
A1 , A2 ,..., An are called as the domains of the relation and n is
called as its degree.
A database consists of records, which are n-tuples, made up of fields.
Example 1
Primary key (no same
value in the domain)
A student record are represented by 4-tuples
of the form (student name, ID, Major, GPA).
A sample database of six such record is:
(Ackermann, 231455, Computer Sci, 3.88)
(Adams, 888323, physics, 3.45)
(Chou, 102147, Computer Sci, 3.49)
(Goodfriend, 453876, Mathematics, 3.45)
(Rao, 678543, Mathematics, 3.90)
(Stevens, 786576, Psychology, 2.99)
Table 1 Students
Student_name
ID
Major
GPA
Ackermann
Adams
Chou
Goodfriend
Rao
Stevens
231455
888323
102147
453876
678543
786576
Computer Sci
Physics
Computer Sci
Mathematics
Mathematics
Psychology
3.88
3.45
3.49
3.45
3.90
2.99
Operations on n-ary relations
Definition 2
Let R be an n-ary relation and C a condition that elements in R may satisfy.
Then the selection operator SC maps the n-ary relation R to the n-ary relation
of all n-tuples from R that satisfy the condition C.
Student_name
ID
Major
GPA
Ackermann
Adams
Chou
Goodfriend
Rao
Stevens
231455
888323
102147
453876
678543
786576
Computer Sci
Physics
Computer Sci
Mathematics
Mathematics
Psychology
3.88
3.45
3.49
3.45
3.90
2.99
SC
(C: GPA>3.5)
Student_name
ID
Major
GPA
Ackermann
Rao
231455
678543
Computer Sci
Mathematics
3.88
3.90
Definition 3
The projection Pi ,i ,...,i maps the n-tuple (a1, a2 ,...,an ) to the m-tuple
1 2
m
(ai1 , ai2 ,...,aim ) , where m  n.
Student_name
ID
Major
GPA
Ackermann
Adams
Chou
Goodfriend
Rao
Stevens
231455
888323
102147
453876
678543
786576
Computer Sci
Physics
Computer Sci
Mathematics
Mathematics
Psychology
3.88
3.45
3.49
3.45
3.90
2.99
P1, 2, 4
Student_name
ID
GPA
Ackermann
Adams
Chou
Goodfriend
Rao
Stevens
231455
888323
102147
453876
678543
786576
3.88
3.45
3.49
3.45
3.90
2.99
Definition 4
Let R be a relation of degree m and S a relation of degree n. The join J p (R, S ),
where p  m and p  n, is a relation of degree m+n-p that consist of all (m+n-p)tuples (a1, a2 ,...,am p , c1, c2 ,...,c p , b1, b2 ,...,bn p ), where (a1, a2 ,...,am p , c1, c2 ,...,c p ) belongs to R
and (c1, c2 ,...,c p , b1, b2 ,...,bn p ) belongs to S.
R
Student_name
ID
GPA
ID
Major
GPA
Ackermann
Adams
Chou
Goodfriend
Rao
Stevens
231455
888323
102147
453876
678543
786576
3.88
3.45
3.49
3.45
3.90
2.99
231455
888323
102147
453876
678543
786576
Computer Sci
Physics
Computer Sci
Mathematics
Mathematics
Psychology
3.88
3.45
3.49
3.45
3.90
2.99
J 2 ( R, S )
Student_name
ID
Major
GPA
Ackermann
Adams
Chou
Goodfriend
Rao
Stevens
231455
888323
102147
453876
678543
786576
Computer Sci
Physics
Computer Sci
Mathematics
Mathematics
Psychology
3.88
3.45
3.49
3.45
3.90
2.99
S
Chapter 7
Graphs
Definition 1. (1) A simple undirected graph G=(V,E) consists of V,
a nonempty set of vertices, and E, a set of unordered pairs of
distinct elements of V called edges. (2) If more than one edges are
allowed between two vertices, G is called multigraph. (3) If there is
any edge connection the same vertex, G is called pseudograph.
(1) Simple undirected graph
San Francisco
Los Angeles
Detroit
Chicago
Denver
New York
Washington
V={San Francisco, Los Angles, Denver, Chicago, Detroit, Washington, New York}
E={(San Francisco, Los Angeles), (San Francisco,Denver), (Los Angeles,Denver),
(Denver,Chicago), (Chicago,Detroit), (Chicago,New York), (Chicago,Washington),
(Detroit,New York), (Washington,New York)}
San Francisco
Detroit
Chicago
New York
Denver
Washington
Los Angeles
Detroit
San Francisco
Chicago
New York
(3) pseudograph
Denver
Los Angeles
Washington
(2) multigraph
Definition 2. A directed graph G=(V,E) consists of V, a
nonempty set of vertices, and E, a set of ordered pairs of
distinct elements of V called edges.
San Francisco
Denver
Los Angeles
Chicago
Detroit
New York
Washington
Example 1 Precedence Graphs and Concurrent Processing
A
a:=0
B
b:=1
C
c:=a+1
D
d:=b+a
E
e:=d+1
F
e:=c+d
F
E
C
D
A
B
Definition 3.
1. Let e=(u,v)be an edge of Graph G (undirected or directed).
(i)Vertices u and v are called adjacent in G.
(ii)Edge e is called incident with (or connect) the vertices u and v.
2. The degree of a vertex v in an undirected graph is the number of
edges incident with it, denoted by deg(v).
3. Let e=(u,v) be an edge of a directed graph G. The vertex u is
called the initial vertex of (u,v), and v is called the terminal
vertex of (u,v).
4. Let v be a vertex of directed graph G. The in-degree of v is the
number of edges with v as their terminal vertex. The out-degree
of v is the number of edges with b as their initial vertex.
Connectivity
Definition 4. Given a graph G=(V,E), a path of G is a sequence of
vertices x0 , x1 ,...,xn , where (x0 , x1 ), ( x1 , x2 ), ( x2 , x3 ),....,( xn1 , xn )  E.
E
F
C
D
A
B
H
I
E
F
C
D
A
B
H
ACFEH is a simple path.
ACFEHBF is a pathbut not simple.
FEHBF is a circle.
I
FADBH is a simple path.
FADBHEB is a path but not simple.
FADBHEF is a circle.
Definition 5. An undirected graph is called connected if there is
a path between every pair of distinct vertices of the graph.
E
F
C
D Connected
graph
B
A
H
E
F
I
C
D
A
B
H
Unconnected
graph
I
Definition 6. A directed graph is strongly connected if there is a
path from a to b whenever a and b are the vertices in the graph.
F
C
E
D
A
Strongly
connected
F
C
A
E
Not
Strongly
D connected
Chapter 8 Trees
8.1 Introduction to tree
Definition 1. A tree is a connected undirected graph with
no simple circuits.
Example 1
Which of the graphs are trees?
Theorem 1 Au undirected graph is a tree if and only if there is a unique
simple path between any two of its vertices.
Ordered rooted tree
root
parent
subtree
third child
Binary tree (every node has at most two children)
Left child
Right child
Right subtree
Left subtree
8.3 Tree Traversal
Ordered rooted trees are used to store information. We need
procedures for visiting each vertex of and ordered rooted tree
to access data.
Root of T
15
Definition1
Let T be an orderedroot edtreewit h root r.
6
18
19
If T containsonly of r , then r is thepreorder
traversalof T .
Otherwise,supposing thatT1 , T2 ,...,Tn are
thesubtrees at r fromleft t oright,
the preorder raversal
t
of T begins by
(1) visit ingr , then
(2) traversing T1 in preorder, then
(3) traversing T 2 in preorder,then
... untilTn is traversedin preorder.
9
30
7
21 4 13
17
1 20
19
19 4
5
11
The preorder traversal of T
15,6,9,30,21,4,7,13,11,18,17,1,
19,20,19,19,4,5
Definition2
Root of T
15
Let T be an orderedrootedtree with root r.
If T containsonly of r , then r is
6
18
theinorder traversalof T .
Otherwise,supposing thatT1 , T2 ,...,Tn are
thesubtrees at r fromleft toright,
9
30
7
21 4 13
17
19
1 20
19
19 4
5
theinorder traversalof T begins by
(1) traversing T1 in inorder, then
(2) visitingr, then
(3) traversing T2 in inorder, then
(4) traversing T 3 in inorder,then
... untilTn is traversedin inorder.
11
The inorder traversal of T
9,6,21,30,4,11,13,7,15,17,18,1,
20,19,19,19,4,5
Definition3
Let T be an orderedrootedtreewith root r.
Root of T
15
If T containsonlyof r , then r is
thepostordertraversalof T .
Otherwise,supposing thatT1 , T2 ,...,Tn are
thesubtrees at r fromleft toright,
thepostordertraversalof T begins by
(1) traversing T1 in postorder,then
(2) traversing T2 in postorder, then
... untilTn is traversedin postorder,then
(3) visitingr.
6
9
30
18
7
21 4 13
17
19
1 20
19
19 4
5
11
The postorder traversal of T
9,21,4,30,11,13,7,6,17,1,18,20,
19,4,5,19,19,15
8.2 Application of Trees
Binary Search Tree
Definition 1 Binary Search Tree is a Binary Tree satisfying the following
condition:
(1) Each vertex contains an item with an key which belongs to a total
ordering set and two links to its left child and right child, respectively.
(2) In each node, its key is larger than the keys of all vertices in its left
subtree and smaller than the keys of all the vertices in its right subtree.
Operations in a BST
1. Search (given a key return the corresponding data
if it is in the BST)
2. Insert (given a data insert it into the BST)
3. Delete (given a key delete the corresponding data
from BST)
root
key
15
6
3
1
18
7
17
13
4
9
20
Define a node of BST
A node of BST: item, right child, left child
item
(key/other data)
class Node {
item Item;
Node Left;
Node Right;
}
Right
left
root
A BST
65/data
struct item {
int id;
key
string name;
float grade;
}
30/data
25/data
75/data
45/data
/
20/data
/ /
40/data
68/data
/ /
55/data
/
35/data
/
42/data
/ /
38/data
36/data
/
/
39/data
/
/
60/data
/ /
80/data
78/data
/ /
88/data
/ /
root
A BST
65/data
30/data
25/data
75/data
45/data
/
20/data
/ /
Visit the nodes of BST in-order
40/data
68/data
/ /
55/data
/
35/data
/
42/data
/
/
80/data
78/data
/ /
88/data
/ /
60/data
/ /
Procedure InOrder(Node root)
{
if (root ≠ null))
{
InOrder(root.Left);
visit root;
InOrder(root.Right);
}
}
38/data
36/data
/
/
39/data
/
/
A BST can be visited in three ways: in-order, preorder and post-order
In-order visit in the BST (only show
keys):
20,25,30,35,36,38,39,40,42,45,55,60,65,
68,75,78,80,88
Operation 1: Search
root
A BST
65/data
height
30/data
25/data
75/data
45/data
/
20/data
/ /
40/data
68/data
/ /
55/data
/
35/data
/
42/data
/
/
38/data
36/data
/
/
Example:
Search (42)
Search (64)
39/data
/
/
60/data
/ /
80/data
78/data
/ /
88/data
/ /
Procedure Search(int id)
{
Node x = root;
while (x ≠ null and x.Item.id ≠ key )
{
if (id < x.Item.id) x = x.Left;
else x = x.Right;
}
return x; /* x.id = key or x = null */
}
Time Complexity
O(height of BST)
Average case: O(lg n)
Worst case O(n)
where n is the size of the BST
Operation 2: Insert
root
current
65/data parent
A BST
curr
paren
height
30/data
75/data
curr
paren
25/data
45/data
68/data
80/data
/
/ /
curr
paren
20/data
40/data
55/data
78/data
88/data
/ /
/
/ /
/ /
35/data
/
42/data
/
/
curr
60/data paren
/ / curr = null
38/data
62/data
/ /
36/data
/
/
39/data
/
/
Procedure Insert( item newItem)
{
Node newNode: = new Node(newItem);
Node parent = null;
Node current = root;
while (current ≠ null)
{
parent: = current;
if (newNode.Item.id < current.Item.id)
current: = current.Left;
else current: = current.Right;
}
if (parent = null) root: = newNode;
else
{
if (newNode.Item.id < parent.Item.id)
parent.Left: = newNode;
else parent.Right: = newNode;
}
}
Time Complexity
Insert
O(height of BST)
1. Create a new node for new item (e.g., 62/data)
Average case: O(lg n)
2. Find the place for insertion
Worst case: O(n)
where n is the size of the BST
3. Link the new node
Operation 3: Delete
Case 1: Deleting node (current) has no child
root
A BST
65/data
height
30/data
25/data
75/data
45/data
/
20/data
/ /
Example: Delete the node with item (36/data)
40/data
68/data
/ /
55/data
/
35/data
/
42/data
/
/
80/data
78/data
/ /
88/data
/ /
60/data
/ /
38/data parent
36/data
/
/
39/data current
/
if (current.Left = null and current.Right: = null)
{
if (current = root) root: = null;
else
if (parent.left = current) parent.Left: = null;
else parent.Right = null;
}
/
Delete
1. Find the deleting node (current) and its parent
2. Delete current
The first step is similar to Search. We only
consider the step of delete current.
Time Complexity
1. Find the deleting node (current) and its
parent: O(height of BST)
2. Delete current: O(1)
Therefore, the time complexity for case 1 is
O(height of BST)
Average case: O(lg n)
Worst case: O(n)
where n is the size of the BST
Case 2: Deleting node (current) has one child
Operation 3: Delete
root
ABST
Example: delete the node with item (35/data)
if (current.Left = null or current.Right = null)
{
if (current.Right = null)
30/data
75/data
height
if (current = root) root: = current.Left;
else if (parent.Left = current)
25/data
45/data 68/data
80/data
parent.Left: = current.Left;
/
/ /
else
parent
parent.Right: = current.Left;
20/data
40/data 55/data
78/data 88/data
/ /
else if (current.Left = null)
/
/ /
/ /
if (current = root) root: = current.Right;
current
else if (parent.Left = current)
60/data
35/data
42/data
parent.Left: = current.Right;
/
/ /
/ /
else
38/data
parent.Right: = current.Right;
}
Time Complexity
36/data 39/data
/ /
/ /
1. Find the deleting node (current) and its
parent: O(height of BST)
Delete
2. Delete current: O(1)
1. Find the deleting node (current) and its parent Therefore, the time complexity for case 1 is
O(height of BST)
2. Delete current
Average case: O(lg n)
The first step is similar to Search. We only
Worst case: O(n)
consider the step of delete current.
where n is the size of the BST
65/data
Operation 3: Delete
Case 3: Deleting node (current) has two
children
root
ABST
65/data
Example: delete the node with item (30/data)
current
height
75/data
30/data
25/data
45/data
/
20/data
/ /
68/data
/ /
Succ Parent
40/data 55/data
/
Step 1 and 2 are similar to Search. We only
consider step 3 and 4.
80/data
78/data
/ /
88/data
/ /
if (!(successor == current.Right))
sucessorParent.Right = successor.Right;
current.item = successor.item
successor
Time Complexity
/
1. Find the deleting node (current) and its
/ /
parent: O(height of BST)
38/data
2. Find successor and successor’s parent:
O(height of BST)
3. Link successor’s parent to successor.Right:
36/data 39/data
O(1)
/ /
/ /
4. Replace the current.item by successor.item:
Delete
O(1)
1. Find the deleting node (current) and its parent
Therefore, the time complexity for case 1 is
2. Find successor which is the node with the smallest key O(height of BST)
in current’s left, and successor’s parent.
Average case: O(lg n)
3. Link successorParent to successor.Right
Worst case: O(n)
4. Replace the current.item by successor.item
where n is the size of the BST
35/data
42/data
60/data
/ /
Example 1
Form a binary search tree for the words mathematics, physics, geography,
zoology, meteorology, geology, psychology, and chemistry (using alphabetical
order).
Example 2
Find the item with key Zoology in the tree of Example 1.
Example 3
Insert the item with key English in the tree of Example 2.
Example 4
Delete the item with key English in the tree of Example 3.