32
Binary Search Trees Binary Search Trees Lecture 5 1

Binary Search Trees Lecture 5 1. Binary search tree sort 2

Embed Size (px)

Citation preview

Page 1: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Binary Search TreesBinary Search Trees

Lecture 5

1

Page 2: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Binary search tree sortBinary search tree sort

2

Page 3: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Binary-search-tree propertyBinary-search-tree property

xyykeyxkey

xyxkeyykey

x

of subtreeright in the node a is if

of subtereeleft in the node a is if

esearch trebinary ain node a be Let

3

Page 4: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Binary-search-tree propertyBinary-search-tree property

• Binary Search Tree can be implemented as a linked data structure in which each node is an object with three pointer fields.

• The three pointer fields left, right and p point to the nodes corresponding to the left child, right child and the parent respectively.

• NIL in any pointer field signifies that there exists no corresponding child or parent.

• The root node is the only node in the BTS structure with

NIL in its p field.

4

Page 5: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Binary-search-tree propertyBinary-search-tree property

5

Page 6: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Inorder-tree walkInorder-tree walk

)()(

)WALK(-TREE-INORDER 4

print 3

)WALK(-TREE-INORDER 2

NIL 1

)WALK(-TREE-INORDER

nnT

xright

xkey

xleft

x

x

thenif

During this type of walk, we visit the root of a subtree between the left subtree visit and right subtree visit.

6

Page 7: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Preorder-tree walkPreorder-tree walk

)()(

)WALK(-TREE-DER PREOR 4

)WALK(-TREE-DER PREOR 3

print 2

NIL 1

)WALK(-TREE-PREORDER

nnT

xright

xleft

xkey

x

x

thenif

In this case we visit the root node before the nodes in either subtree.

7

Page 8: Binary Search Trees Lecture 5 1. Binary search tree sort 2

PPostostorder-tree walkorder-tree walk

)()(

print 4

)WALK(-TREE-RDER POSTO 3

)WALK(-TREE-RDER POSTO 2

NIL 1

)WALK(-TREE-POSTORDER

nnT

xkey

xright

xleft

x

x

thenif

We visit the root node after the nodes in its subtrees.

8

Page 9: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Sorting by binary-search-treeSorting by binary-search-tree 6

3 7

2 5 8

12

6

3

12

1 NIL

3 PRINT 24

NIL

3 PRINT 3

41 2 NIL3 PRINT 54 NIL

2

1 2 NIL3 PRINT 84 NIL

5

3 PRINT 64

1 2 NIL3 PRINT 74

12

77

8

)WALK(-TREE-INORDER 4

print 3

)WALK(-TREE-INORDER 2

NIL 1

)WALK(-TREE-INORDER

xright

xkey

xleft

x

x

then

if

Page 10: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Sorting by binary-search-tree Sorting by binary-search-tree

WALK-TREE-INORDER execute to timethe reflects where

)1()()(

Then nodes.

1 has right tree whoseand nodes has left tree whose

node aon called is WALK-TREE-INORDERthat Suppose

d

dknTkTnT

knk

x

10

Page 11: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Sorting by binary-search-treeSorting by binary-search-tree (contnd.) (contnd.)

)(

)(

)()(

))1)((())((

)1()()(

)()( that providingby methodon substituti Use

n

cndc

dcdccndc

dckndcckdc

dknTkTnT

cndcnT

11

Page 12: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for a keySearching for a key

15

6 18

3

2 4

7 17

13

20

9

Search for 13

12

Page 13: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for a keySearching for a key

),SEARCH(-TREE 5

),SEARCH(-TREE 4

3

2

or NILL 1

),SEARCH(-TREE

kxright

kxleft

xkeyk

x

xkeykeyx

kx

returnelse

returnthen

if

returnthen

if

)()( hOnT 13

Page 14: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for minimumSearching for minimum

15

6 18

3

2 4

7 17

13

20

9

Minimum

14

Page 15: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for minimumSearching for minimum

x

xleftx

xleft

x

return

do

while

3

][ 2

NILL ][ 1

)MINIMUM(-TREE

)()( hOnT 15

Page 16: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for maximumSearching for maximum

15

6 18

3

2 4

7 17

13

20

9

Maximum

16

Page 17: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for maximumSearching for maximum

x

xrightx

xright

x

return

do

while

3

][ 2

NILL ][ 1

)MAXIMUM(-TREE

)()( hOnT 17

Page 18: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for successorSearching for successor

The successor of a node x is the node with the smallest key greather than key[x]

18

Page 19: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for successorSearching for successor

15

6 18

3

2 4

7 17

13

20

9

Successor of 15

Case 1: The right subtree of node x is nonempty 19

Page 20: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for successorSearching for successor

15

6 18

3

2 4

7 17

13

20

9

Successor of 13

Case 2: The right subtree of node x is empty 20

Page 21: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for successorSearching for successor

15

6 18

3

2 4

7 17

13

20

9

Successor of 4search untily.left = x

Case 2: The right subtree of node x is empty 21

Page 22: Binary Search Trees Lecture 5 1. Binary search tree sort 2

Searching for successorSearching for successor

y

ypy

yx

yrightxy

xpy

xright

xright

x

return

do

while

returnthen

if

7

][ 6

5

][ and NILL 4

][ 3

])[(MINIMUMTREE 2

NILL ][ 1

)SUCCESSOR(-TREE

)()( hOnT 22

Go upper until y.left = x

Page 23: Binary Search Trees Lecture 5 1. Binary search tree sort 2

InsertionInsertion

12

5 18

2

13 17

9 15 19

Inserting an item with key 13 23

Page 24: Binary Search Trees Lecture 5 1. Binary search tree sort 2

InsertionInsertion

][ 13

][ 12

][][ 11

empty was Tree ][ 10

NILL 9

][ 8

][ 7

][ 6

][][ 5

4

NILL 3

][ 2

NILL 1

),(INSERTTREE

zyright

zyleft

ykeyzkey

TzTroot

y

yzp

xrightx

xleftx

xkeyzkey

xy

x

Trootx

y

zT

else

then

ifelse

then

if

else

then

if

do

while

)()( hOnT

24

Page 25: Binary Search Trees Lecture 5 1. Binary search tree sort 2

DeletionDeletion

15

5 16

2

18

12

23

20

Deleting an item with key 13 (z has no children)

10 13 z

5

3

6

7

25

Page 26: Binary Search Trees Lecture 5 1. Binary search tree sort 2

DeletionDeletion

15

5 16

2

18

12

23

20

Deleting an item with key 13 (z has no children)

10

5

3

26

6

7

Page 27: Binary Search Trees Lecture 5 1. Binary search tree sort 2

DeletionDeletion

15

5 16 z

2

18

12

23

20

Deleting a node with key 16 (z has only one child )

10 13

5

3

27

6

7

Page 28: Binary Search Trees Lecture 5 1. Binary search tree sort 2

DeletionDeletion

15

5

2 12

Deleting a node with key 16 (z has only one child )

10 13

5

318 23

20

28

6

7

Page 29: Binary Search Trees Lecture 5 1. Binary search tree sort 2

DeletionDeletion

15

5 16

2

18

12

23

20

Deleting a node with key 5 (6 successor of 5) (z has two children)

10

z 5

3

6 y

7

13

29

Page 30: Binary Search Trees Lecture 5 1. Binary search tree sort 2

DeletionDeletion

15

5 16

2

18

12

23

20

Deleting a node with key 5 (z has two children)

10

z 5

3

6 y

7

30

13

Page 31: Binary Search Trees Lecture 5 1. Binary search tree sort 2

DeletionDeletion

15

5 16

2

18

12

23

20

Deleting a node with key 5 (z has two children)

10

6

3

7

31

13

Page 32: Binary Search Trees Lecture 5 1. Binary search tree sort 2

DeletionDeletion

17

into data satellite s'copy 16

][][ 15

14

]][[ 13

]][[ 12

]][[ 11

][ 10

NIL][ 9

][][ 8

NIL 7

][ 6

][ 5

NIL][ 4

)SUCCESSOR(-TREE 3

2

NIL][or NIL][ 1

),(DELETETREE

return

then

if

else

then

ifelse

then

if

then

if

else

then

if

else

then

if

zy

ykeyzkey

zy

xypright

xypleft

yplefty

xTroot

yp

ypxp

x

yrightx

yleftx

yleft

zy

zy

zrightzleft

zT

)()( hOnT

32