ADT for Map:

31
Binary Search Trees 1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. •Each element (entry) is a key-value pair (k, v), where k is the key and v can be any object to store additional information. •Each key is unique. (different entries have different keys.) Map support the following methods: Size(): Return the number of entries in M isEmpty(): Test whether M is empty get(k); If M contains an entry e with key=k, then return e else return null. put(k, v): If M does not contain an entry with key=k then add (k, v) to the map and return null; else replace the entry with (k, v) and return the old value.

description

Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair (k, v), where k is the key and v can be any object to store additional information. Each key is unique. (different entries have different keys.) - PowerPoint PPT Presentation

Transcript of ADT for Map:

Page 1: ADT for Map:

Binary Search Trees 1

ADT for Map:Map stores elements (entries) so that they can be located quickly using keys.

•Each element (entry) is a key-value pair (k, v), where k is the key and v can be any object to store additional information.

•Each key is unique. (different entries have different keys.)

Map support the following methods:

Size(): Return the number of entries in M

isEmpty(): Test whether M is empty

get(k); If M contains an entry e with key=k, then return e else return null.

put(k, v): If M does not contain an entry with key=k then add (k, v) to the map and return null; else replace the entry with (k, v) and return the old value.

Page 2: ADT for Map:

Binary Search Trees 2

Methods of Map (continued)remove(k): remove from M the netry with key=k and return its value; if M has no such entry with key=k then return null.

keys(); Return an iterable collection containing all keys stored in M

values(): Return an iterable collection containing all values in M

entries(): return an iterable collection containing all key-value entries in M.

Remakrs: hash table is an implementation of Map.

Page 3: ADT for Map:

Binary Search Trees 3

ADT for Dictionary:A Dictionary stores elements (entries).

•Each element (entry) is a key-value pair (k, v), where k is the key and v can be any object to store additional information.

•The key is NOT unique.

Dictionary support the following methods:

size(): Return the number of entries in D

isEmpty(): Test whether D is empty

find(k): If D contains an entry e with key=k, then return e else return null.

findAll(k): Return an iterable collection containing all entries with key=k.

insert(k, v): Insert an entry into D, returning the entry created.

remove(e): remove from D an enty e, returing the removed entry or null if e was not in D.

entries(): return an iterable collection of the key-value entries in D.

Page 4: ADT for Map:

Binary Search Trees 4

Part-F1

Binary Search Trees

6

92

41 8

Page 5: ADT for Map:

Binary Search Trees 5

Search Trees Tree data structure that can be used to implement a dictionary.find(k): If D contains an entry e with key=k, then return e else return null. findAll(k): Return an iterable collection containing all entries with key=k.insert(k, v): Insert an entry into D, returning the entry created.remove(e): remove from D an enty e, returing the removed entry or null if e was not in D.

Page 6: ADT for Map:

Binary Search Trees 6

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)

Different nodes can have the same key.

External nodes do not store items

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

6

92

41 8

Page 7: ADT for Map:

Binary Search Trees 7

Search To search for a key k, we trace a downward path starting at the rootThe next node visited depends on the outcome of the comparison of k with the key of the current nodeIf we reach a leaf, the key is not found and we return nullExample: find(4):

Call TreeSearch(4,root)

Algorithm TreeSearch(k, v)if T.isExternal (v)

return vif k key(v)

return TreeSearch(k, T.left(v))else if k key(v)

return velse { k key(v) }

return TreeSearch(k, T.right(v))

6

92

41 8

Page 8: ADT for Map:

Binary Search Trees 8

InsertionTo perform operation insert(k, o), we search for key k (using TreeSearch)

Algorithm TreeINsert(k, x, v):Input: A search key, an associate

value x and a node v of T to start with

Output: a new node w in the subtree T(v) that stores the entry (k, x)

W TreeSearch(k,v)If k=key(w) then return TreeInsert(k, x,

T.left(w))T.insertAtExternal(w, (k, x))Return

Example: insert 5Example: insert another 5?

6

92

41 8

6

92

41 8

5

w

w

Page 9: ADT for Map:

Binary Search Trees 9

DeletionTo perform operation remove(k), we search for key kAssume key k is in the tree, and 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 parent and replace v with the remaining child. Example: remove 4

6

92

41 8

5

vw

6

92

51 8

Page 10: ADT for Map:

Binary Search Trees 10

Deletion (cont.)We consider the case where the key k to be removed is stored at a node v whose children are both internal

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 removeExternal(z)

Example: remove 3

3

1

8

6 9

5

v

w

z

2

5

1

8

6 9

v

2

Page 11: ADT for Map:

Binary Search Trees 11

Deletion (Another Example)

3

1

8

6 9

4

v

w

z

2

4

1

8

6 9

v

2

5

5

Page 12: ADT for Map:

Binary Search Trees 12

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

the space used is O(n) methods find, insert

and remove take O(h) time

The height h is O(n) in the worst case and O(log n) in the best caseLater, we will try to keep h

=O(log n).Review the

past

Page 13: ADT for Map:

Binary Search Trees 13

Part-F2

AVL Trees

6

3 8

4

v

z

Page 14: ADT for Map:

Binary Search Trees 14

AVL Tree Definition (§ 9.2)

AVL trees are balanced.An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1.

88

44

17 78

32 50

48 62

2

4

1

1

2

3

1

1

An example of an AVL tree where the heights are shown next to the nodes:

Page 15: ADT for Map:

Binary Search Trees 15

Balanced nodes A internal node is balanced if the heights of its two children differ by at most 1. Otherwise, such an internal node is unbalanced.

Page 16: ADT for Map:

Binary Search Trees 16

