chapter 6 relations relationships are dealed with every day: businesses and their telephone numbers,...

37
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 . of subset a is to from relation binary A sets. be and Let 1. Definition B A B A R B A • From Definition 1, the binary relation R is a subset of } , | ) , {( B b A a b a . denote to and , denote to use we Sometimes, . by to related be to said is , When R (a,b) aRb R (a,b) aRb R b a R (a,b)

Upload: julia-allison

Post on 16-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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

. ofsubset a is

to from relation binary A sets. be and Let 1. Definition

BA

BARBA

• From Definition 1, the binary relation R is a subset of},|),{( BbAaba

. denote to and , denote to use weSometimes,

.by torelated be tosaid is ,When

R(a,b)aRbR(a,b)aRb

RbaR(a,b)

Page 2: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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.

} coursein enrolled student ,,A |),{( is,That baBbabaR

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.

1

0

2

a

b

Page 3: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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.

. as denoted isIt . to unique aassign ,each for is,That bf(a)aBbfAa

}.)(,,|),{( :relation a as considered becan bafBbAabaff

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.

1

0

2

a

b

R

1

0

2

a

b

R’

R’ is a function, but R is not a function.

Page 4: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Relations on A Set

Definition 2. A relation R on the set A is a relation from A to A. . ofsubset a is is,That A}A, b{(a,b) |aR

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.

(2,2)? and (1,-1)(2,1),(1,2),(1,1), ofeach contain relations theseofWhich

}3|),{(

}1|),{(

}|),{(

}or |),{(

}|),{(

}|),{(

6

5

4

3

2

1

babaR

babaR

babaR

bababaR

babaR

babaR

.)2,2(;)1,1(

;(2,1) ;(1,2);(1,1) :Solution

431632

652616431

,R,RR,R,RR

,R,RR,RR ,R,R,RR

Page 5: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Properties of Relations

.element every for

if reflexive called is set aon relation A 3. Definition

Aa

R(a,a)AR

Example 6. Consider the following relations on {1,2,3,4}

?reflextive are relations theseofWhich

)}4,3{(

)}4,4(),4,3(),3,3(),4,2(),3,2)(2,2(),4,1(),3,1(),2,1(),1,1{(

)}3,4(),2,4(),1,4(),2,3(),1,3(),1,2{(

)}4,4)(1,4(),3,3(),2,2(),1,2(),4,1(),2,1(),1,1{(

)}1,2(),2,1(),1,1{(

)}4,4(),1,4(),4,3(),2,2(),1,2(),2,1(),1,1{(

6

5

4

3

2

1

R

R

R

R

R

R

reflexive.not are Others e.reflexitiv are and :Solution 53 RR

Page 6: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Example 7. Consider the following relations on the set of integers.

reflexive? are relations theseofWhich

}3|),{(

}1|),{(

}|),{(

}or |),{(

}|),{(

}|),{(

6

5

4

3

2

1

babaR

babaR

babaR

bababaR

babaR

babaR

. and ,integer andfor since

,reflextive are and , :Solution

431

431

RRRa, (a,a)

RRR

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.

Page 7: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

. called is ,any for

, ifonly )( and )(such that set aon relation A

.any for

,)( whenever)( if called is set aon relation A

4. Definition

ricantisymmetAa,b

baRb,aRa,bAR

Aa,b

Ra,bRb,asymmetricAR

Example 9. Consider the following relations on {1,2,3,4}

ric?antisymmet are which and symmetric are relations theseofWhich

)}4,3{(

)}4,4(),4,3(),3,3(),4,2(),3,2)(2,2(),4,1(),3,1(),2,1(),1,1{(

)}3,4(),2,4(),1,4(),2,3(),1,3(),1,2{(

)}4,4)(1,4(),3,3(),2,2(),1,2(),4,1(),2,1(),1,1{(

)}1,2(),2,1(),1,1{(

)}4,4(),1,4(),4,3(),2,2(),1,2(),2,1(),1,1{(

6

5

4

3

2

1

R

R

R

R

R

R

ric.antisymmet are and R, symmetric, are and :Solution 65432 RRRR

Page 8: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Example 10. Consider the following relations on the set of integers.

ric?antisymmet are which and symmetric are relations theseofWhich

}3|),{(

}1|),{(

}|),{(

}or |),{(

}|),{(

}|),{(

6

5

4

3

2

1

babaR

babaR

babaR

bababaR

babaR

babaR

ric.antisymmet are

and,, and symmetric, are and , :Solution 5421643 R RRRRRR

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.

Page 9: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

.any for ,)( then ,)( and )( whenever if

e transitivcalled is set aon relation A 5. Definition

Aa,b,cRa,cRb,cRa,b

AR

Example 12. Consider the following relations on {1,2,3,4}

e? transitivare relations theseofWhich

)}4,3{(

)}4,4(),4,3(),3,3(),4,2(),3,2)(2,2(),4,1(),3,1(),2,1(),1,1{(

)}3,4(),2,4(),1,4(),2,3(),1,3(),1,2{(

)}4,4)(1,4(),3,3(),2,2(),1,2(),4,1(),2,1(),1,1{(

)}1,2(),2,1(),1,1{(

)}4,4(),1,4(),4,3(),2,2(),1,2(),2,1(),1,1{(

6

5

4

3

2

1

R

R

R

R

R

R

not. are others The e. transitivare and , :Solution 654 RRR

Page 10: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Example 13. Consider the following relations on the set of integers.

e? transitivare relations theseofWhich

}3|),{(

}1|),{(

}|),{(

}or |),{(

}|),{(

}|),{(

6

5

4

3

2

1

babaR

babaR

babaR

bababaR

babaR

babaR

.not are others and e, transitivare and ,, :Solution 4321 RRRR

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.

Page 11: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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.

relation? eequivalencan Is

}. and | ){(relation aLet . string theof

length thebe and lettersEnglish of strings ofset a be Let 1 Example

R

l(b) l(a)Aa,ba,bRx

l(x)A

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.

Page 12: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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.

integers. ofset on therelation eequivalencan is

)}mod({

relation that theShow 1.an greater thinteger positive a beLet

Modulo Congruence 4. Example

mb(a,b) | aR

m

m

).(mod then ,)(mod and )(mod if since e, transitivisIt

).(mod then )(mod if since symmetric, isIt

).mod(since reflexive, isIt

:Solution

mcamcbmba

mabmba

ma a

Page 13: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

6.6 Partial Orderings

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 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 .

Page 14: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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

?comparable 7 and 5 Are

?comparable 9 and 3 integers theare |),,(Zset ordering partial In the

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

ordered. totally is ) (Z,set ordering partial The

Example 6

ordered.y not totall is |),(Zset ordering partial The

Page 15: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

6.7 n-ary Relations and Database

Definition 1

Let be the sets. An n-ary relation on these sets is a subset of

are called as the domains of the relation and n is called as its degree.

nAAA ,...,, 21

.21 nAAA nAAA ,...,, 21

A database consists of records, which are n-tuples, made up of fields.

Example 1

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)

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

Table 1 Students

Primary key (no same value in the domain)

Page 16: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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

Student_name ID Major GPA

Ackermann

Rao

231455

678543

Computer Sci

Mathematics

3.88

3.90

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 maps the n-ary relation R to the n-ary relation of all n-tuples from R that satisfy the condition C.

CS (C: GPA>3.5)

CS

Page 17: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Definition 3

The projection maps the n-tuple to the m-tuple

, where miiiP ,...,, 21

),...,,( 21 naaa

),...,,(21 miii aaa .nm

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

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

4,2,1P

Page 18: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Definition 4

Let R be a relation of degree m and S a relation of degree n. The join where is a relation of degree m+n-p that consist of all (m+n-p)-tuples , where belongs to R and belongs to S.

),,( SRJ p

, and npmp ),...,,,,...,,,,...,,( 212121 pnppm bbbcccaaa ),...,,,,...,,( 2121 ppm cccaaa

),...,,,,...,,( 2121 pnp bbbccc

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

3.88

3.45

3.49

3.45

3.90

2.99

Computer Sci

Physics

Computer Sci

Mathematics

Mathematics

Psychology

231455

888323

102147

453876

678543

786576

GPAMajorID

3.88

3.45

3.49

3.45

3.90

2.99

231455

888323

102147

453876

678543

786576

Ackermann

Adams

Chou

Goodfriend

Rao

Stevens

GPAID Student_name

R S

),(2 SRJ

Page 19: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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.

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)}

