CS 225 Data Structures Oc October 16 AV AVL Applications G G - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Oc October 16 AV AVL Applications G G - - PowerPoint PPT Presentation

CS 225 Data Structures Oc October 16 AV AVL Applications G G Carl Evans AV AVL Tree Analysis We know: insert, remove and find runs in: __________. We will argue that: h is _________. AV AVL Tree Analysis n, number of nodes h, height


slide-1
SLIDE 1

CS 225

Data Structures

Oc October 16 – AV AVL Applications

G G Carl Evans

slide-2
SLIDE 2

AV AVL Tree Analysis

We know: insert, remove and find runs in: __________. We will argue that: h is _________.

slide-3
SLIDE 3

AV AVL Tree Analysis

  • The number of nodes in the tree, f-1(h), will always

be greater than c × g-1(h) for all values where n > k.

n, number of nodes h, height n, number of nodes h, height

slide-4
SLIDE 4

Pl Plan an of Acti tion

Since our goal is to find the lower bound on n given h, we can begin by defining a function given h which describes the smallest number of nodes in an AVL tree of height h:

slide-5
SLIDE 5

Si Simplify t the R Recu curr rrence ce

N(h) = 1 + N(h - 1) + N(h - 2)

slide-6
SLIDE 6

St State a a T Theor

  • rem

Theorem: An AVL tree of height h has at least __________. Proof: I. Consider an AVL tree and let h denote its height.

  • II. Case: ______________

An AVL tree of height ____ has at least ____ nodes.

slide-7
SLIDE 7

Pr Prove ve a Theorem

  • III. Case: ______________

An AVL tree of height ____ has at least ____ nodes.

slide-8
SLIDE 8

Pr Prove ve a Theorem

  • IV. Case: ______________

By an Inductive Hypothesis (IH): We will show that: An AVL tree of height ____ has at least ____ nodes.

slide-9
SLIDE 9

Pr Prove ve a Theorem

  • V. Using a proof by induction, we have shown that:

…and inverting:

slide-10
SLIDE 10

AV AVL Runtime Proof

On Friday, we proved an upper-bound on the height of an AVL tree is 2 × lg(n) or O( lg(n) ): N(h) := Minimum # of nodes in an AVL tree of height h N(h) = 1 + N(h-1) + N(h-2) > 1 + 2h-1/2 + 2h-2/2 > 2 × 2h-2/2 = 2h-2/2+1 = 2h/2 Theorem #1: Every AVL tree of height h has at least 2h/2 nodes.

slide-11
SLIDE 11

AV AVL Runtime Proof

On Friday, we proved an upper-bound on the height of an AVL tree is 2 × lg(n) or O( lg(n) ): # of nodes (n) ≥ N(h) > 2h/2 n > 2h/2 lg(n) > h/2 2 × lg(n) > h h < 2 × lg(n) , for h ≥ 1 Proved: The maximum number of nodes in an AVL tree of height h is less than 2 × lg(n).

slide-12
SLIDE 12

Su Summary of

  • f Ba

Balance ced BS BST

AVL Trees

  • Max height: 1.44 * lg(n)
  • Rotations:
slide-13
SLIDE 13

Su Summary of

  • f Ba

Balance ced BS BST

AVL Trees

  • Max height: 1.44 * lg(n)
  • Rotations:

Zero rotations on find One rotation on insert O(h) == O(lg(n)) rotations on remove Red-Black Trees

  • Max height: 2 * lg(n)
  • Constant number of rotations on insert (max 2), remove

(max 3).

slide-14
SLIDE 14

Wh Why Balanced BST?

slide-15
SLIDE 15

Su Summary of

  • f Ba

Balance ced BS BST

Pros:

  • Running Time:
  • Improvement Over:
  • Great for specific applications:
slide-16
SLIDE 16

Su Summary of

  • f Ba

Balance ced BS BST

Cons:

  • Running Time:
  • In-memory Requirement:
slide-17
SLIDE 17

Re Red-Bl Black T Trees i in C+ C++

C++ provides us a balanced BST as part of the standard library: std::map<K, V> map;

slide-18
SLIDE 18

Re Red-Bl Black T Trees i in C+ C++

V & std::map<K, V>::operator[]( const K & )

slide-19
SLIDE 19

Re Red-Bl Black T Trees i in C+ C++

V & std::map<K, V>::operator[]( const K & ) std::map<K, V>::erase( const K & )

slide-20
SLIDE 20

Re Red-Bl Black T Trees i in C+ C++

iterator std::map<K, V>::lower_bound( const K & ); iterator std::map<K, V>::upper_bound( const K & );

