CS 225 Data Structures March 5 5 AVL Applications Wad ade Fag - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures March 5 5 AVL Applications Wad ade Fag - - PowerPoint PPT Presentation

CS 225 Data Structures March 5 5 AVL Applications Wad ade Fag agen-Ulm lmschneid ider 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) ) . Summary ry of f Balanced BST AVL


slide-1
SLIDE 1

CS 225

Data Structures

March 5 5 – AVL Applications

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Summary ry of f Balanced BST

AVL Trees

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

Summary ry of f Balanced 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, remove, and find
slide-5
SLIDE 5

Why AVL?

slide-6
SLIDE 6

Summary ry of f Balanced BST

Pros:

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

Summary ry of f Balanced BST

Cons:

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

Red-Black Trees in C++ ++

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

slide-9
SLIDE 9

Red-Black Trees in C++ ++

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

slide-10
SLIDE 10

Red-Black Trees in C++ ++

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

slide-11
SLIDE 11

Red-Black Trees in C++ ++

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

slide-12
SLIDE 12

CS 225 --

  • - Course Update

This weekend, the following grades were updated:

  • mp1
  • mp2*
  • mp3*
  • lab_inheritance
  • lab_quacks
  • lab_trees
slide-13
SLIDE 13

It Iterators

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-14
SLIDE 14

It Iterators

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-15
SLIDE 15

It Iterators

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-16
SLIDE 16

It Iterators

ImageTraversal *traversal = /* ... */; for ( const Point & p : traversal ) { std::cout << p << std::endl; } 1 2 3 4

slide-17
SLIDE 17

Every ry Data Structure So Far

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

slide-18
SLIDE 18

Range-based Searches

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

slide-19
SLIDE 19

Range-based Searches

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-20
SLIDE 20

Range-based 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-21
SLIDE 21

Range-based Searches

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

slide-22
SLIDE 22

Range-based Searches

slide-23
SLIDE 23

Range-based Searches

6 3 11 33 44 41

3 6 11 33 41 44 55

slide-24
SLIDE 24

Range-based Searches

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-25
SLIDE 25

Range-based Searches

6 3 11 33 44 41

3 6 11 33 41 44 55

slide-26
SLIDE 26

Running Time

6 3 11 33 44 41

3 6 11 33 41 44 55

slide-27
SLIDE 27

Range-based 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
slide-29
SLIDE 29

Range-based Searches

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-30
SLIDE 30

Range-based Searches

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

p1 p2 p4 p3 p7 p5 p6

slide-31
SLIDE 31

Range-based Searches

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

slide-32
SLIDE 32

kD kD-Trees

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

slide-33
SLIDE 33

kD kD-Trees

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