Advance Tree Structure for Object Oriented Programming II | CMSC 132, Study notes of Computer Science

Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Fall 2008;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-oea
koofers-user-oea 🇺🇸

10 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 132:
Object-Oriented Programming II
Advanced Tree Structures
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Advance Tree Structure for Object Oriented Programming II | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Advanced Tree Structures

Department of Computer Science

University of Maryland, College Park

Overview

Binary trees

Balance Rotation

Multi-way trees

Search Insert

Indexed tries

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

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

Tree Rotations

Changes shape of tree

Rotation moves one node up in the tree and one node down Height is decresed by moving larger subtrees up and smaller subtrees down

Types

Single rotation Left Right Double rotation Left-right Right-left

Tree Rotation Example

Single right rotation

1

2

3

1

2

3

Example Single Rotations

T 0 T 1 T 3 T 0 T 1 T 2

T 3

single left rotation

T 2

T 3 T 2 T 1

T 0 T 1 T 2 T 3

T 0

single right rotation

Example Double Rotations

right-left double rotation

T 0 T 2 T 1

T 3 T 0

T 2 T 1 T 3

T 3 T 1 T 2

T 0 T 3

T 1 T 0 T 2

left-right double rotation

Red-black Trees

Example

Red-black Trees

History

Discovered in 1972 by Rudolf Bayer

Algorithm

Insert / delete may require complicated bookkeeping & rotations

Java collections

TreeMap, TreeSet use red-black trees

Types of Multi-way Search Trees

2-3 tree

Internal nodes have 2 or 3 children

Index search 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 … 2 T-

1 2 … 2T

c

a o s

Multi-way Search Trees

Search algorithm

  1. Compare key x to 1…k keys in node
  2. If x = some key then return node
  3. Else if (x < key j) search child j
  4. Else if (x > all keys) search child k+

Example

Search(17)

Multi-way Search Trees

Insert Example (for 2-3 tree)

Insert( 4 )

Multi-way Search Trees

Insert Example (for 2-3 tree)

Insert( 1 )

Split node Split parent