/
Binary Search Trees 1 Binary Search Trees Binary Search Trees 1 Binary Search Trees

Binary Search Trees 1 Binary Search Trees - PowerPoint Presentation

bagony
bagony . @bagony
Follow
343 views
Uploaded On 2020-06-22

Binary Search Trees 1 Binary Search Trees - PPT Presentation

6 9 2 4 1 8 lt gt 2014 Goodrich Tamassia Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java 6 th edition by M T Goodrich R Tamassia and M H Goldwasser Wiley 2014 ID: 783537

binary search trees key search binary key trees 2014 goodrich tamassia goldwasser node items tree remove case ordered treesearch

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Binary Search Trees 1 Binary Search Tree..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Binary Search Trees

1

Binary Search Trees

6

9

2

4

1

8

<

>

=

© 2014 Goodrich, Tamassia, Goldwasser

Presentation for use with the textbook

Data Structures and Algorithms in Java, 6

th

edition

, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014

Slide2

Binary Search Trees

2

Ordered Maps

Keys are assumed to come from a total order.

Items are stored in order by their keysThis allows us to support nearest neighbor queries:

Item with largest key less than or equal to kItem with smallest key greater than or equal to k

© 2014 Goodrich, Tamassia, Goldwasser

Slide3

Binary Search Trees

3

Binary Search

Binary search can perform nearest neighbor queries on an ordered map that is implemented with an array, sorted by key

similar to the high-low children’s gameat each step, the number of candidate items is halvedterminates after O(log n) steps

Example: find(7)

1

3

4

5

7

8

9

11

14

1618

19

1

3

4

5

7

8

9

11

14

16

18

191345

78911141618

191345

789111416

18

19

0

0

0

0

m

l

h

m

l

h

m

l

h

l

=

m

=

h

© 2014 Goodrich, Tamassia, Goldwasser

Slide4

Binary Search Trees

4

Search Tables

A search table is an ordered map implemented by means of a sorted sequence

We store the items in an array-based sequence, sorted by keyWe use an external comparator for the keysPerformance:Searches take

O(log n) time, using binary search

Inserting a new item takes O(n) time, since in the worst case we have to shift

n/2 items to make room for the new item

Removing an item takes O(n

) time, since in the worst case we have to shift n/2

items to compact the items after the removalThe lookup table is effective only for ordered maps of small size or for maps on which searches are the most common operations, while insertions and removals are rarely performed (e.g., credit card authorizations)

© 2014 Goodrich, Tamassia, Goldwasser

Slide5

Binary Search Trees

5

Binary Search Trees

A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property:

Let u, v, and

w be three nodes such that u is in the left subtree of v and

w is in the right subtree of v. We have key(

u) 

key(v) 

key(w)

External nodes do not store items

An inorder traversal of a binary search trees visits the keys in increasing order

6

9

2

4

1

8

© 2014 Goodrich, Tamassia, Goldwasser

Slide6

Binary Search Trees

6

Search

To search for a key k

, we trace a downward path starting at the rootThe next node visited depends on the comparison of k with the key of the current nodeIf we reach a leaf, the key is not found

Example: get(4):

Call TreeSearch(4,root)The algorithms for nearest neighbor queries are similar

Algorithm

TreeSearch

(

k

,

v

)

if

T.isExternal (

v) return

v

if k

< key(

v)

return TreeSearch

(k,

left(v

))else if

k =

key(v)

return velse { k > key(v) }

return TreeSearch(k, right(v))69

24

1

8

<

>

=

© 2014 Goodrich, Tamassia, Goldwasser

Slide7

Binary Search Trees

7

Insertion

To perform operation put

(k, o), we search for key k (using TreeSearch)Assume k is not already in the tree, and let w be the leaf reached by the searchWe insert k at node w and expand w into an internal nodeExample: insert 5

6

9

2

4

1

8

6

9

2

4

1

8

5

<

>

>

w

w

© 2014 Goodrich, Tamassia, Goldwasser

Slide8

Binary Search Trees

8

Deletion

To perform operation remove

(k), we search for key kAssume key k

is in the tree, and let let v be the node storing kIf node v

has a leaf child w, we remove v and w from the tree with operation

removeExternal(w), which removes

w and its parentExample: remove 4

6

9

2

4

1

8

5

v

w

6

9

2

5

1

8

<

>

© 2014 Goodrich, Tamassia, Goldwasser

Slide9

Binary Search Trees

9

Deletion (cont.)

We consider the case where the key k to be removed is stored at a node

v whose children are both internalwe find the internal node w that follows

v in an inorder traversalwe copy key(w

) into node vwe remove node w and its left child

z (which must be a leaf) by means of operation removeExternal(

z)Example: remove 3

3

1

8

6

9

5

v

w

z

2

5

1

8

6

9

v

2

© 2014 Goodrich, Tamassia, Goldwasser

Slide10

Binary Search Trees

10

Performance

Consider an ordered map with n

items implemented by means of a binary search tree of height hthe space used is O

(n)methods get

, put and remove take

O(h) time

The height h is O(

n) in the worst case and O(log

n) in the best case

© 2014 Goodrich, Tamassia, Goldwasser