(1) Simple undirected graph DetroitSan Francisco

Los AngelesDenver

ChicagoNew York

Washington

Detroit

(2) multigraphSan Francisco

Los AngelesDenver

ChicagoNew York

WashingtonDetroitSan Francisco

Los AngelesDenver

ChicagoNew York

Washington(3) pseudograph

Page 20: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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.

New YorkSan Francisco

Los Angeles Denver

ChicagoDetroit

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

A

F

C

B

D

E

Page 21: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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.

Page 22: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Connectivity

Definition 4. Given a graph G=(V,E), a path of G is a sequence of vertices .),(),....,,(),,(),,(x where,,...,, 132211010 Exxxxxxxxxx nnn

circle. a is

simple.not but path a is

path. simple a is

FEHBF

ACFEHBF

ACFEH

A

F

C

B

D

E

H I

A

F

C

B

D

E

H I

circle. a is

simple.not but path a is

path. simple a is

FADBHEF

FADBHEB

FADBH

Page 23: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Definition 5. An undirected graph is called connected if there is a path between every pair of distinct vertices of the graph.

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.

A

F

C

B

D

E

H I

Connected graph

A

F

C

B

D

E

H I

Unconnected graph

A

F

C D

EStrongly connected

A

F

C D

ENot Strongly connected

Page 24: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Chapter 8 Trees

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.

8.1 Introduction to tree

Page 25: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Ordered rooted tree root

subtree

parent

third child

Binary tree (every node has at most two children)

Left subtree

Right subtree

Left child Right child

Page 26: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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.

preorder.in traversedis until ...

then preorder,in g traversin(3)

then ,preorderin g traversin(2)

then , visiting(1)

by begins of raversalpreorder t the

right, left to from at subtrees the

are ,...,, that supposing Otherwise,

. of traversal

preorder theis then, ofonly contains If

root with treerooted orderedan be Let

1 Definition

2

1

21

n

n

T

T

T

r

T

r

TTT

T

rrT

r.T 6 18

30

4

7

1321

20179

15Root of T

19

1

4

19

19 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

Page 27: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

inorder.in traversedis until ...

then inorder,in g traversin(4)

then ,inorderin g traversin(3)

then, visiting(2)

then,inorder in ng traversi(1)

by begins of aversalinorder tr the

right, left to from at subtrees the

are ,...,, that supposing Otherwise,

. of aversalinorder tr the

is then, ofonly contains If

root with treerooted orderedan be Let

2 Definition

3

2

1

21

n

n

T

T

T

r

T

T

r

TTT

T

rrT

r.T

6 18

30

4

7

1321

20179

15Root of T

19

1

4

19

19 5

11

The inorder traversal of T9,6,21,30,4,11,13,7,15,17,18,1, 20,19,19,19,4,5

Page 28: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

. visiting(3)

then postorder,in traversedis until ...

then ,postorderin g traversin(2)

thenpostorder,in ng traversi(1)

by begins of traversalpostorder the

right, left to from at subtrees the

are ,...,, that supposing Otherwise,

. of traversalpostorder the

is then, ofonly contains If

root with treerooted orderedan be Let

3 Definition

2

1

21

r

T

T

T

T

r

TTT

T

rrT

r.T

n

n

6 18

30

4

7

1321

20179

15Root of T

19

1

4

19

19 5

11

The postorder traversal of T9,21,4,30,11,13,7,6,17,1,18,20,19,4,5,19,19,15

Page 29: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Operations in a BST1. 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)

