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

Post on 18-Jan-2018

215 views 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 of 2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington ...

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

2RECAP

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

TODAY Introduction to Trees Reading: Chapter 16 in textbook

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)

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

5Divide & Conquer

Linear access is slow only one candidate eliminated at a time

TobyTiger

LeaLion

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

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

7

From Linear to Hierarchical Access

Linear linkage structure split into one head and the rest

TobyTiger

LeaLion

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

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

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

10

Trees are Hierarchical Structures

Upside Down Trees?

Feline

LeoLion

TobyTiger

BullyBulldog

CarrieCollie

TanjaTui

KurtKaka

TimTurtle

SallySnake

Egg LayingMammal

ReptileCanine Bird

Animal

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

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

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

14

Generalised LinkedNode

Representing trees in Java

M

Linked List Nodes

F C

15

Generalised LinkedNode

Representing trees in Java

Binary Tree Nodes

M

F C

T L

16

Generalised LinkedNode

Representing trees in Java

General Tree Nodes

F…

…… C

……

M…

……

T…

…… L

……

some collection type(ordered or unordered)

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

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)

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

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 …

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

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

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, …