(rooted) trees Oct. 23, 2017 1 Linear Data Structures linked list - - PowerPoint PPT Presentation

rooted trees
SMART_READER_LITE
LIVE PREVIEW

(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


slide-1
SLIDE 1

1

COMP 250

Lecture 19

(rooted) trees

  • Oct. 23, 2017
slide-2
SLIDE 2

Linear Data Structures

Non-Linear Data Structures

linked list array tree graph

2

slide-3
SLIDE 3

Tree Example: Organization Hierarchy (McGill)

3

slide-4
SLIDE 4

person Child 1 Child 2 Child 3

Family Tree (descendents)

Here I ignore spouses (partner).

4

slide-5
SLIDE 5

person mom dad mom’s mom mom’s dad

Family Tree (ancestors)

dad’s mom dad’s dad

This is an example of a binary tree.

5

slide-6
SLIDE 6

Tree: UNIX file system

6

slide-7
SLIDE 7

Tree: Java Classes e.g. GUI

7

slide-8
SLIDE 8

root

Tree Terminology

node, vertex

8

A directed edge is ordered pair: (from, to)

slide-9
SLIDE 9

root

parent child

9

Every node except the root is a child, and has exactly one parent.

slide-10
SLIDE 10

10

For some trees,

  • edges are from parent to child
  • edges are from child to parent
  • edges are both from parent to child and child to

parent.

  • edge direction is ignored e.g. common with non-

rooted trees (see final slide of today)

slide-11
SLIDE 11

11

For some trees,

  • edges are from parent to child
  • edges are from child to parent
  • edges are both from parent to child and child to

parent.

  • edge direction is ignored e.g. common with non-

rooted trees (see final slide of today)

Most of definitions today will assume edges are from parent to child.

slide-12
SLIDE 12

Q: If a tree has N nodes, how many edges does it have ?

12

slide-13
SLIDE 13

Q: If a tree has N nodes, how many edges does it have ? A: N-1 Since every edge is of the form (parent, child), and each node except the root is a child and each child has exactly one parent.

13

slide-14
SLIDE 14

parent siblings

14

Two nodes are siblings if they have the same parent.

slide-15
SLIDE 15

Recursive definition of rooted tree…

15

slide-16
SLIDE 16

Recursive definition of rooted tree

16

A tree T is a finite (& possibly empty) set of 𝑜 nodes such that:

  • if 𝑜 > 0 then one of the nodes is the

root r

  • if 𝑜 > 1 then the 𝑜 − 1 non-root

nodes are partitioned into (non- empty) subsets T1, T2, …, Tk, each

  • f which is a tree (called a subtree”),

and the roots of the subtrees are the children of root r.

This definition does NOT assume edges are from parent to child.

slide-17
SLIDE 17

internal nodes (e.g. file directories) external nodes, leaves

(e.g. files)

17

slide-18
SLIDE 18

A path in a tree is a sequence of nodes (𝑤1, 𝑤2,…, 𝑤𝑙) such that (𝑤𝑗, 𝑤𝑗+1 ) is an edge. The length of a path is the number of edges in the path (number of nodes – 1)

18

slide-19
SLIDE 19

A path with just

  • ne node (𝑤1)

has length = 0, since it has no edges.

19

slide-20
SLIDE 20

What is the path length? (2 or 3?)

20

slide-21
SLIDE 21

ancestor descendent

21

Node v is an ancestor of node w if there is a path from v to w. Node w is a descendent of node v.

slide-22
SLIDE 22

root

depth (level) 1 2 3 4 The depth or level of a node is the length of the path from the root to the node.

22

slide-23
SLIDE 23

depth (level) 1 2 3 4

How to compute depth(v) ?

23

slide-24
SLIDE 24

24

depth( v ){ if ( v.parent == null) //root return 0 else return 1 + depth( v.parent ) }

This requires parent links. This is analogous to a ‘prev’ link in a doubly linked list.

slide-25
SLIDE 25

?

1 3 1 1 2 1

The height of a node is the maximum length of a path from that node to a leaf.

25

slide-26
SLIDE 26

2

1 3 1

? 1

0? 2 1

The height of a node is the maximum length of a path from that node to a leaf.

26

slide-27
SLIDE 27

? 2

1 3 1

11

00 2 1

The height of a node is the maximum length of a path from that node to a leaf.

27

slide-28
SLIDE 28

4

2 1 3 1 1 2 1

How to compute height(v) ?

28

slide-29
SLIDE 29

height(v){ if (v is a leaf) return 0 else{ h = 0 for each child w of v h = max(h, height(w)) return 1 + h } }

4

2 1 3 1 1 2 1

29

slide-30
SLIDE 30

How to implement a tree ?

class TreeNode<T>{ T element; }

30

slide-31
SLIDE 31

How to implement a tree ?

class TreeNode<T>{ T element; ArrayList< TreeNode<T> > children; TreeNode<T> parent; }

31

slide-32
SLIDE 32

Another common implementation: ‘first child, next sibling’

32

slide-33
SLIDE 33

More common implementation: ‘first child, next sibling’ (similar to singly linked lists)

class Tree<T>{ TreeNode<T> root; : // inner class class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; : } }

33

slide-34
SLIDE 34

More common implementation: ‘first child, next sibling’ (similar to singly linked lists)

34

class Tree<T>{ TreeNode<T> root; : // inner class class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; TreeNode<T> parent; : } }

slide-35
SLIDE 35

A tree of what? Each node has an element (not illustrated on right)

class Tree<T>{ TreeNode<T> root; : // inner class class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; : } }

35

slide-36
SLIDE 36

Exercise

36

A tree can be represented using lists, as follows:

tree = root | ( root listOfSubTrees ) listOfSubTrees = tree | tree listOfSubTrees

slide-37
SLIDE 37

Exercise

37

A tree can be represented using lists, as follows:

tree = root | ( root listOfSubTrees ) listOfSubTrees = tree | tree listOfSubTrees Draw the tree that corresponds to the following list, where the root elements are single digits.

( 6 ( 2 1 7 ) 3 ( 4 5 ) ( 9 8 0 ) )

slide-38
SLIDE 38

( 6 ( 2 1 7 ) 3 ( 4 5 ) ( 9 8 0 ) )

38

6

2 3 4 1 7 5 9 8

slide-39
SLIDE 39

( 6 ( 2 1 7 ) 3 ( 4 5 ) ( 9 8 0 ) )

39

6

2 3 4 1 7 5 9 8

6

2 3 4 1 7 5 9 8

slide-40
SLIDE 40

ASIDE: Non-rooted trees

40

6

2 3 4 1 7 5 9 8

6

2 3 4 1 7 5 9 8

You will see non-rooted trees mostly commonly when edges are not directed, and there is no natural way to define the ‘root’. You will see examples in COMP 251. Note the two trees below are the same.