8.2 Application of Trees

Binary Search TreeDefinition 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.

6 18

3

4

7

131

2017

9

15root

key

Page 30: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

item (key/other data)

Right left

A node of BST: item, right child, left childDefine a node of BST

class Node { item Item; Node Left; Node Right;}

struct item {int id;string name;float grade;}

key

65/data

30/data

75/data

25/data

/

45/data

40/data

35/data/

55/data/

42/data/ /

68/data/ /

80/data

78/data/ /

88/data/ /

60/data/ /

38/data

20/data/ /

39/data/ /

36/data/ /

rootA BST

Page 31: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

65/data

30/data

75/data

25/data

/

45/data

40/data

35/data/

55/data/

42/data/ /

68/data/ /

80/data

78/data/ /

88/data/ /

60/data/ /

38/data

20/data/ /

39/data/ /

36/data/ /

rootA BST

Visit the nodes of BST in-orderProcedure InOrder(Node root)

{ if (root ≠ null)) { InOrder(root.Left); visit root; InOrder(root.Right); } }

A BST can be visited in three ways: in-order, pre-order 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

Page 32: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

65/data

30/data

75/data

25/data

/

45/data

40/data

35/data/

55/data/

42/data/ /

68/data/ /

80/data

78/data/ /

88/data/ /

