2015-t2 lecture 20 school of engineering and computer science, victoria university of wellington ...

23
2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis, and Thomas Kuehne, VUW COMP 103 Thomas Kuehne Introduction to Trees

Upload: gregory-hudson

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

3 Remaining Efficiency Challenge  Linear Linked Structures (LinkedList, LinkedStack, … )  adding / removal operations are O(1)   but random access is expensive   Why?  reducing a search or access problem by 1 and leaving a subproblem of size n-1 is not a good divide & conquer strategy  This is why a naïve QuickSort implementation can be slow (O(n 2 ) in the worst case)

TRANSCRIPT

Page 1: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria

University of Wellington

Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis, and Thomas Kuehne, VUW

COM

P 10

3

Thomas Kuehne

Introduction to Trees

Page 2: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

2RECAP

RECAP Used linked lists to implement linear data structures Efficiency issues still remain

TODAY Introduction to Trees Reading: Chapter 16 in textbook

Page 3: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

3

Remaining Efficiency Challenge

Linear Linked Structures (LinkedList, LinkedStack, …) adding / removal operations are O(1) but random access is expensive

Why? reducing a search or access problem by 1

and leaving a subproblem of size n-1 is not a good divide & conquer strategy

This is why a naïve QuickSort implementation can be slow (O(n2) in the worst case)

Page 4: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

4Divide & Conquer

Challenge Guess the secret animal with as few questions

as possible Strategy

eliminate as many as possible in each step

TobyTiger

LeaLion

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Egg LayingMammal

ReptileBird

Page 5: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

5Divide & Conquer

Linear access is slow only one candidate eliminated at a time

TobyTiger

LeaLion

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Page 6: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

6Divide & Conquer

Linear access is slow only one candidate eliminated at a time

Hierarchical access is fast many (proportional to total amount) eliminated

at a time

TobyTiger

LeaLion

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Egg LayingMammal

ReptileFeline Canine Bird

Page 7: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

7

From Linear to Hierarchical Access

Linear linkage structure split into one head and the rest

TobyTiger

LeaLion

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Page 8: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

8

From Linear to Hierarchical Access

Hierarchical linkage structure split into parts, each containing

multiple elements

TobyTiger

LeaLion

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Egg LayingMammal

ReptileFeline Canine Bird

Animal

Page 9: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

9

From Linear to Hierarchical Access

Hierarchical linkage structure split into parts, each containing

multiple elements

TobyTiger

LeaLion

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Egg LayingMammal

ReptileFeline Canine Bird

Animal

Page 10: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

10

Trees are Hierarchical Structures

Upside Down Trees?

Feline

LeoLion

TobyTiger

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Egg LayingMammal

ReptileCanine Bird

Animal

Page 11: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

11

Some Terminology

Trees are Hierarchical Structures

Feline

LeoLion

TobyTiger

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Egg Laying Mammal

Reptile CanineBird

Animal

root

leaves

branch

Page 12: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

12

Trees are Hierarchical Structures

Same Terminology, despite different orientation

Feline

LeoLion

TobyTiger

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Egg LayingMammal

ReptileCanine Bird

Animalroot

leaves

branch

Implementation with LinkedNode++ support multiple successors, instead of just

one

Page 13: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

13

Feline

LeoLion

TobyTiger

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Egg LayingMammal

ReptileCanine Bird

Animal

Implementation as a Linked Structure

Implementation with LinkedNode++ support multiple successors, instead of just

one

Page 14: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

14

Generalised LinkedNode

Representing trees in Java

M

Linked List Nodes

F C

Page 15: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

15

Generalised LinkedNode

Representing trees in Java

Binary Tree Nodes

M

F C

T L

Page 16: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

16

Generalised LinkedNode

Representing trees in Java

General Tree Nodes

F…

…… C

……

M…

……

T…

…… L

……

some collection type(ordered or unordered)

Page 17: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

17

Arrays It is possible to represent trees with arrays No reference overhead! Clever assignment of nodes to array indeces We’ll probably won’t cover this (outside heaps) Textbook → Ch. 16.3

Representing trees in Java

Page 18: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

18More Tree Examples

Other Taxonomies e.g. game genres

Organisational Charts CEO, managers, employees, slaves, …

Filing systems e.g., the folder structure of your hard drive

Computer Graphics models Octrees, for partitioning 3D space

hierarchical structuresnaturally represented with trees(rather than using trees as an

access technique)

Page 19: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

19

Other Taxonomies e.g. game genres

Organisational Charts CEO, managers, employees, slaves, …

Filing systems e.g., the folder structure of your hard drive

Computer Graphics models Octrees, for partitioning 3D space

More Tree Examples

Page 20: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

20More Tree Examples

Other Taxonomies e.g. game genres

Organisational Charts CEO, managers, employees, slaves, …

Filing systems e.g., the folder structure of your hard drive

Computer Graphics models Octrees, for partitioning 3D space

Decision processes …

Page 21: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

21Planning

Tic Tac Toe search tree for

moves

XX XX X X …

O XO X XO

XO

XO

X

O…

often not represented explicitly;

only implicitly “created” by recursion

Page 22: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

22More Tree Terminology

A tree is a collection of items in a strict hierarchical structure.

Each item is in a node of the tree. The root is at the top. The leaves are at the bottom. Each node may have child nodes – except a leaf, which

has none. Each node has one parent - except the root, which has

none. An edge joins a node to its parent – may be labelled. A subtree is a node plus all its descendents. The depth of a node is its distance from the root. The height or depth of a tree is the depth of the lowest

leaf. Level = set/list of nodes at the same depth. Branch = sequence of nodes on a path from root to a

leaf.

Children may be ordered/unordered

Tree may or may not store explicit parent

references

Page 23: 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,

23Terminology visualised

K

G

C I M Q

O

A E

node, root, leaf, child, parent, edge, subtree, depth, height, level, branch,siblings, ancestors, descendants, cousins, …