75
Binary search trees (chapters 18.1 – 18.3)

Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

  • Upload
    ledieu

  • View
    247

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Binary search trees(chapters 18.1 – 18.3)

Page 2: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

horsehorse

Binary search trees

In a binary search tree (BST), every node is greater than all its left descendants, and less than all its right descendants(recall that thisis an invariant) owlowl

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

hamsterhamster

Page 3: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

horsehorse

Searching in a BST

Finding an element in a BST is easy, because by looking at the root you can tell which subtree the element is in

owlowl

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

hamsterhamster

lemur must bein left subtree

of owl

lemur must bein right subtree

of hamster

Page 4: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Searching in a binary search tree

To search for target in a BST:● If the target matches the root node's data, we've

found it● If the target is less than the root node's data,

recursively search the left subtree● If the target is greater than the root node's data,

recursively search the right subtree● If the tree is empty, fail

A BST can be used to implement a set, or a map from keys to values

Page 5: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Inserting into a BST

To insert a value into a BST:● Start by searching for the value● But when you get to null (the empty tree), make a

node for the value and place it there

horsehorse

owlowl

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

hamsterhamster

monkeymonkey

Page 6: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Deleting from a BST

To delete a value into a BST:● Find the node containing the value● If the node is a leaf, just remove it

horsehorse

owlowl

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

hamsterhamster

To delete wolf,just remove

this node fromthe tree

Page 7: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Deleting from a BST, continued

If the node has one child, replace the node with its child

horsehorse

owlowl

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

hamsterhamster

To delete penguin,replace it in thetree with wolf

Page 8: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Deleting from a BST

To delete a value from a BST:● Find the node● If it has no children, just remove it from the tree● If it has one child, replace the node with its child● If it has two children...?

Can't remove the node without removing its children too!

Page 9: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Deleting a node with two children

Delete the biggest value from the node's left subtree and put this value [why this one?] in place of the node we want to delete

Delete owlby replacing itwith monkey

Deletemonkeyhorsehorse

owlowl

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

hamsterhamster

monkeymonkey

Page 10: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Deleting a node with two children

Delete the biggest value from the node's left subtree and put this value [why this one?] in place of the node we want to delete

The root isnow monkey

horsehorse

monkeymonkey

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

hamsterhamster

Page 11: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Deleting a node with two children

Here is the most complicated case:To delete

monkey, replaceit by lemur

horsehorse

monkeymonkey

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

hamsterhamster

But lemur has achild! Put horse

where lemur was

Page 12: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Deleting a node with two children

Here is the most complicated case:To delete

monkey, replaceit by lemur

horsehorse

monkeymonkey

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

hamsterhamster

But lemur has achild! Put horse

where lemur was

Page 13: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Deleting a node with two children

Here is the most complicated case:

lemurlemur

horsehorsegorillagorilla

penguinpenguin

wolfwolf

apeape

hamsterhamster

Page 14: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

A bigger example

What happens if we deletefarmer? is? cow? rat?

Page 15: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Deleting a node with two children

Deleting rat, we replace it with priest; now we have to delete priest which has a child, morn

Page 16: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Deleting a node with two children

Find and delete the biggest value in the left subtree and put that value in the deleted node● Using the biggest value preserves the

invariant (check you understand why)● To find the biggest value: repeatedly

descend into the right child until you find a node with no right child

● The biggest node can't have two children, so deleting it is easier

Page 17: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Complexity of BST operations

All our operations are O(height of tree)This means O(log n) if the tree is balanced, but O(n) if it's unbalanced (like the tree on the right)● how might we get

this tree?

Balanced BSTs add anextra invariant that makessure the tree is balanced● then all operations are O(log n)

Page 18: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Summary of BSTs

Binary trees with BST invariantCan be used to implement sets and maps● lookup: can easily find a value in the tree● insert: perform a lookup, then put the new value at the

place where the lookup would terminate● delete: find the value, then remove its node from the tree –

several cases depending on how many children the node has

Complexity:● all operations O(height of tree)● that is, O(log n) if tree is balanced, O(n) if unbalanced● inserting random data tends to give balanced trees,

sequential data gives unbalanced ones

Page 19: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Tree traversal

Traversing a tree means visiting all its nodes in some orderA traversal is a particular order that we visit the nodes inThree common traversals: preorder, inorder, postorder

Page 20: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

owlowl

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

Preorder traversal

hamsterhamster

1

2

3

4

5

6

7

Visit root node, then left subtree, then right (root node first)

Page 21: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

owlowl

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

Postorder traversal

hamsterhamster

7

4

2

1

3

6

5

Visit left subtree, then right, then root node (root node last)

Page 22: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

owlowl

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

apeape

Inorder traversal

hamsterhamster

5

3

2

1

4

6

7

Visit left subtree, then root node, then right (root node in middle)

Inorder traversalof a BST gives you