60/data/ /

38/data

20/data/ /

39/data/ /

36/data/ /

rootA BST

Operation 1: Search

Example:

Search (42)

Search (64)

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 */ }

Operation 1: Search

height

Time Complexity

O(height of BST)Average case: O(lg n)Worst case O(n) where n is the size of the BST

Page 33: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Insert 1.Create a new node for new item (e.g., 62/data)2.Find the place for insertion 3.Link the new node

Operation 2: Insert 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; } }

62/data/ /

root

65/data

30/data

75/data

25/data

/

45/data

40/data

35/data/

55/data/

42/data/ /

68/data/ /

80/data

78/data/ /

88/data/ /

60/data/ /

38/data

20/data/ /

39/data/ /

36/data/ /

A BST currentparent

currparen

currparen

currparen

currparen

curr = null

height

Time Complexity

O(height of BST)Average case: O(lg n)Worst case: O(n) where n is the size of the BST

Page 34: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Delete 1. Find the deleting node (current) and its parent2. Delete currentThe first step is similar to Search. We only consider the step of delete current.

Operation 3: Delete

65/data

30/data

75/data

25/data

/

45/data

40/data

35/data/

55/data/

42/data/ /

68/data/ /

80/data

78/data/ /

88/data/ /

60/data/ /

38/data

20/data/ /

39/data/ /

36/data/ /

rootA BST

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; }

Case 1: Deleting node (current) has no child

current

parent

height

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

Example: Delete the node with item (36/data)

Page 35: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Operation 3: Delete

65/data

30/data

75/data

25/data

/

45/data

40/data

35/data/

55/data/

42/data/ /

68/data/ /

80/data

78/data/ /

88/data/ /

60/data/ /

38/data

20/data/ /

39/data/ /

36/data/ /

rootABST if (current.Left = null or current.Right = null){ if (current.Right = null) if (current = root) root: = current.Left; else if (parent.Left = current) parent.Left: = current.Left; else parent.Right: = current.Left; else if (current.Left = null) if (current = root) root: = current.Right; else if (parent.Left = current) parent.Left: = current.Right; else parent.Right: = current.Right;}

Case 2: Deleting node (current) has one child

current

parent

Example: delete the node with item (35/data)

height

Delete 1. Find the deleting node (current) and its parent2. Delete currentThe 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

Page 36: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

Operation 3: Delete

65/data

30/data

75/data

25/data

/

45/data

40/data

35/data/

55/data/

42/data/ /

68/data/ /

80/data

78/data/ /

88/data/ /

60/data/ /

38/data

20/data/ /

39/data/ /

36/data/ /

rootABST

if (!(successor == current.Right)) sucessorParent.Right = successor.Right;current.item = successor.item

Case 3: Deleting node (current) has two children

current

Delete 1.Find the deleting node (current) and its parent2.Find successor which is the node with the smallest key in current’s left, and successor’s parent. 3.Link successorParent to successor.Right4.Replace the current.item by successor.item

successor

Succ Parent

35/data

height

Example: delete the node with item (30/data)

Step 1 and 2 are similar to Search. We only consider step 3 and 4.

Time Complexity 1.Find the deleting node (current) and its parent: O(height of BST)2.Find successor and successor’s parent: O(height of BST)3.Link successor’s parent to successor.Right: O(1) 4.Replace the current.item by successor.item: 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

Page 37: Chapter 6 Relations Relationships are dealed with every day: businesses and their telephone numbers, employees and their salaries, …. Relationships can

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.

Example 1

Form a binary search tree for the words mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry (using alphabetical order).