Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal...

32
Lecture 6: Trees Tree as an ADT Lecture 6: Trees Tree as an ADT Special case of Trees: Binary trees Binary trees Tree Traversal methods Euler tour (new) Euler tour (new) Applications of trees Data Structures for Representing Trees Data Structures for Representing Trees 1 Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie

Transcript of Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal...

Page 1: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Lecture 6: Trees

Tree as an ADT

Lecture 6: Trees

Tree as an ADTSpecial case of Trees:

Binary treesBinary treesTree Traversal methods

Euler tour (new)Euler tour (new)

Applications of treesData Structures for Representing TreesData Structures for Representing Trees

1Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie

Page 2: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

What is a Tree

In computer science, a t i ADT hi htree is an ADT which stores elements hierarchically

Computers”R”Us

A tree consists of nodes with a parent-childrelation

Sales R&DManufacturing

Applications:Organization chartsFil t

Laptops DesktopsUS International

Europe Asia CanadaFile systemsProgramming environments

Europe Asia Canada

2

Page 3: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Another Tree Example

Unix/DosUnix/Dos File System:

3

Page 4: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Tree Definition

If a tree T is non-empty, it Ahas a root

Each node v of T different B DC

from the root has a unique parent node

h bG HE F

Note that a tree can be empty according to this definition I J K

4

Page 5: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Tree TerminologyRoot:

Internal node: A depth 0

External node (a.k.a. leaf ):

Ancestors of a node:

D th f dB DCdepth 1

p

Depth of a node: number of ancestors

G HE Fdepth 2

Height of a tree:

I J Kdepth 3

maximum depth of any node (3 in this example)

Descendant of a node: depth 3Descendant of a node:

Siblings:

5

Page 6: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Tree Terminology

Subtree of tree T rooted at node v is the tree

f fA

consisting of v itself and all of the descendants of v

An edge of tree T is aB DC

An edge of tree T is a pair of nodes (u,v) s.t. u is the parent of v

subtree

G HE FA path in T is a sequence of nodes s t any two subtree

rooted at CI J Ks.t. any two

consecutive nodes form an edge

6

Page 7: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Ordered TreeA

1st child 2nd child 3rd childB DC

1st child 2 child

1st child 2nd childG HE F

1st child 2nd child

I J K1st child 2nd child 3rd child

Tree is ordered if there is a linear ordering defined for the children of each node, that is we can identify children of a node as being first second third so on

7

children of a node as being first, second, third, so on.

Page 8: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Tree ADT

We use positions to abstract d

Query methods:nodes

Generic methods:integer size()

boolean isInternal(p)boolean isExternal(p)boolean isRoot(p)

integer size()boolean isEmpty()Iterator elements()Iterator positions()

Update method:object replace (p, o)

Iterator positions()

Accessor methods:position root()

Additional update methods may be defined by data structures implementing the T ADTposition parent(p)

positionIterator children(p)

Tree ADT

8

Page 9: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Systematic Tree Traversal: Preorder Traversal

In a preorder traversal, a node is visited before its descendants

Algorithm preOrder(v)visit(v)for each child w of vIf visit(v) is O(1), then the

complexity of preorder is O(n)

for each child w of vpreOrder (w)

P d li d i f th d h t• Produce a linear ordering of the nodes where parents must always come before their children in the ordering

Make Money Fast!1

2 5 91. Motivations References2. Methods

2.1 Stock 2.2 Ponzi1 1 Greed 1 2 Avidity 2.3 Bank3 4 6 7 8

9

Fraud Scheme1.1 Greed 1.2 Avidity Robbery

Page 10: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Preorder Traversal Application: Print a Structured Doc

Algorithm printDoc(v, indent)print(newline,indent, v)f f Call to print(root ””)for each child w of v

printDoc(w, indent+tab)

Call to print(root, )

Computers”R”Us Computers”R”UsSales

USSales R&DManufacturing

USInternational

EuropeAsia

Laptops DesktopsUS International CanadaManufacturing

LaptopsDesktops

10

Europe Asia CanadaDesktops

R&D

Page 11: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Systematic Tree Traversal: Postorder TraversalPostorder Traversal

In a postorder traversal, a node Algorithm postOrder(v)

for each child w of vis visited after its descendants

If visit(v) is O(1), then the complexity of postorder is O(n)

for each child w of vpostOrder (w)

visit(v)complexity of postorder is O(n)

cs16/9

cs16/

homeworks/ todo.txt1Kprograms/

3 78

homeworks/ 1Kprograms/

DDR.java Stocks.javah1c.doc h1nc.doc Robot.java

1 2 4 5 6

11

j10K

j25K3K 2K

j20K

Page 12: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Postorder Traversal ApplicationCompute space used by files in a directory and its subdirectories

Algorithm Space(v)t = 0for each child w of v

t = t + Space (w)t (t) // “ i it( )”return (t) // “visit(v)”

12

