Advanced Tree Structures Department of Computer Science University - - PowerPoint PPT Presentation

advanced tree structures
SMART_READER_LITE
LIVE PREVIEW

Advanced Tree Structures Department of Computer Science University - - PowerPoint PPT Presentation

CMSC 132: Object-Oriented Programming II Advanced Tree Structures Department of Computer Science University of Maryland, College Park IMPORTANT Make sure you check your e-mails every day and the messages we post on the class announcements.


slide-1
SLIDE 1

CMSC 132: Object-Oriented Programming II

Advanced Tree Structures

Department of Computer Science University of Maryland, College Park

slide-2
SLIDE 2

IMPORTANT

  • Make sure you check your e-mails every day and the

messages we post on the class announcements. It is your responsibility to check them so you are aware of important information/deadlines.

  • Final exam information is available on the class web page.

Please complete course evaluations 

– https://courseevalum.umd.edu/

  • Double-check all your scores in grades server are correct
  • Save your projects (CVS repository will disappear after

class is over)

slide-3
SLIDE 3

Overview

  • Binary trees

– Balance – Rotation

  • Multi-way trees

– Search – Insert

  • Indexed tries
slide-4
SLIDE 4

Tree Balance

  • Degenerate

– Worst case – Search in O(n) time

  • Balanced

– Average case – Search in O( log(n) ) time

Degenerate binary tree Balanced binary tree

slide-5
SLIDE 5

Tree Balance

  • Question

– Can we keep tree (mostly) balanced?

  • Self-balancing binary search trees

– AVL trees – Red-black trees

  • Approach

– Select invariant (that keeps tree balanced) – Fix tree after each insertion / deletion

  • Maintain invariant using rotations

– Provides operations with O( log(n) ) worst case

slide-6
SLIDE 6

AVL Trees

  • Properties

Binary search tree

Heights of children for node differ by at most 1

  • Example

88 44 17 78 32 50 48 62 2 4 1 1 2 3 1 1

Heights of children shown in red

slide-7
SLIDE 7

AVL Trees

  • History

– Discovered in 1962 by two Russian mathematicians,

Adelson-Velskii & Landis

  • Algorithm

– Find / insert / delete as a binary search tree – After each insertion / deletion

  • If height of children differ by more than 1
  • Rotate children until subtrees are balanced
  • Repeat check for parent (until root reached)
slide-8
SLIDE 8

Tree Rotations

  • Changes shape of tree

Rotation moves one node up in the tree and one node down

Height is decreased by moving larger sub-trees up and smaller sub-trees down

  • Types

Single rotation

  • Left
  • Right

Double rotation

  • Left-right
  • Right-left
slide-9
SLIDE 9

Tree Rotation Example

  • Single right rotation

1 2 3 1 2 3

slide-10
SLIDE 10

Tree Rotation Example

  • Single right rotation

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

Node 4 attached to new parent

slide-11
SLIDE 11

Red-black Trees

  • History

Discovered in 1972 by Rudolf Bayer

  • Algorithm

Insert / delete may require complicated bookkeeping & rotations

  • Java collections

TreeMap andTreeSet use red-black trees

  • Properties

Binary search tree

Every node is red or black

The root is black

Every leaf is black

All children of red nodes are black

For each leaf, same # of black nodes on path to root

  • Characteristics

Properties ensures no leaf is twice as far from root as another leaf

slide-12
SLIDE 12

Red-black Trees

  • Example
slide-13
SLIDE 13

Multi-way Search Trees

  • Properties

Generalization of binary search tree

Node contains 1…k keys (in sorted order)

Node contains 2…k+1 children

Keys in jth child < jth key < keys in (j+1)th child

  • Examples

5 12 2 17 8 5 8 15 33 1 3 19 21 9 7 44

slide-14
SLIDE 14

Types of Multi-way Search Trees

  • 2-3 Tree

– Internal nodes have 2 or 3

children

  • Indexed Search Tree (trie)

– Internal nodes have up to 26

children (for strings)

  • B-Tree

– T = minimum degree – Non-root internal nodes have

T-1 to 2T-1 children

– All leaves have same depth

T-1 … 2T-1 1 2T 2

5 12 2 17 8 c a s

slide-15
SLIDE 15

Multi-way Search Trees

  • Search algorithm

Compare key x to 1…k keys in node

If x = some key then return node

Else if (x < key j) search child j

Else if (x > all keys) search child k+1

  • .Example

Search(17)

5 12 1 2 17 8 30 40 25 27 44 36

slide-16
SLIDE 16

Multi-way Search Trees

  • Insert algorithm

– Search key x to find node n – If ( n not full ) insert x in n – Else if ( n is full )

  • Split n into two nodes
  • Move middle key from n to n’s parent
  • Insert x in n
  • Recursively split n’s parent(s) if necessary
slide-17
SLIDE 17

Multi-way Search Trees

  • Insert Example (for 2-3 tree)

– Insert( 4 )

5 12 2 17 8 5 12 2 4 17 8

slide-18
SLIDE 18

Multi-way Search Trees

  • Insert Example (for 2-3 tree)

Insert( 1 )

5 12 1 2 4 17 8 2 5 12 2 1 17 8 4 5 12 1 17 8 4

Split parent Split node

slide-19
SLIDE 19

B-Trees

  • Characteristics

– Height of tree is O( logT(n) ) – Reduces number of nodes accessed – Wasted space for non-full nodes

  • Popular for large databases (indices)

– 1 node = 1 disk block – Reduces number of disk blocks read

slide-20
SLIDE 20

Indexed Search Tree (Trie)

  • Special case of tree
  • Applicable when

Key C can be decomposed into a sequence of subkeys C1, C2, … Cn

Redundancy exists between subkeys

  • Approach

Store subkey at each node

Path through trie yields full key

C3 C1 C2 C4 C3

slide-21
SLIDE 21

Standard Trie Example

  • For strings

{ bear, bell, bid, bull, buy, sell, stock, stop }

a e b r l l s u l l y e t l l

  • c

k p i d

slide-22
SLIDE 22

Word Matching Trie

  • Insert words

into trie

  • Each leaf

stores

  • ccurrences of

word in the text

s e e b e a r ? s e l l s t

  • c k

! s e e b u l l ? b u y s t

  • c k

! b i d s t

  • c k

! a a h e t h e b e l l ? s t

  • p

! b i d s t

  • c k

!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

a r

87 88

a e b l s u l e t e 0, 24

  • c

i l r 6 l 78 d 47, 58 l 30 y 36 l 12 k 17, 40, 51, 62 p 84 h e r 69 a

slide-23
SLIDE 23

Compressed Trie

  • Observation

– Internal node v of T is redundant if v has one child

and is not the root

  • Approach

– A chain of redundant nodes can be compressed

  • Replace chain with single node
  • Include concatenation of labels from chain
  • Result

– Internal nodes have at least 2 children – Some nodes have multiple characters

slide-24
SLIDE 24

Compressed Trie

  • Example

e b ar ll s u ll y ell to ck p id a e b r l l s u l l y e t l l

  • c

k p i d

slide-25
SLIDE 25

Tries and Web Search Engines

  • Search engine index

Collection of all searchable words

Stored in compressed trie

  • Each leaf of trie

Associated with a word

List of pages (URLs) containing that word

  • Called occurrence list
  • Trie is kept in memory (fast)
  • Occurrence lists kept in external memory

Ranked by relevance