the nodes inascending order!

Page 23: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

In-order traversal – printing

void inorder(Node<E> node) { if (node == null) return; inorder(node.left); System.out.println(node.value); inorder(node.right);}But nicer to define an iterator!Iterator<Node<E>> inorder(Node<E> node);See 17.4

Page 24: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

AVL trees(chapter 18.4)

Page 25: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Balanced BSTs: the problem

The BST operations take O(height of tree), so for unbalanced trees can take O(n) time

Page 26: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Balanced BSTs: the solution

Take BSTs and add an extra invariant that makes sure that the tree is balanced● Height of tree must be O(log n)● Then all operations will take O(log n) time

One possible idea for an invariant:● Height of left child = height of right child

(for all nodes in the tree)● Tree would be sort of “perfectly balanced”

What's wrong with this idea?

Page 27: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

A too restrictive invariant

Perfect balance is too restrictive!Number of nodes can only be 1, 3, 7, 15, 31, ... owlowl

lemurlemurgorillagorilla

penguinpenguin

wolfwolf

hamsterhamster

pandapanda

Page 28: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

AVL trees – a less restrictive invariant

The AVL tree is the first balanced BST discovered (from 1962) – it's named after Adelson-Velsky and LandisIt's a BST with the following invariant:● The difference in heights between the left and

right children of any node is at most 1

This makes the tree's height O(log n), so it's balanced

Page 29: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Example of an AVL tree(from Wikipedia)

12 23 54 76

9 14 19 67

50

17 72

Left child height 2Right child height 1Left child height 2

Right child height 2

Left child height 1Right child height 0

Page 30: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Why are these not AVL trees?

Page 31: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Why are these not AVL trees?

Left child height 0Right child height 8

Page 32: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Why are these not AVL trees?

Left child height 1Right child height 3

Page 33: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Rotation

Rotation rearranges a BST by moving a different node to the root, without changing the BST's contents

(pic from Wikipedia)

Page 34: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Rotation

We can strategically use rotations to rebalance an unbalanced tree.This is what most balanced BST variants do!

Height of 4

Height of 3

Page 35: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

AVL insertion

Start by doing a BST insertion● This might break the AVL (balance) invariant

Then go upwards from the newly-inserted node, looking for nodes that break the invariant (unbalanced nodes)Whenever you find one, rotate it● Then continue upwards in the tree

There are several cases depending on how the node became unbalanced

Page 36: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Case 1: a left-left tree

50

c

25

ba

Each pink trianglerepresents an

AVL treewith height k

The purple representsan insertion that hasincreased the height

of tree a to k+1

Page 37: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Case 1: a left-left tree

50

c

25

ba

Height kHeight k+2

Left height minusright height = 2:

invariant broken!

Page 38: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Case 1: a left-left tree

50

c

25

ba

This is called aleft-left tree

because both the root andthe left child are deeper

on the left

To fix it we do aright rotation

Page 39: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Balancing a left-left tree, afterwards

50

c

25

b

a

Height k+1 Height k+1

Invariantrestored!

Page 40: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Case 2: a right-right tree

25

a

50

b c

Mirror image of left-left treeCan be fixed with

left rotation

Page 41: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Case 3: a left-right tree

50

c

25

ba

Height kHeight k+2

Left height minusright height = 2:

invariant broken!

Page 42: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Case 3: a left-right tree

50

c

25

ba We can't fix this withone rotation

Let's look at b'ssubtrees b

L and b

R

Page 43: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Case 3: a left-right tree

50

c

25

a

Rotate 25-subtree to the left

40

bRbL

Height k-1

Page 44: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Case 3: a left-right tree

50

c25

a

We now have a left-left tree!So we can fix it by

rotating the whole treeto the right

40

bR

bL

Height k+2 Height kHeight k+2

Height k+1

Page 45: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Case 3: a left-right tree

50

c

25

a

40

bRbL

Balanced!Notice it works whichever

of bL and b

R has the

extra height

Page 46: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Case 4: a right-left tree

25

a

50

b c

Mirror image ofleft-right tree

Page 47: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

Four sorts of unbalanced trees

Left-left (height of left-left grandchild = k+1, height of left child = k+2, height of right child = k)● Rotate the whole tree to the right

Left-right (height of left-right grandchild = k+1, height of left child = k+2, height of right child = k)● First rotate the left child to the left● Then rotate the whole tree to the right

Right-left and right-right: symmetric

Page 48: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

The four cases

(picture from Wikipedia)The numbers in thediagram show the balanceof the tree: left heightminus right heightTo implement thisefficiently, record thebalance in the nodes andlook at it to work outwhich case you're in

5

D

3

A

4

CB

Left Right Case Right Left Case

3

A

4

5

C D

B

Right Right Case

5

D

4

3

BA

C

Left Left Case

4

5

C D

Balanced

3

A B

4

