2014-t2 lecture 20 school of engineering and computer science, victoria university of wellington ...
TRANSCRIPT
2014-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
CO
MP 1
03
Marcus Frean
Introduction to Trees
2
RECAPRECAP Used linked lists to implement linear data structures Efficiency issues still remain
TODAY Introduction to Trees Reading: Chapter 16 in textbook
Announcements
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)
4
Divide & 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
5
Divide & Conquer
Linear access is slow only one candidate eliminated at a time
TobyTiger
LeaLion
BullyBulldog
CarrieCollie
TanjaTui
KurtKaka
TimTurtle
SallySnake
6
Divide & 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
Animal
root
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 indices (we should get to this later on)
Representing trees in Java
18
More 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
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
20
More 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 …
hierarchical structuresnaturally represented with trees(rather than using trees as an
access technique)
21
Planning
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
22
More 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
23
Terminology 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, …