Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 Introduction to Trees Thomas Kuehne 2013-T2 Lecture 19

Similar presentations


Presentation on theme: "COMP 103 Introduction to Trees Thomas Kuehne 2013-T2 Lecture 19"— Presentation transcript:

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

2 RECAP RECAP TODAY Announcements …
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 Strategy
Guess the secret animal with as few questions as possible Strategy eliminate as many as possible in each step Mammal Egg Laying Bird Reptile Toby Tiger Lea Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake

5 Divide & Conquer Linear access is slow
only one candidate eliminated at a time Toby Tiger Lea Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake

6 Divide & Conquer Linear access is slow Hierarchical access is fast
only one candidate eliminated at a time Hierarchical access is fast many (proportional to total amount) eliminated at a time Mammal Egg Laying Feline Canine Bird Reptile Toby Tiger Lea Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake

7 From Linear to Hierarchical Access
Linear linkage structure split into one head and the rest Toby Tiger Lea Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake

8 From Linear to Hierarchical Access
Hierarchical linkage structure split into parts, each containing multiple elements Animal Mammal Egg Laying Feline Canine Bird Reptile Toby Tiger Lea Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake

9 From Linear to Hierarchical Access
Hierarchical linkage structure split into parts, each containing multiple elements Animal Mammal Egg Laying Feline Canine Bird Reptile Toby Tiger Lea Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake

10 Trees are Hierarchical Structures
Upside Down Trees? Feline Leo Lion Toby Tiger Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake Egg Laying Mammal Reptile Canine Bird Animal

11 Trees are Hierarchical Structures
Some Terminology leaves Feline Leo Lion Toby Tiger Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake Egg Laying Mammal Reptile Canine Bird Animal root branch

12 Trees are Hierarchical Structures
Implementation with LinkedNode++ support multiple successors, instead of just one Same Terminology, despite different orientation root branch Feline Leo Lion Toby Tiger Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake Egg Laying Mammal Reptile Canine Bird Animal leaves

13 Implementation as a Linked Structure
Implementation with LinkedNode++ support multiple successors, instead of just one Animal Mammal Egg Laying Feline Canine Bird Reptile Toby Tiger Leo Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake

14 Representing trees in Java
Generalised LinkedNode Linked List Nodes M F C

15 Representing trees in Java
Generalised LinkedNode Binary Tree Nodes M F C T L

16 Representing trees in Java
Generalised LinkedNode some collection type (ordered or unordered) General Tree Nodes M F C T L

17 Representing trees in Java
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 Textbook → Ch. 16.3

18 More Tree Examples Other Taxonomies Organisational Charts
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 More Tree Examples Other Taxonomies Organisational Charts
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

20 More Tree Examples Other Taxonomies Organisational Charts
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

21 Planning Tic Tac Toe search tree for moves X O X

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


Download ppt "COMP 103 Introduction to Trees Thomas Kuehne 2013-T2 Lecture 19"

Similar presentations


Ads by Google