5

C D

Balanced

3

A B

3

A

5

D

4

B C

-22

1-1

2 -2

1/0 -1/0

-1/0 1/0

Page 49: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

49

A bigger example(slides from Peter Ljunglöf )

Let's build an AVL tree for the words in

”a quick brown fox jumps over the lazy dog”

Try this example on https://www.cs.usfca.edu/~galles/visualization/AVLtree.html

Page 50: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

50

a quick brown…

a

quick

brown

+2

-1

0

The overall tree is right-heavy (Right-Left)

parent balance = +2right child balance = -1

Page 51: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

51

a quick brown…

a

quick

brown

+2

-1

0

1. Rotate right around the child

Page 52: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

52

a quick brown…

a

brown

quick

+2

+1

0

1. Rotate right around the child

Page 53: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

53

a quick brown…

a

brown

quick

+2

+1

0

1. Rotate right around the child

2. Rotate left around the parent

Page 54: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

54

a quick brown…

brown

quicka

0

00

1. Rotate right around the child

2. Rotate left around the parent

Page 55: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

55

a quick brown fox…

brown

quicka

0

00Insert fox

Page 56: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

56

a quick brown fox…

brown

quicka

+1

-10Insert fox

fox 0

Page 57: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

57

a quick brown fox jumps…

brown

quicka

+1

-10

Insert jumps

fox 0

Page 58: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

58

a quick brown fox jumps…

brown

quicka

+2

-20

Insert jumps

fox +1

jumps 0

Page 59: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

59

a quick brown fox jumps…

brown

quicka

+2

-20

fox +1

jumps 0

The tree is now left-heavy about quick (Left-

Right case)

Page 60: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

60

a quick brown fox jumps…

brown

quicka

+2

-20

fox +1

jumps 0

1. Rotate left around the child

Page 61: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

61

a quick brown fox jumps…

brown

quicka

+2

-20

jumps -1

fox 0

1. Rotate left around the child

Page 62: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

62

a quick brown fox jumps…

brown

quicka

+2

-20

jumps -1

fox 0

1. Rotate left around the child

2. Rotate right around the parent

Page 63: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

63

a quick brown fox jumps…

brown

jumpsa

+1

00

fox 0 quick 0

1. Rotate left around the child

2. Rotate right around the parent

Page 64: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

64

a quick brown fox jumps over…

brown

jumpsa

+1

00

fox 0 quick 0

Insert over

Page 65: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

65

a quick brown fox jumps over…

brown

jumpsa

+2

+10

fox 0 quick -1

Insert over

over 0

Page 66: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

66

a quick brown fox jumps over…

brown

jumpsa

+2

+10

fox 0 quick -1

over 0

We now have a Right-Right imbalance

Page 67: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

67

a quick brown fox jumps over…

brown

jumpsa

+2

+10

fox 0 quick -1

over 0

1. Rotate left around the parent

Page 68: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

68

a quick brown fox jumps over…

jumps

quickbrown

0

-10

a 0 fox 0 over 0

1. Rotate left around the parent

Page 69: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

69

a quick brown fox jumps over the…

jumps

quickbrown

0

-10

a 0 fox 0 over 0

Insert the

Page 70: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

70

a quick brown fox jumps over the…

jumps

quickbrown

0

00

a 0 fox 0 over 0

Insert the

the 0

Page 71: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

71

a quick brown fox jumps over thelazy…

jumps

quickbrown

0

00

a 0 fox 0 over 0

Insert lazy

the 0

Page 72: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

72

a quick brown fox jumps over thelazy…

jumps

quickbrown

+1

-10

a 0 fox 0 over -1

Insert lazy

the 0

lazy 0

Page 73: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

73

a quick brown fox jumps over thelazy dog

jumps

quickbrown

+1

-10

a 0 fox 0 over -1

Insert dog

the 0

lazy 0

Page 74: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

74

a quick brown fox jumps over thelazy dog!

jumps

quickbrown

0

-1+1

a 0 fox -1 over -1

Insert dog

the 0

lazy 0dog 0

Page 75: Binary search trees (chapters 18.1 – 18.3) - Chalmers · Binary search trees In a binary search tree (BST), ... Deleting from a BST ... Traversing a tree means visiting all its

AVL trees

A balanced BST that maintains balance by rotating the tree● Insertion: insert as in a BST and move upwards from the inserted node,

rotating unbalanced nodes● Deletion (in book if you're interested): delete as in a BST and move

upwards from the node that disappeared, rotating unbalanced nodes

Worst-case (it turns out) 1.44log n, typical log n comparisons for any operation – very balanced. This means lookups are quick.● Insertion and deletion can be slower than in a naïve BST, because you

have to do a bit of work to repair the invariant

Look in Haskell compendium (course website) for implementationVisualisation: https://www.cs.usfca.edu/~galles/visualization/AVLtree.html