Binary Search Trees

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Binary Search Trees as PDF for free.

More details

  • Words: 790
  • Pages: 2
Dictionaries

4/5/2002 15:1

Ordered Dictionaries Binary Search Trees

order. Š New operations:

6

< 2

Š Keys are assumed to come from a total

4 =

1

„

9

>

„

8

„ „

Binary Search Trees

closestKeyBefore(k) closestElemBefore(k) closestKeyAfter(k) closestElemAfter(k)

1

Binary Search Trees

Binary Search (§3.1.1)

2

Lookup Table (§3.1.1)

Š Binary search performs operation findElement(k) on a dictionary

implemented by means of an array-based sequence, sorted by key „ „ „

Š A lookup table is a dictionary implemented by means of a sorted sequence

similar to the high-low game at each step, the number of candidate items is halved terminates after O(log n) steps

„

„

Š Example: findElement(7)

Š Performance: „

0

1

3

4

5

7

8

9

11

14

16

18

19

m

l 0

1

l

3

4

5

7

m

h

8

9

11

14

16

18

19

8

9

11

14

16

18

19

8

9

11

14

16

18

19

h

0

1

3

4

5

7

l

m

h

0

1

3

4

5

7

l=m =h Binary Search Trees

Š A binary search tree is a binary tree storing keys (or key-element pairs) 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)

„

„

findElement takes O(log n) time, using binary search insertItem takes O(n) time since in the worst case we have to shift n/2 items to make room for the new item removeElement take O(n) time since in the worst case we have to shift n/2 items to compact the items after the removal

Š The lookup table is effective only for dictionaries of small size or for dictionaries on which searches are the most common operations, while insertions and removals are rarely performed (e.g., credit card authorizations)

3

Binary Search Tree (§3.1.2)

„

We store the items of the dictionary in an array-based sequence, sorted by key We use an external comparator for the keys

Binary Search Trees

4

Search (§3.1.3) Š An inorder traversal of a binary search trees visits the keys in increasing order 6 2 1

9 4

8

Š External nodes do not store items

Binary Search Trees

5

Š To search for a key k,

we trace a downward path starting at the root Š The next node visited depends on the outcome of the comparison of k with the key of the current node Š If we reach a leaf, the key is not found and we return NO_SUCH_KEY Š Example: findElement(4)

Algorithm findElement(k, v) if T.isExternal (v) return NO_SUCH_KEY if k < key(v) return findElement(k, T.leftChild(v)) else if k = key(v) return element(v) else { k > key(v) } return findElement(k, T.rightChild(v))

< 2 1

Binary Search Trees

6 9

> 4 =

8

6

Dictionaries

4/5/2002 15:1

Insertion (§3.1.4)

Deletion (§3.1.5) 6

<

Š To perform operation

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

2

>

1

4

removeElement(k), we search for key k Š Assume key k is in the tree, and let let v be the node storing k Š If node v has a leaf child w, we remove v and w from the tree with operation removeAboveExternal(w) Š Example: remove 4

8

> w 6

2

9

1

4

8

w

5

7

Deletion (cont.) Š We consider the case where

„ „

we find the internal node w that follows v in an inorder traversal we copy key(w) into node v we remove node w and its left child z (which must be a leaf) by means of operation removeAboveExternal(z)

Š Example: remove 3

3

4 v

w

8 5

6 2

Binary Search Trees

9 5

8

8

Š Consider a dictionary

v

2

8 6

w

9

5

with n items implemented by means of a binary search tree of height h „

z

„

1 5

v

2

the space used is O(n) methods findElement , insertItem and removeElement take O(h) time

Š The height h is O(n) in

8 6

Binary Search Trees

9

>

1

Performance (§3.1.6) 1

the key k to be removed is stored at a node v whose children are both internal „

2

1

Binary Search Trees

6

<

Š To perform operation 9

9

9

the worst case and O(log n) in the best case

Binary Search Trees

10

Related Documents

Binary Search Trees
October 2019 21
Binary Trees
October 2019 20
Binary Search
July 2020 13
Binary Search
October 2019 21
Binary Search
May 2020 11