Page 13: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Binary TreesA binary tree is a tree with the following properties:

Each internal node has at most two children

Atwo children If each internal node has exactly two children, it is called a proper or full binary tree B C

B is left child of A C right child of A

The children of a node are an ordered pair

We call the children of an internal node left child and right child

F GD E

right subtree of node A

node left child and right child, and corresponding subtrees left subtree and right subtree H I

left subtree of node AApplications:arithmetic expressionsdecision processes

13

searching

Page 14: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Arithmetic Expression TreeBinary tree associated with an arithmetic expression

internal nodes: operatorsl d dexternal nodes: operands

Example: arithmetic expression tree for the expression (2 × (a - 1) + (3 × b))(2 × (a 1) + (3 × b))

+

××

2 3 b−2

a 1

3 b

14

Page 15: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Decision TreeBinary tree associated with a decision process

internal nodes: questions with yes/no answerexternal nodes: decisions

Example: dining decision

Want a fast meal?Y N

How about coffee? On expense account?

Yes No

Starbucks Spike’s Al Forno Café Paragon

Yes No Yes No

15

Page 16: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Properties of Binary Trees

(# external nodes) = (# internal nodes) + 1

(# nodes at level i) ≤ 2i( )

(# external nodes) ≤ 2 (height)

(# height) ≤ (# internal nodes) = ((# nodes) -1) /2(# height) ≤ (# internal nodes) = ((# nodes) 1) /2

level 0

level 1

level 2

16

level 3

Page 17: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

BinaryTree ADT

The BinaryTree ADT extends the Tree ADT, i.e., it inherits y , ,all the methods of the Tree ADT

Additional methods:position left(p)position right(p)boolean hasLeft(p)boolean hasLeft(p)boolean hasRight(p)

Update methods may be defined by dataUpdate methods may be defined by data structures implementing the BinaryTree ADT

17

Page 18: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Inorder Traversal for Binary Trees

In an inorder traversal a node is visited after its left subtree and 2

6

8its left subtree and before its right subtree 1

2

7 9

8

4

Algorithm inOrder(v)if hasLeft (v)

3 5

inOrder (left (v))visit(v)if hasRight (v)

If visit(v) is O(1), then the complexity of inOrder is O(n)if hasRight (v)

inOrder (right (v))inOrder is O(n)

18

Page 19: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Print Arithmetic Expressions

Specialization of an inorder traversal

p int ope and o ope atoAlgorithm printExpression(v)

print operand or operator when visiting nodeprint “(“ before traversing left subtree

g p p ( )if hasLeft (v)

print(“(’’)inOrder (left(v))left subtree

print “)“ after traversing right subtree

inOrder (left(v))print(v.element ())if hasRight (v)

inOrder (right(v))print (“)’’)

+

××

−2 3 b((2 × (a − 1)) + (3 × b))

19

a 1

Page 20: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Evaluate Arithmetic Expressions

Specialization of a postorder traversal

recursive method

Algorithm evalExpr(v)if isExternal (v)

recursive method returning the value of a subtreewhen visiting an internal

return v.element ()else

x ← evalExpr(leftChild (v))when visiting an internal node, combine the values of the subtrees

x ← evalExpr(leftChild (v))y ← evalExpr(rightChild (v))◊ ← operator stored at vpreturn x ◊ y+

×× ××

−2 3 2

20

5 1

Page 21: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Unified Tree Traversal FrameworkThe tree-traversal algorithms we had so far (postOrder, preOrder, inOrder for binary trees) are forms of iteratorsforms of iterators

Each of them visits the nodes in certain order, with exactly one visit per nodeexactly one visit per node

Can unify tree-traversal algorithms in a single framework, however by relaxing the requirement that each node be visited exactly once

The resulting traversal is called Euler tour traversal

Euler tour traversal allows for more general kinds of algorithm to be expressed easily

21

Page 22: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Euler Tour TraversalGeneric traversal of a binary tree

Includes special cases of preorder, postorder and inorder traversals

Walk around the tree and visit each node three times:on the left (preorder)from below (inorder)from below (inorder)on the right (postorder) +

L R ×

−2 3 2

LB

R×If edges of tree viewed as “walls”, then we always

5 1

ykeep them to our left

22

Page 23: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Euler Tour TraversalAlgorithm EulerTour(T, v)

perform action for visiting node v on the leftf f ( )if T.hasLeft (v) then

EulerTour(T, T.left)perform action for visiting node v from belowperform action for visiting node v from belowif T.hasRight (v) then

EulerTour(T, T.right)

For preorder, no action performed when visiting from below and on the right

perform action for visiting node v on the right

For preorder, no action performed when visiting from below and on the rightFor postorder, no action performed when visiting on the left and from belowFor inorder, no action performed when visiting on the right and on the leftCan perform other kind of traversals with Euler Tour

23

Can perform other kind of traversals with Euler Tour

If the “visiting action” is O(1) then running time of the Euler Tour is O(n)

Page 24: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Euler Tour Application: Compute Number of Descendants for Each Node

global variable T.counter, initialize T.counter = 0each node has a local variable counterVisit from left: set counter = T.counter; increment T.counterVisit from right: number of descendants v.d =T counter - counter -1Visit from right: number of descendants v.d T.counter counter 1

T.c = 1 c=0 T.c = 9v.d =8

T.c = 2

T 4

T.c = 7

T 8

T.c = 9v.d =2c=1

3 7

c=6

v.d 8

T.c =6v.d =4

T.c = 3 T.c =3v.d =0

T.c = 4 T.c = 6v.d =2

T.c = 8 T.c = 8v.d =0

T.c = 9 T.c = 9v.d =0

c=2c=3 c=7 c=8

T.c = 5 T.c = 6T.c =5d 0 T.c = 6c=4 c=5

24

T.c 6v.d =0 v.d =0c 5

Page 25: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Euler Tour Traversal: ApplicationsEuler tour to count number of descendants for each node

Start by initializing T.counter = 0T counter stores the number of nodes seen so farT.counter stores the number of nodes seen so far

Call countEuler(T, root)

Algorithm countEuler(T v)Algorithm countEuler(T,v)count = T.counter; //number of nodes seen so far (not in subtree of node v)T.counter++; //saw one more node, namely node vif T.hasLeft (v) then

countEuler(T,T.left) //explore nodes to the left of vif T.hasRight (v) theng ( )

countEuler(T,T.right) //explore nodes to the right of v//at this point we explored all descendants of node vv numberOfdescendants = T counter – count – 1;

25

v.numberOfdescendants T.counter count 1; //num. of nodes seen so far – num. of nodes seen before v – 1 for node v itself

Page 26: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Linked Structure for General Trees

A node is represented by an object storing

Element ∅ElementParent nodeSequence of children nodes

Node objects implement

B

Node objects implement the Position ADT

B

FD

AB

DA FDA

C E

F

∅ ∅

26

C E

Page 27: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Linked Structure for Binary Trees

A node is represented by an object storing

Element ∅ElementParent nodeLeft child nodeRight child node B

Right child node

Node objects implement the Position ADT

∅ ∅

B

B A D

DA

C E

∅ ∅∅ ∅

27

C E EC

Page 28: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Linked Structure for Binary Trees: Complexity

operations size, isEmpty, replace, root, parent, children, l ft i ht h L ft h Ri ht i I t l i E l i R tleft, right, hasLeft, hasRight, isInternal, isExernal, isRoottake O(1) time

Ope ations l t iti a e O( ) timeOperations elements, positions are O(n) time

Space usage is O(n)

28

Page 29: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Array-Based Representation of Binary Trees

nodes are stored in an array 1A

…2 3

DB

let index(node) be defined as follows:index(root) = 1if node q is the left child of node p then

6 74 5FE C Jif node q is the left child of node p then

index(q) = 2*index(p)if node q is the right child of node p, then index(q) = 2 *index (p)+1 10 11index(q) = 2 index (p)+1 10 11

HG

A B D E F C J G H

29

A B D E F1 2 3 4 50

C J G6 7

H8 9 10 11

Page 30: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Array-Based Binary Trees

If nodes have the maximum number of children at each d th ( t th l t d th) thi i l t ti idepth (except the largest depth), this implementation is efficient. Otherwise, a lot of space will be wasted

depth 1nodes

depth 2 nodes depth 3 nodes

rootnodes nodes

30

Page 31: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Array-Based Binary Trees: Complexity

Let n be the number of nodes in the treeIt can be shown that in the worst case, the maximum size of the array 2nthe array = 2n

This is the case when each node has only one child

Thus, for general binary trees, the exponential worst-caseThus, for general binary trees, the exponential worst case space requirement is prohibitive

There are special cases when the array implementation is very ffi i t d h ld b f d th li k d i l t tiefficient and should be favored over the linked implementation

The case when at each depth (except the last one) there are as many nodes as possible

With array implementation, operations size, isEmpty, replace, root, parent, children, left, right, hasLeft, hasRight, isInternal, isExernal, isRoot take O(1) time

31

, ( )

Operations elements, positions are O(n) time

Page 32: Lecture 6: Trees - ORCCAyxie/courses/cs2210b-2011/htmls/notes/06-tree.pdf · Euler Tour Traversal Algorithm EulerTour ... Euler Tour Traversal: Applications Euler tour to count number

Linked Structure vs. Array-Based Structure

Linked structure Array-basedGood choice for general trees

only for binary trees

Should only be used for special type of binary trees we call

Good choice for binary trees

type of binary trees we call complete, that is as many children at each depth (except the last one) as possible) p

Should be the preferred structure for complete binary trees, since in general array structures arein general, array structures are more efficient than linked structures both in terms of space and in terms of time

32

and in terms of time