![Page 1: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/1.jpg)
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
![Page 2: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/2.jpg)
2
RECAPRECAP Used linked lists to implement linear data structures Efficiency issues still remain
TODAY Introduction to Trees Reading: Chapter 16 in textbook
Announcements
![Page 3: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/3.jpg)
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: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/4.jpg)
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
![Page 5: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/5.jpg)
5
Divide & Conquer
Linear access is slow only one candidate eliminated at a time
TobyTiger
LeaLion
BullyBulldog
CarrieCollie
TanjaTui
KurtKaka
TimTurtle
SallySnake
![Page 6: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/6.jpg)
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
![Page 7: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/7.jpg)
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: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/8.jpg)
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: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/9.jpg)
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: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/10.jpg)
10
Trees are Hierarchical Structures
Upside Down Trees?
Feline
LeoLion
TobyTiger
BullyBulldog
CarrieCollie
TanjaTui
KurtKaka
TimTurtle
SallySnake
Egg LayingMammal
ReptileCanine Bird
Animal
![Page 11: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/11.jpg)
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: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/12.jpg)
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
![Page 13: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/13.jpg)
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: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/14.jpg)
14
Generalised LinkedNode
Representing trees in Java
M
Linked List Nodes
F C
![Page 15: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/15.jpg)
15
Generalised LinkedNode
Representing trees in Java
Binary Tree Nodes
M
F C
T L
![Page 16: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/16.jpg)
16
Generalised LinkedNode
Representing trees in Java
General Tree Nodes
F…
…… C
…
……
M…
……
T…
…… L
…
……
some collection type(ordered or unordered)
![Page 17: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/17.jpg)
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
![Page 18: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/18.jpg)
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
![Page 19: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/19.jpg)
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: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/20.jpg)
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)
![Page 21: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/21.jpg)
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
![Page 22: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/22.jpg)
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
![Page 23: 2014-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,](https://reader036.vdocument.in/reader036/viewer/2022062719/56649ecf5503460f94bdc276/html5/thumbnails/23.jpg)
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, …