## Binary Search Trees ( 9.1) Binary Search Trees 6. Insertion. Search ( 9.1.1) Deletion. Deletion (cont.)

Dictionaries 11/3/2004 11:57 AM Binary Search Trees (§ 9.1) A binary search tree is a binary tree storing keys (or key-value entries) at its interna...
Author: Godwin Harper
Dictionaries

11/3/2004 11:57 AM

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

Binary Search Trees 6

< 2 1



9

> 4 =

8

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

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)

2

9

1

4

8

External nodes do not store items © 2004 Goodrich, Tamassia

Binary Search Trees

1

Search (§ 9.1.1)

Call TreeSearch(4,root)

4 =

1

To perform operation insert(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 search We insert k at node w and expand w into an internal node Example: insert 5

8

Binary Search Trees

3

Deletion

2

6

< 2

9

>

1

4

8

> w 6

2

9

1

4

8

w 5

Binary Search Trees

4

Deletion (cont.)

To perform operation remove(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 removeExternal(w), which removes w and its parent Example: remove 4

Binary Search Trees

Insertion

Algorithm TreeSearch(k, v) To search for a key k, we trace a downward if T.isExternal (v) path starting at the root return v The next node visited if k < key(v) depends on the return TreeSearch(k, T.left(v)) outcome of the else if k = key(v) comparison of k with return v the key of the current else { k > key(v) } node return TreeSearch(k, T.right(v)) If we reach a leaf, the 6 key is not found and we < return null 2 9 Example: find(4): > 

6

< 2

> 4 v

1

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

9 8

w



5



6



2 1

Binary Search Trees

9 5

8

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

5

1

v 3 2

8 6

w z 1

v 5 2

8 6

Binary Search Trees

9

5

9

6

1

Dictionaries

11/3/2004 11:57 AM

Performance

Balanced Trees

Consider a binary search tree of height h with n items  

Binary search trees: if all levels filled, then search, insertion and deletion are O(log N). However, performance may deteriorate to linear if nodes are inserted in order:

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 case

10

11 12

Binary Search Trees

7

Solution

Binary Search Trees

9

How to keep the tree balanced

Binary Search Trees

10

Level 0 7

Level 1 Level 2

6

Level 3

13 15

12 14

Binary Search Trees

10

Top down and bottom up insertion

Still need to insert preserving the order of keys (otherwise search does not work) Insert as the new items arrive (cannot wait till have all items) So need to alter the tree as more items arrive.

8

Example

Keep the trees height balanced (for every node, the difference in height between left and right subtrees at most 1, or no more than twice the depth of the shorter subtree). Performance always logarithmic.

Binary Search Trees

11

Top down insertion algorithms make changes to the tree (necessary to keep the tree balanced) as they search for the place to insert the item. They make one pass through the tree. Bottom up: first insert the item, and then work back through the tree making changes. Less efficient because make two passes through the tree. © 2004 Goodrich, Tamassia

Binary Search Trees

12

2

Dictionaries

11/3/2004 11:57 AM

Examples Bottom up: AVL trees Top down: red-black trees

AVL Trees 6

v

8

3

z 4

Binary Search Trees

13

AVL Trees (chapter 9.2)

It is represented as a number equal to the depth of the right subtree minus the depth of the left subtree. Can be 0,1 or -1.

Binary Search Trees

15

n(2)

Height of an AVL Tree

Binary Search Trees

Not an AVL:

AVL: B

X (2) A

X

B (1) C

A

D

D C

E

E

Binary Search Trees

16

n(2)

3 4

n(1)

Fact: 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) = 2 For 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) © 2004 Goodrich, Tamassia

14

Non-example and example

AVL (Adelson-Velskii & Landis) trees are binary search trees where nodes also have additional information: the difference in depth between their right and left subtrees (balance factor). First example of balanced trees.

Binary Search Trees

17

3

Height of an AVL Tree

4

n(1)

n(h) = 1 + n(h-1) + n(h-2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction), n(h) > 2in(h-2i)

Solving the base case we get: n(h) > 2 h/2-1 Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n) © 2004 Goodrich, Tamassia

Binary Search Trees

18

3

Dictionaries

11/3/2004 11:57 AM

Insertion in AVL Trees

Rotations

• Insert new leaf node, as for ordinary binary

The restructuring is done by performing a rotation.

search tree.

• Then work back up from new leaf to root,

checking if any height imbalance has been introduced (computing new balance factors). • Perform rotation to correct height imbalance (which rotation depends on the balance factor).

Binary Search Trees

19

Right rotation

A rotation is performed around some node X. It can be •

single right rotation,

single left rotation or

a double rotation. Binary Search Trees

20

Right rotation

Right rotation around X (tree is heavier on the left):

Right rotation around X: X (-2)

A

X (-2) B

A

X

A B B

Binary Search Trees

21

Right rotation

C

This leaves the old right child of A unaccounted for. Since it comes from the left of X, it is less than X. So it becomes X's new left child. Binary Search Trees

Binary Search Trees

22

Left rotation

X moves down and to the right, into the position of its right child; X's right subtree is unchanged. X's left child A moves up to take X's place. X is now A's new right child.

C

C

23

The tree is heavier on the right: X (2) A

B C

D E

Binary Search Trees

24

4

Dictionaries

11/3/2004 11:57 AM

Left rotation

Double rotations

Left rotation around X: B

X (2) A

Two single rotations in opposite directions (around two different nodes).

X

B A

D

C

Needed when there is a bend in the branch: 30

D C

E

25

E 27 Binary Search Trees

25

Example

Binary Search Trees

26

Example

Rotating right around 30 will not help:

First single left rotation around 25:

20 (2)

20 (2) 25 (2)

10

30 (-2)

10

30

25(1)

27 Binary Search Trees

27 27

Example

Binary Search Trees

28

Example

First single left rotation around 25:

Then single right rotation around 30:

20 (2)

20 (1) 30 (-2)

10

27

10

27 (-1)

25

30

Binary Search Trees

29

Binary Search Trees

30

5

Dictionaries

11/3/2004 11:57 AM

Which rotation?

Which rotation?

Balance factor -2 (left subtree longer), balance factor of the left daughter -1 or 0: single right rotation. Balance factor 2 (right subtree longer), balance factor of the right daughter 1 or 0: single left rotation

Binary Search Trees

31

• Balance factor -2, balance factor of the

left daughter 1: double rotation, around the daughter to the left, around the root to the right. • Balance factor 2, balance factor of the right daughter -1: double rotation, around the daughter to the right, around the root to the left. © 2004 Goodrich, Tamassia

Binary Search Trees

32

Running Times for AVL Trees

Deleting in AVL Trees

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

Find leftmost node/subtree of deleted node's right subtree.

search is O(log n)

Delete leftmost subtree.

insertion is O(log n)



Work back up to deleted node, restoring any imbalances. Replace deleted node by leftmost subtree.

Binary Search Trees

 

height of tree is O(log n), no restructures needed initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n)

deletion is O(log n) 

Work back up to root, restoring any imbalances. © 2004 Goodrich, Tamassia





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

On average, better than binary search trees. 33

Binary Search Trees

34

6