slide-21
SLIDE 21

CS 225 CS 225 --

  • - Cou

Course U Update

Your grades can now be viewed on moodle (https://learn.illinois.edu/) We will discuss the grades for the course as a whole (ex: average, etc) in lecture on Wednesday.

385

slide-22
SLIDE 22

It Iter erators

Why do we care?

DFS dfs(...); for ( ImageTraversal::Iterator it = dfs.begin(); it != dfs.end(); ++it ) { std::cout << (*it) << std::endl; } 1 2 3 4

slide-23
SLIDE 23

It Iter erators

Why do we care?

DFS dfs(...); for ( ImageTraversal::Iterator it = dfs.begin(); it != dfs.end(); ++it ) { std::cout << (*it) << std::endl; } 1 2 3 4 DFS dfs(...); for ( const Point & p : dfs ) { std::cout << p << std::endl; } 1 2 3 4

slide-24
SLIDE 24

It Iter erators

Why do we care?

DFS dfs(...); for ( ImageTraversal::Iterator it = dfs.begin(); it != dfs.end(); ++it ) { std::cout << (*it) << std::endl; } 1 2 3 4 DFS dfs(...); for ( const Point & p : dfs ) { std::cout << p << std::endl; } 1 2 3 4 ImageTraversal & traversal = /* ... */; for ( const Point & p : traversal ) { std::cout << p << std::endl; } 1 2 3 4

slide-25
SLIDE 25

Ev Every Data Structure So Far

Unsorted Array Sorted Array Unsorted List Sorted List Binary Tree BST AVL Find Insert Remove Traverse

slide-26
SLIDE 26

Ra Range-ba base sed d Searche hes

Q: Consider points in 1D: p = {p1, p2, …, pn}. …what points fall in [11, 42]? Tree construction:

slide-27
SLIDE 27

Ra Range-ba base sed d Searche hes

Balanced BSTs are useful structures for range-based and nearest-neighbor searches. Q: Consider points in 1D: p = {p1, p2, …, pn}. …what points fall in [11, 42]? Ex:

3 6 11 33 41 44 55

slide-28
SLIDE 28

Ra Range-ba base sed d Searche hes

Q: Consider points in 1D: p = {p1, p2, …, pn}. …what points fall in [11, 42]? Ex:

3 6 11 33 41 44 55

slide-29
SLIDE 29

Ra Range-ba base sed d Searche hes

Q: Consider points in 1D: p = {p1, p2, …, pn}. …what points fall in [11, 42]? Tree construction:

slide-30
SLIDE 30

Ra Range-ba base sed d Searche hes

slide-31
SLIDE 31

Ra Range-ba base sed d Searche hes

6 3 11 33 44 41

3 6 11 33 41 44 55

slide-32
SLIDE 32

Ra Range-ba base sed d Searche hes

6 3 11 33 44 41

3 6 11 33 41 44 55

Q: Consider points in 1D: p = {p1, p2, …, pn}. …what points fall in [11, 42]?

slide-33
SLIDE 33

Ra Range-ba base sed d Searche hes

6 3 11 33 44 41

3 6 11 33 41 44 55

slide-34
SLIDE 34

Ru Running T Time

6 3 11 33 44 41

3 6 11 33 41 44 55

slide-35
SLIDE 35

Ra Range-ba base sed d Searche hes

Q: Consider points in 1D: p = {p1, p2, …, pn}. …what points fall in [11, 42]? Ex:

3 6 11 33 41 44 55

slide-36
SLIDE 36
slide-37
SLIDE 37

Ra Range-ba base sed d Searche hes

Consider points in 2D: p = {p1, p2, …, pn}. Q: What points are in the rectangle: [ (x1, y1), (x2, y2) ]? Q: What is the nearest point to (x1, y1)?

p1 p2 p4 p3 p7 p5 p6

slide-38
SLIDE 38

Ra Range-ba base sed d Searche hes

Consider points in 2D: p = {p1, p2, …, pn}. Tree construction:

p1 p2 p4 p3 p7 p5 p6

slide-39
SLIDE 39

Ra Range-ba base sed d Searche hes

p1 p2 p3 p4 p5 p6 p7 p1 p2 p4 p3 p7 p5 p6

slide-40
SLIDE 40

kD kD-Tr Trees

p1 p2 p3 p4 p5 p6 p7 p1 p2 p4 p3 p7 p5 p6

slide-41
SLIDE 41

kD kD-Tr Trees

p1 p2 p3 p4 p5 p6 p7 p1 p2 p4 p3 p7 p5 p6