Data Structures in Java Session 10 Instructor: Bert Huang - - PowerPoint PPT Presentation

data structures in java
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Java Session 10 Instructor: Bert Huang - - PowerPoint PPT Presentation

Data Structures in Java Session 10 Instructor: Bert Huang http://www1.cs.columbia.edu/~bert/courses/3134 Announcements Homework 3 due 10/20 Review AVL Trees Single rotate for left-left imbalance Double rotate for left-right


slide-1
SLIDE 1

Data Structures in Java

Session 10 Instructor: Bert Huang http://www1.cs.columbia.edu/~bert/courses/3134

slide-2
SLIDE 2

Announcements

  • Homework 3 due 10/20
slide-3
SLIDE 3

Review

  • AVL Trees
  • Single rotate for left-left imbalance
  • Double rotate for left-right imbalance
  • Running time Analysis
  • Depth always O(log N)
  • Constant cost for rotations
slide-4
SLIDE 4

Todayʼs Plan

  • Amortized Running time
  • Splay Trees
  • Tries
slide-5
SLIDE 5

Amortized Running Time

  • So far, we measure the worst-case

running time of each operation

  • Usually we perform many operations
  • Sometimes M O(f(N)) operations can

run provably faster than O(M f(N))

  • Then we can guarantee better average

running time, aka amortized

slide-6
SLIDE 6

Comparing Models

  • Amortized and Average case average

running time of many operations

  • Amortized and Standard: adversary

chooses input values and operations

  • Average analysis, analyst chooses

randomization scheme

slide-7
SLIDE 7

Splay Trees

  • Like AVL trees, use the standard binary

search tree property

  • After any operation on a node, make

that node the new root of the tree

  • Make the node the root by repeating
  • ne of two moves that make the tree

more spread out

slide-8
SLIDE 8

Informal Justification

  • Similar to caching.
  • Heuristically, data that is accessed

tends to be accessed often.

  • Easier to implement than AVL trees
  • No height bookkeeping
slide-9
SLIDE 9

Easy cases

  • If node is root, do nothing
  • If node is child of root, do single AVL

rotation

  • Otherwise, node has a grandparent,

and there are two cases

slide-10
SLIDE 10

Case 1: zig-zag

  • Use when the node is the right child of a

left child (or left-right)

  • Double rotate, just like AVL tree

a b c w x y z a b c w x y z

slide-11
SLIDE 11

Case 2: zig-zig

  • We canʼt use the single-rotation

strategy like AVL trees

  • Instead we use a different process, and

weʼll compare this to single-rotation

slide-12
SLIDE 12

Case 2: zig-zig

  • Use when node is the right-right child (or left-left)
  • Reverse the order of grandparent->parent->node
  • Make it node->parent->grandparent

a b c y w x z a b c y w x z

slide-13
SLIDE 13

Case 2 versus Single Rotations 1

6 5 4 3 2 1 7 6 5 4 3 2 1 7

zig-zig single-rotate

slide-14
SLIDE 14

Case 2 versus Single Rotations 2

6 5 4 3 2 1 7

6 5 4 3 2 1 7

zig-zig single-rotate

slide-15
SLIDE 15

Case 2 versus Single Rotations 3

6 5 4 3 2 1 7

6 5 4 3 2 1 7

zig-zig single-rotate

slide-16
SLIDE 16

Case 2 versus Single Rotations 4

6 5 4 3 2 1 7

6 5 4 3 2 1 7

zig-zig single-rotate

slide-17
SLIDE 17

Splay Analysis (Informal)

  • We can make a chain by inserting nodes that

make the tree its left child

  • Each of these operations is cheap
  • Then we can search for deepest node
  • Splay operation squishes the tree;

can only bad operations once before they become cheap

  • M operations take O(M log N), so amortized

O(log N) per operation (fyi, not proved)

slide-18
SLIDE 18

Prefix Trees (Tries)

  • Nicknamed “Trie”, short for retrieval
  • Efficiently store objects for fast retrieval

via keys

  • Usually key is a String
  • Basic strategy:
  • split into sub-tries based on current

letter

slide-19
SLIDE 19

Trie Example

  • “cat”, “cow”, “dog”, “doberman”, “duck”

Root c d

  • g

b u

  • a

cat cow dog doberman duck

slide-20
SLIDE 20

Trie Analysis

  • In the worst case, inserting a key of

length k or (looking up) is O(k)

  • This is not dependent on N (this is

shocking!)

  • Much better than log(N) for huge data

like dictionaries

slide-21
SLIDE 21

Reading

  • Splay Trees: 4.5