Height of an AVL TreeFact: The height of an AVL tree storing n keys is O(log n).Proof: Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h.We easily see that n(1) = 1 and n(2) = 2For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2.That is, n(h) = 1 + n(h-1) + n(h-2)Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). Son(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by

induction),n(h) > 2in(h-2i)>2 {h/2 -1} (1) = 2 {h/2 -1}

Solving the base case we get: n(h) > 2 h/2-1

Taking logarithms: h < 2log n(h) +2Thus the height of an AVL tree is O(log n)

3

4 n(1)

n(2)

h-1 h-2

Page 17: ADT for Map:

Binary Search Trees 17

Insertion in an AVL TreeInsertion is as in a binary search treeAlways done by expanding an external node.Example: 44

17 78

32 50 88

48 62

54w

b=x

a=y

c=z

44

17 78

32 50 88

48 62

before insertion after insertionIt is no longer balanced

Page 18: ADT for Map:

Binary Search Trees 18

Names of important nodes

w: the newly inserted node. (insertion process follow the binary search tree method)The heights of some nodes in T might be increased after inserting a node.

Those nodes must be on the path from w to the root. Other nodes are not effected.

z: the first node we encounter in going up from w toward the root such that z is unbalanced.y: the child of z with higher height.

y must be an ancestor of w. (why? Because z in unbalanced after inserting w)

x: the child of y with higher height. x must be an ancestor of w. The height of the sibling of x is smaller than that of x.

(Otherwise, the height of y cannot be increased.) See the figure in the last slide.

Page 19: ADT for Map:

Binary Search Trees 19

Algorithm restructure(x): Input: A node x of a binary search tree T that has

both parent y and grand-parent z.Output: Tree T after a trinode restructuring.1. Let (a, b, c) be the list (increasing order) of

nodes x, y, and z. Let T0, T1, T2 T3 be a left-to-right (inorder) listing of the four subtrees of x, y, and z not rooted at x, y, or z.

2. Replace the subtree rooted at z with a new subtree rooted at b..

3. Let a be the left child of b and let T0 and T1 be the left and right subtrees of a, respectively.

4. Let c be the right child of b and let T2 and T3 be the left and right subtrees of c, respectively.

Page 20: ADT for Map:

Binary Search Trees 20

Restructuring (as Single Rotations)

Single Rotations:

T0T1

T2

T3

c = xb = y

a = z

T0 T1 T2

T3

c = xb = y

a = zsingle rotation

T3T2

T1

T0

a = xb = y

c = z

T3T2T1

T0

a = xb = y

c = zsingle rotation

Page 21: ADT for Map:

Binary Search Trees 21

Restructuring (as Double Rotations)

double rotations:

double rotationa = z

b = xc = y

T0T2

T1

T3 T0

T2T3T1

a = zb = x

c = y

double rotationc = z

b = xa = y

T3T1

T2

T0 T3

T1T0 T2

c = zb = x

a = y

Page 22: ADT for Map:

Binary Search Trees 22

Insertion Example, continued

88

44

17 78

32 50

48 62

2

5

1

1

3

4

2

1

54

1

T0T2

T3

x

y

z

2

3

4

5

67

1

88

44

17

7832 50

48

622

4

1

1

2 2

3

154

1

T0 T1

T2

T3

x

y z

unbalanced...

...balanced

1

2

3

4

5

6

7

T1

Page 23: ADT for Map:

Binary Search Trees 23

Theorem: One restructure operation is enough to ensure that the whole tree is balanced.

Proof: Look at the four cases on slides 20 and 21.

Page 24: ADT for Map:

Binary Search Trees 24

Removal in an AVL TreeRemoval begins as in a binary search tree by calling removal(k) for binary tree. may cause an imbalance.Example:

44

17

7832 50

8848

62

54

44

17

7850

8848

62

54

before deletion of 32 after deletion

w

Page 25: ADT for Map:

Binary Search Trees 25

Rebalancing after a Removal

Let z be the first unbalanced node encountered while travelling up the tree from w. w-parent of the removed node (in terms of structure, not the name)let y be the child of z with the larger height, let x be the child of y defined as follows;

If one of the children of y is taller than the other, choose x as the taller child of y.

If both children of y have the same height, select x be the child of y on the same side as y (i.e., if y is the left child of z, then x is the left child of y; and if y is the right child of z then x is the right child of y.)

The way to obtain x, y and z are different from insertion.

Page 26: ADT for Map:

Binary Search Trees 26

Rebalancing after a Removal

We perform restructure(x) to restore balance at z.As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached

44

17

7850

8848

62

54

c=x

b=y

a=z

44

17

78

50 88

48

62

54

w

Page 27: ADT for Map:

Binary Search Trees 27

Unbalanced after restructuring

44

17

7850

88

62w

c=x

b=y

a=z

44

17

78

50 88

62

32

1 1

h=5h=5h=3h=4

Unbalanced balanced

Page 28: ADT for Map:

Binary Search Trees 28

Rebalancing after a Removal

We perform restructure(x) to restore balance at z.As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached

44

17

7850

8848

62

54

w

c=x

b=y

a=z

44

17

78

50 88

48

62

54

Page 29: ADT for Map:

Binary Search Trees 29

Example a:Which node is w? Let us remove node 17.

44

17

7832 50

8848

62

54

44

32

7850

8848

62

54

before deletion of 32 after deletion

w

Page 30: ADT for Map:

Binary Search Trees 30

Rebalancing: We perform restructure(x) to restore balance at z.As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached

44

32

7850

8848

62

54

c=x

b=y

a=z

44

32

78

50 88

48

62

54

w

Page 31: ADT for Map:

Binary Search Trees 31

Running Times for AVL Trees

a single restructure is O(1) using a linked-structure binary tree

find is O(log n) height of tree is O(log n), no restructures needed

insert is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log

n)

remove is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log

n)