Download presentation
Presentation is loading. Please wait.
Published byGregory Hudson Modified over 9 years ago
1
2015-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 COMP 103 Thomas Kuehne Introduction to Trees
2
2 RECAP Used linked lists to implement linear data structures Efficiency issues still remain TODAY Introduction to Trees Reading: Chapter 16 in textbook
3
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(n 2 ) in the worst case)
4
4 Divide & Conquer Challenge Guess the secret animal with as few questions as possible Strategy eliminate as many as possible in each step Toby Tiger Lea Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake Egg Laying Mammal Reptile Bird
5
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
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 Toby Tiger Lea Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake Egg Laying Mammal Reptile Feline Canine Bird
7
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
8 From Linear to Hierarchical Access Hierarchical linkage structure split into parts, each containing multiple elements Toby Tiger Lea Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake Egg Laying Mammal Reptile Feline Canine Bird Animal
9
9 From Linear to Hierarchical Access Hierarchical linkage structure split into parts, each containing multiple elements Toby Tiger Lea Lion Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake Egg Laying Mammal Reptile Feline Canine Bird Animal
10
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
11 Some Terminology Trees are Hierarchical Structures 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 leaves branch
12
12 Trees are Hierarchical Structures Same Terminology, despite different orientation 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 leaves branch Implementation with LinkedNode++ support multiple successors, instead of just one
13
13 Feline Leo Lion Toby Tiger Bully Bulldog Carrie Collie Tanja Tui Kurt Kaka Tim Turtle Sally Snake Egg Laying Mammal Reptile Canine Bird Animal Implementation as a Linked Structure Implementation with LinkedNode++ support multiple successors, instead of just one
14
14 Generalised LinkedNode Representing trees in Java M Linked List Nodes FC
15
15 Generalised LinkedNode Representing trees in Java Binary Tree Nodes M FCTL
16
16 Generalised LinkedNode Representing trees in Java General Tree Nodes F … … … C … … … M … … … T … … … L … … … some collection type (ordered or unordered)
17
17 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 (outside heaps) Textbook → Ch. 16.3 Representing trees in Java
18
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 hierarchical structures naturally represented with trees (rather than using trees as an access technique) hierarchical structures naturally represented with trees (rather than using trees as an access technique)
19
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
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 ……
21
21 Planning Tic Tac Toe search tree for moves XXX XXX … OXOXX O X O X O X O … often not represented explicitly; only implicitly “created” by recursion
22
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
23 Terminology visualised K K G G C C I I M M Q Q O O A A E E node, root, leaf, child, parent, edge, subtree, depth, height, level, branch, siblings, ancestors, descendants, cousins, …
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.