Top Banner
© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected] Trees CS 220 Data Structures & Algorithms Dr. Ian Watson
33

© University of Auckland ian/ [email protected] Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

Dec 29, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

Trees

CS 220Data Structures & Algorithms

Dr. Ian Watson

Page 2: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

2

Non computer scienceview of a tree

leaves

branches

root

Page 3: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

3

The computer science view

root

branches

leaves

Page 4: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

4

Linear Lists And Trees Linear lists are useful for serially ordered

data. (e0, e1, e2, …, en-1) Days of week. Months in a year. Students in this class.

Trees are useful for hierarchically ordered data. Employees of a corporation.

President, vice presidents, managers, and so on. Java’s classes.

Object is at the top of the hierarchy. Subclasses of Object are next, and so on.

Page 5: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

5

Hierarchical Data & Trees

The element at the top of the hierarchy is the root

Elements next in the hierarchy are the children of the root

Elements next in the hierarchy are the grandchildren of the root, and so on…

Elements at the lowest level of the hierarchy are the leaves

Page 6: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

6

descendant of root

grand children of root

children of root

Java’s Classes

Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException

root

Page 7: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

7

Definition A tree t is a finite nonempty set of

elements One of these elements is called the

root The remaining elements, if any,

are partitioned into trees, which are called the subtrees of t

Page 8: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

8

SubtreesObject

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException

root

Page 9: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

9Leaves

Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException

Page 10: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

10Parent, Grandparent, Siblings, Ancestors, Descendents

Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException

Page 11: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

11

Levels

Level 4

Level 3

Level 2

Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException

Level 1

Page 12: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

12

Caution Some CS texts start level numbers

at 0 rather than at 1 Root is at level 0 Its children are at level 1 The grand children of the root are

at level 2 And so on

Page 13: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

13height = depth = number of levels

Level 4

Level 3

Level 2

Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException

Level 1

Page 14: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

14Node Degree = Number Of Children

Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException

3

2 1 1

0 0 1 0

0

Page 15: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

15Tree Degree = Max Node Degree

Object

Number Throwable OutputStream

Integer Double Exception FileOutputStream

RuntimeException

3

2 1 1

0 0 1 0

0Degree of tree = 3

Page 16: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

16

Binary Tree Finite (possibly empty) collection of

elements A nonempty binary tree has a root

element The remaining elements (if any) are

partitioned into two binary trees These are called the left and right

subtrees of the binary tree

Page 17: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

17Differences Between a Tree& a Binary Tree No node in a binary tree may have a

degree more than 2 There is no limit on the degree of a node

in a tree A binary tree may be empty; a tree

cannot be empty The subtrees of a binary tree are ordered Those of a tree are not ordered

Page 18: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

18

The subtrees of a binary tree are ordered; those of a tree are not ordered.

a

b

a

b

• Trees 1 & 2 are different when viewed as binary trees (because of left right ordering).

• But they are the same when viewed as trees

1 2

Differences Between a Tree& a Binary Tree

Page 19: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

19

Arithmetic Expressions

(a + b) * (c + d) + e – f/g*h + 3.25 Expressions comprise three kinds of

entities. Operators (+, -, /, *). Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c

+ d), etc.). Delimiters ((, )).

Page 20: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

20

Operator Degree

Number of operands that the operator requires.

Binary operator requires two operands. a + b c / d e - f

Unary operator requires one operand. + g - h

Page 21: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

21

Infix Form

Normal way to write an expression. Binary operators come in between

their left and right operands. a * b a + b * c a * b / c (a + b) * (c + d) + e – f/g*h + 3.25

Page 22: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

22

Operator Priorities

How do you figure out the operands of an operator? a + b * c a * b + c / d

This is done by assigning operator priorities. priority(*) = priority(/) > priority(+) = priority(-)

When an operand lies between two operators, the operand associates with the operator that has higher priority.

Page 23: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

23

Tie Breaker

When an operand lies between two operators that have the same priority, the operand associates with the operator on the left. a + b - c a * b / c / d

Page 24: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

24

Delimiters

Sub-expression within delimiters is treated as a single operand, independent from the remainder of the expression. (a + b) * (c – d) / (e – f)

single operand

Page 25: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

25

Infix Expression Is Hard To Parse

Need operator priorities, apply tie breaker, and find delimiters.

This makes computer evaluation more difficult than is necessary.

Postfix and prefix expression forms do not rely on operator priorities, a tie breaker, or delimiters (eg Polish notation)

So it is easier for a computer to evaluate expressions that are in these forms.

Page 26: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

26

Postfix Form The postfix form of a variable or

constant is the same as its infix form. a, b, 3.25

The relative order of operands is the same in infix and postfix forms.

Operators come immediately after the postfix form of their operands. Infix = a + b Postfix = ab+

Page 27: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

27

Postfix Examples

• Infix = a * b + c Postfix = a b * c +

• Infix = (a + b) * (c – d) / (e + f) Postfix = a b + c d - * e f + /

postfix

postfix

Page 28: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

28

Postfix Evaluation Scan postfix expression from left to right

pushing operands on to a stack. When an operator is encountered, pop as

many operands as this operator needs; evaluate the operator & push the result

back on to the stack. This works because, in postfix, operators

come immediately after their operands.

Page 29: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

29

Postfix Evaluation (a + b) * (c – d) / (e +

f) a b + c d - * e f + / a b + c d - * e f + /

stack

a

a b + c d - * e f + /

b

a b + c d - * e f + /

Page 30: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

30

Postfix Evaluation (a + b) * (c – d) / (e +

f) a b + c d - * e f + / a b + c d - * e f + /

stack

(a + b)

a b + c d - * e f + /

a b + c d - * e f + / a b + c d - * e f + / c a b + c d - * e f + /

d

a b + c d - * e f + /

Page 31: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

31

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + /

stack

(a + b)

a b + c d - * e f + /

(c – d)

Page 32: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

32

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + /

stack

(a + b)*(c – d)

a b + c d - * e f + /

e

a b + c d - * e f + / a b + c d - * e f + / f a b + c d - * e f + /

Page 33: © University of Auckland ian/ ian@cs.auckland.ac.nz Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

© University of Auckland www.cs.auckland.ac.nz/~ian/ [email protected]

33

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + /

stack

(a + b)*(c – d)

a b + c d - * e f + /

(e + f)

a b + c d - * e f + / a b + c d - * e f + / a b + c d - * e f + / a b + c d - * e f + /