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
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 …
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)
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
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
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
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
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
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
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
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
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
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
Representing trees in Java Generalised LinkedNode Linked List Nodes M F C
Representing trees in Java Generalised LinkedNode Binary Tree Nodes M F C T L
Representing trees in Java Generalised LinkedNode some collection type (ordered or unordered) General Tree Nodes M … F … C … T … L …
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
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
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
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 …
Planning Tic Tac Toe search tree for moves X … O X …
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.
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, …