Download COMP 103 Lecture 1 - School of Engineering and Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COMP 103
Introduction to Trees
 Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis, and Thomas Kuehne, VUW
2014-T2 Lecture 20
Marcus Frean
School of Engineering and Computer Science, Victoria University of Wellington
RECAP
2
RECAP

Used linked lists to implement linear data structures

Efficiency issues still remain
TODAY

Introduction to Trees

Reading: Chapter 16 in textbook
Announcements
Remaining Efficiency Challenge
3

Linear Linked Structures (LinkedList, LinkedStack, …)
 adding
 but

/ removal operations are O(1)
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)
Divide & Conquer
4

Challenge
 Guess

the secret animal with as few questions as possible
Strategy
 eliminate
as many as possible in each step
Mammal
Egg Laying
Bird
Toby
Tiger
Lea
Lion
Bully
Bulldog
Carrie
Collie
Tanja
Tui
Reptile
Kurt
Kaka
Tim
Turtle
Sally
Snake
Divide & Conquer
5

Linear access is slow
 only
Toby
Tiger
one candidate eliminated at a time
Lea
Lion
Bully
Bulldog
Carrie
Collie
Tanja
Tui
Kurt
Kaka
Tim
Turtle
Sally
Snake
Divide & Conquer
6

Linear access is slow
 only

one candidate eliminated at a time
Hierarchical access is fast
 many
(proportional to total amount) eliminated at a time
Mammal
Feline
Toby
Tiger
Egg Laying
Canine
Lea
Lion
Bully
Bulldog
Carrie
Collie
Bird
Tanja
Tui
Reptile
Kurt
Kaka
Tim
Turtle
Sally
Snake
From Linear to Hierarchical Access
7

Linear linkage
 structure
Toby
Tiger
Lea
Lion
split into one head and the rest
Bully
Bulldog
Carrie
Collie
Tanja
Tui
Kurt
Kaka
Tim
Turtle
Sally
Snake
From Linear to Hierarchical Access
8

Hierarchical linkage
 structure
split into parts, each containing multiple elements
Animal
Mammal
Feline
Toby
Tiger
Egg Laying
Canine
Lea
Lion
Bully
Bulldog
Carrie
Collie
Bird
Tanja
Tui
Reptile
Kurt
Kaka
Tim
Turtle
Sally
Snake
From Linear to Hierarchical Access
9

Hierarchical linkage
 structure
split into parts, each containing multiple elements
Animal
Mammal
Feline
Toby
Tiger
Egg Laying
Canine
Lea
Lion
Bully
Bulldog
Carrie
Collie
Bird
Tanja
Tui
Reptile
Kurt
Kaka
Tim
Turtle
Sally
Snake
Trees are Hierarchical Structures
10

Upside Down Trees?
Animal
Mammal
Canine
Feline
Toby
Tiger
Egg Laying
Leo
Lion
Bully
Bulldog
Carrie
Collie
Bird
Tanja
Tui
Reptile
Kurt
Kaka
Tim
Turtle
Sally
Snake
root
Animal
Mammal
Egg Laying
Canine
Feline
Toby
Tiger
branch
Leo
Lion
Bully
Bulldog
Bird
Carrie
Collie
Tanja
Tui
Reptile
Kurt
Kaka
Tim
Turtle
Sally
Snake

leaves
Some Terminology
11
Trees are Hierarchical Structures
Trees are Hierarchical Structures
12

Same
Terminology,
different orientation
Implementation
withdespite
LinkedNode++
 support
multiple successors, instead of just one
root
branch
Animal
leaves
Mammal
Canine
Feline
Toby
Tiger
Egg Laying
Leo
Lion
Bully
Bulldog
Carrie
Collie
Bird
Tanja
Tui
Reptile
Kurt
Kaka
Tim
Turtle
Sally
Snake
Implementation as a Linked Structure
13

Implementation with LinkedNode++
 support
multiple successors, instead of just one
Animal
Mammal
Canine
Feline
Toby
Tiger
Egg Laying
Leo
Lion
Bully
Bulldog
Carrie
Collie
Bird
Tanja
Tui
Reptile
Kurt
Kaka
Tim
Turtle
Sally
Snake
Representing trees in Java
14

Generalised LinkedNode
Linked List Nodes
M
F
C
Representing trees in Java
15

Generalised LinkedNode
Binary Tree Nodes
M
F
T
C
L
Representing trees in Java
16

Generalised LinkedNode
some collection type
(ordered or unordered)
General Tree Nodes
…
…
…
M
F
…
…
…
T
…
…
…
C
L
…
…
…
…
…
…
Representing trees in Java
17

Arrays
It is possible to represent trees with arrays
 No reference overhead!
 Clever assignment of nodes to array indices
 (we should get to this later on)

More Tree Examples
18

Other Taxonomies
 e.g.

game genres
Organisational Charts
 CEO,

Filing systems
 e.g.,

managers, employees, slaves, …
the folder structure of your hard drive
Computer Graphics models

Octrees, for partitioning 3D space
More Tree Examples
19

Other Taxonomies
 e.g.

game genres
Organisational Charts
 CEO,

Filing systems
 e.g.,

managers, employees, slaves, …
the folder structure of your hard drive
Computer Graphics models

Octrees, for partitioning 3D space
More Tree Examples
20

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
 …

hierarchical structures
naturally represented with trees
(rather than using trees as an
access technique)
Planning
21

Tic Tac Toe
 search
often not represented
explicitly;
only implicitly “created”
by recursion
tree for
moves
X
X
X
X
O
X
OX
X
X
O
X
O
X
X
O
X
O
…
…
More Tree Terminology
22












A tree is a collection of items in a strict hierarchical structure.
Each item is in a node of the tree.
Children may be
ordered/unordered
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.
Tree may or may not
A subtree is a node plus all its descendents.
store explicit parent
references
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.
Terminology visualised
23
K
O
G
C
A
I
M
Q
E
node, root, leaf, child, parent, edge, subtree, depth, height, level, branch,
siblings, ancestors, descendants, cousins, …