search trees. binary search tree (§10.1) a binary search tree is a binary tree storing keys (or...

39
Search Trees

Upload: vincent-chapman

Post on 04-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Search Trees

Page 2: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Binary Search Tree (§10.1)

• 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)

• Leaf nodes do not store items

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

6

92

41 8

Page 3: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Search• 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 a null position

• Example: find(4)

Algorithm find (k, v)if T.isLeaf (v)

return Position(null)if k key(v)

return find(k, T.leftChild(v))else if k key(v)

return Position(v)else { k key(v) }

return find(k, T.rightChild(v))

6

92

41 8

Page 4: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Insertion

• 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

6

92

41 8

6

92

41 8

5

w

w

Page 5: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Exercise: Binary Search Trees

• Insert into an initially empty binary search tree items with the following keys (in this order). Draw the resulting binary search tree• 30, 40, 24, 58, 48, 26, 11, 13

Page 6: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Deletion

• To perform operation 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

6

92

41 8

5

v

w

6

92

51 8

Page 7: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

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

• Example: remove 3

3

1

8

6 9

5

v

w

z

2

5

1

8

6 9

v

2

Page 8: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Exercise: Binary Search Trees

• Insert into an initially empty binary search tree items with the following keys (in this order). Draw the resulting binary search tree• 30, 40, 24, 58, 48, 26, 11, 13

• Now, remove the item with key 30. Draw the resulting tree

• Now remove the item with key 48. Draw the resulting tree.

Page 9: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Performance• Consider a dictionary

with n items implemented by means of a binary search tree of height h• the space used is O(n)• methods

findElement() , insertItem() and removeElement() take O(h) time

• The height h is O(n) in the worst case and O(log n) in the best case

Page 10: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

AVL Trees

6

3 8

4

v

z

Page 11: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

AVL Tree Definition

• 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 12: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Height of an AVL Tree• 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 h-1 and another of height h-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). So

• n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(h-6), … (by induction),• n(h) > 2in(h-2i)

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

• Taking logarithms: log(n)>h/2-1 => h < 2log n(h) +2• Thus the height of an AVL tree is O(log n)

3

4 n(1)

n(2)

Page 13: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Insertion in an AVL Tree• Insertion is as in a binary search tree• Always done by expanding a leaf node.• Example:

44

17 78

32 50 88

48 62

Page 14: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Insertion in an AVL Tree• Insertion is as in a binary search tree• Always done by expanding a leaf node.• Example:

44

17 78

32 50 88

48 62

49

Page 15: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Insertion in an AVL Tree• Insertion is as in a binary search tree• Always done by expanding a leaf node.• Example:

44

17 78

32 50 88

48 62

49

Page 16: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Insertion in an AVL Tree• Insertion is as in a binary search tree• Always done by expanding a leaf node.• Example:

44

17 78

32 50 88

48 62

49

2 1

Page 17: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Insertion in an AVL Tree• Insertion is as in a binary search tree• Always done by expanding a leaf node.• Example:

44

17 78

32 50 88

48 62

49

3 1

Page 18: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

b

a

c

T0

T1

T2 T3

1h2h

2h 2h

3h3h

h

Page 19: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

b

a

c

T0

T1

T2 T3

1h2h

2h 2h

3h3h

h

Page 20: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

b

a

c

T0

T1

T2 T3

h2h

2h 1h

3h2h

1h

Page 21: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

b

a

c

T0

T1

T2 T3

h2h

2h 1h

3h2h

b

a c

T0 T1 T2 T3

2h 2h

1h

3h2h

1h

h

Page 22: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

b

a

c

T0

T1

T2 T3

h2h

2h 1h

2h3h

b

a c

T0 T1 T2 T3

2h 2h

1h

2h3h

1h

h

Page 23: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

b

a

c

T0

T3

T1 T2

1h2h

2h2h

3h3h

h

Page 24: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

b

a

c

T0

T3

T1 T2

1h2h

2h2h

3h3h

h

Page 25: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

c

a

b

T0

T3

T1 T2

h2h

2h1h

3h2h

1h

Page 26: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

c

a

b

T0

T3

T1 T2

h2h

2h1h

3h2h

1h

ca

b

T0 T3T1 T2

1h

2h 2h

h

3h2h

1h

Page 27: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

c

a

b

T0

T3

T1 T2

h2h

2h1h

2h3h

1h

ca

b

T0 T3T1 T2

1h

2h 2h

h

2h3h

1h

Page 28: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

b

a

c

T0

T1

T2T3

1h

h

1h

2h

2h

2h

3h

Page 29: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Tree Rotations

c

a

b

T0

T3

T1T2

1h

h

1h

2h

2h

2h

3h

Page 30: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Exercise: AVL Trees

• Insert into an initially empty AVL tree items with the following keys (in this order). Draw the resulting AVL tree• 30, 40, 24, 58, 48, 26, 11, 13

Page 31: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Removal in an AVL Tree

• Removal begins as in a binary search tree, which means the node removed will become an empty external node. Its parent, w, may cause an imbalance.

• Example:

remove 32

Page 32: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Rebalancing after a Removal

• Let z be the first unbalanced node encountered while travelling up the tree from w (parent of removed node) . Also, let y be the child of z with the larger height, and let x be the child of y with the larger height.

• We perform restructure(x) to restore balance at z.

Page 33: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Rebalancing after a Removal• 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• This can happen at most O(log n) times. Why?

Page 34: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

Exercise: AVL Trees

• Insert into an initially empty AVL tree items with the following keys (in this order). Draw the resulting AVL tree• 30, 40, 24, 58, 48, 26, 11, 13

• Now, remove the item with key 48. Draw the resulting tree

• Now, remove the item with key 58. Draw the resulting tree

Page 35: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

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)

Page 36: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying
Page 37: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

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 38: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

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

T0T1T2

T3

a = xb = y

c = zsingle rotation

Page 39: Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying

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

T0T2

T1

T3 T0

T2T3 T1

c = zb = x

a = y