1
COMP 250
Lecture 19
(rooted) trees
- Oct. 23, 2017
(rooted) trees Oct. 23, 2017 1 Linear Data Structures linked list - - PowerPoint PPT Presentation
COMP 250 Lecture 19 (rooted) trees Oct. 23, 2017 1 Linear Data Structures linked list array Non-Linear Data Structures tree graph 2 Tree Example: Organization Hierarchy (McGill) 3
1
2
3
person Child 1 Child 2 Child 3
4
person mom dad mom’s mom mom’s dad
dad’s mom dad’s dad
5
6
7
8
root
9
10
11
Most of definitions today will assume edges are from parent to child.
12
13
14
15
16
This definition does NOT assume edges are from parent to child.
(e.g. files)
17
18
19
20
21
root
22
23
24
This requires parent links. This is analogous to a ‘prev’ link in a doubly linked list.
1 3 1 1 2 1
25
1 3 1
0? 2 1
26
1 3 1
00 2 1
27
28
4
2 1 3 1 1 2 1
29
30
31
32
class Tree<T>{ TreeNode<T> root; : // inner class class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; : } }
33
34
class Tree<T>{ TreeNode<T> root; : // inner class class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; TreeNode<T> parent; : } }
class Tree<T>{ TreeNode<T> root; : // inner class class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; : } }
35
36
37
38
2 3 4 1 7 5 9 8
39
2 3 4 1 7 5 9 8
2 3 4 1 7 5 9 8
40
2 3 4 1 7 5 9 8
2 3 4 1 7 5 9 8