Postorder traversal

36
Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -

description

Postorder traversal. Ed. 2. and 3.: Chapter 6 – Ed. 4.: Chapter 7 -. Postorder Traversal. Algorithm postorder(T,v): for each child w of v call postorder(T,w) perform the “visit” action for node v. postorder(T,v). v. postorder(T,w). w. Postorder Traversal of a Binary Tree. - PowerPoint PPT Presentation

Transcript of Postorder traversal

Page 1: Postorder traversal

Postorder traversal

- Ed. 2. and 3.: Chapter 6 –- Ed. 4.: Chapter 7 -

Page 2: Postorder traversal

Postorder Traversal

T i tl e

P a p e r

A b s tra c t C h . 1

1 .1 1 .2

C h . 2

2 .1 2 .2 2 .3

C h . 3

3 .1 3 .2

R e fe re n c e s

S o fa r , w e h a v e le a rn tw o ty p e s t r a v e rs a ls : In o rd e r a n d p re o rd e r . I s th e re a n y o th e r w a y to p e r fo rm a t r a v e rs a l?

Page 3: Postorder traversal

postorder traversal of T: The subtrees rooted at its children are traversed recursively first. Then the root of T is visited. If the tree is ordered, then the subtrees are traversed according to the order of the children. The action of visiting a node: When we visit each node of T, what do we do? We can do anything that makes sense such as incrementing a counter or some complex computation based on the element at each node.

Page 4: Postorder traversal

E x a m p l e : P o s t o r d e r t r a v e r s a l o f a b o o k s t r u c t u r e

T i t l e

P a p e r

A b s t r a c t C h . 1

1 . 1 1 . 2

C h . 2

2 . 1 2 . 2 2 . 3

C h . 3

3 . 1 3 . 2

R e f e r e n c e s

Page 5: Postorder traversal

Algorithm postorder(T,v): for each child w of v

call postorder(T,w)perform the “visit” action for node v

v

w

postorder(T,v)

postorder(T,w)

Page 6: Postorder traversal

A Java implementation of postorder traversal for printing the elements. In this case, the action of visiting a node is printing the element. Postorder searching: public static String postorderPrint( InspectableTree T, Position v ) { String s = ""; PositionIterator children = T.children( v ); while( children.hasNext()) s += postorderPrint( T, children.nextPosition()) + " "; s += v.element().toString();

// elements must implement toString return s; }

Preorder searching: public static String preorderPrint( InspectableTree T, Position v ) { String s = v.element().toString(); PositionIterator children = T.children( v ); while( children.hasNext()) s += " " + preorderPrint( T, children.nextPosition()); return s; }

Page 7: Postorder traversal

Example: We have a directory cs016 that holds a file grades and a subdirectory homeworks. We want to use postorder traversal to calculate the disk usage.

cs016/2k

homeworks/1k

grades8k

hw13k

hw22k

hw34k

Page 8: Postorder traversal

size1 = 8k, size2 = 0k

cs016/2k

homeworks/1k

grades8k

hw13k

hw22k

hw34k

Page 9: Postorder traversal

size1 = 8k, size2 = 3k

cs016/2k

homeworks/1k

grades8k

hw13k

hw22k

hw34k

Page 10: Postorder traversal

size1 = 8k, size2 = 5k

cs016/2k

homeworks/1k

grades8k

hw13k

hw22k

hw34k

Page 11: Postorder traversal

size1 = 8k, size2 = 9k

cs016/2k

homeworks/1k

grades8k

hw13k

hw22k

hw34k

Page 12: Postorder traversal

size1 = 18k, size2 = 10k

cs016/2k

homeworks/1k

grades8k

hw13k

hw22k

hw34k

10k

Page 13: Postorder traversal

size1 = 20k, size2 = 10k

cs016/2k

homeworks/1k

grades8k

hw13k

hw22k

hw34k

20k

10k

Page 14: Postorder traversal

The following is an implementation of diskSpace(): public static int diskSpace( InspectableTree T, Position v ) { int s = 0; PositionIterator children = T.children( v ); while( children.hasNext()) s += disSpace( T, children.nextPosition()); s += size(); if( T.isInternal( v )) System.out.println( name( v ) + ": " + s ); return s; }

Page 15: Postorder traversal

Postorder Traversal of a Binary Tree

Since a binary tree is just a special case of trees, the algorithm for postorder traversal of a binary tree is very similar. Algorithm binaryPostorder(T,v): if v is an internal node call binaryPostorder(T, T.leftChild( v )) call binaryPostorder(T, T.rightChild( v )) perform the “visit” action for node v

Page 16: Postorder traversal

Example: Evaluate expression 3 * ( 7 - 4 ) + 6

+

-

* 6

3

7 4

Page 17: Postorder traversal

Example: Evaluate expression 3 * ( 7 - 4 ) + 6

operator: + operator: * left operand: 3

+

-

* 6

3

7 4

Page 18: Postorder traversal

Example: Evaluate expression 3 * ( 7 - 4 ) + 6

operator: + operator: * left operand: 3 operator: - left operand: 7

+

-

* 6

3

7 4

Page 19: Postorder traversal

Example: Evaluate expression 3 * ( 7 - 4 ) + 6 operator: + operator: * left operand: 3 operator: - left operand: 7 right operand: 4

+

-

* 6

3

7 4

Page 20: Postorder traversal

Example: Evaluate expression 3 * ( 7 - 4 ) + 6

operator: + operator: * left operand: 3 right operand: 3

+

-

* 6

3

7 4

Page 21: Postorder traversal

Example: Evaluate expression 3 * ( 7 - 4 ) + 6

operator: + left operand: 9

+

-

* 6

3

7 4

Page 22: Postorder traversal

Example: Evaluate expression 3 * ( 7 - 4 ) + 6

operator: + left operand: 9 right operand: 6

+

-

* 6

3

7 4

Page 23: Postorder traversal

Example: Evaluate expression 3 * ( 7 - 4 ) + 6

result: 15 +

-

* 6

3

7 4

Page 24: Postorder traversal

Postorder traversal using Stack

Algorithm stack-postorder(T, v)establish stack S;S.push(v)while (S in not empty) do {

u := S.top();if (u is leaf or marked) then {visit u; S.pop();}else mark the top element of S;

let u1, u2, …, un be the children of u;for (j = n; j >= 1; j--) S.push(uj);}

}

Page 25: Postorder traversal

Storing a tree onto disk

a

d f

e h b k

a

d

e

0123456789…

file:

node 0

node 1

node 2

i = 0

i = 1

i = 2

Page 26: Postorder traversal

Storing a tree onto disk

a

d f

e h b k

a

d

e

0123456789…

file:

node 0

node 1

node 2

i = 0

i = 1

i = 2

1

2

h

3

…4

Page 27: Postorder traversal

A B+-tree

5

3 7 8

6 7 9 125 81 3

pinternal = 3,pleaf = 2.

1 5 6 12 9 7 3 8 data file

Page 28: Postorder traversal

Storing a tree onto disk

We search a tree in preorder and use a special stack datastructure to control the traversal in such a way the parentaddress can be recorded.

data flag to indicate left or right child

parent address

Page 29: Postorder traversal

Storing a tree onto disk

Algorithm storing-tree(T, v)establish stack S; i = 0;S.push((v, 0, -1))while (S in not empty) do {

u := S.pop(); store u.Data in the file at address i*3;if (u.Parent-address is not equal to –1)then {if (u.Flag == 0) then j := 0; else j := 1;

store i in the file at address (u.Parent-address)*3 + 1 + j;}let u1, u2 be the left and right child of u, respectively;S.push((u2, 1, i)); S.push((u1, 0, i)); i++; }

-1 indicates that the correspondingnode is the root.

Page 30: Postorder traversal

Lab:

The tree shown in the following figure represents an expression:

(((( 3 + 1 ) * 3 ) / (( 9 - 5 ) + 2 )) - (( 3 * ( 7 - 4 )) + 6 )) Please write a Java program to generate this tree and then compute the expression using thepostorder searching method.

+

-

*

/

+

* 6+

2

-

33 -

3 1 9 5 7 4

Page 31: Postorder traversal

public class {

public static void main(String[] args) {generate a tree;ArithCalculation(…) { … }

…}

public static doubleArithCalculation(LinkedBinaryTree t, BTNode v)

{ … }}

Page 32: Postorder traversal

Generate a tree:

LinkedBinaryTree tree = new LinkedBinaryTree();

Position p0 = tree.addRoot(new Character('-'));

Position p1 = tree.insertLeft(p0, new Character('/'));

Position p2 = tree.insertRight(p0, new Character('+'));

Position p3 = tree.insertLeft(p1, new Character('*'));

Position p4 = tree.insertRight(p1, new Character('+'));

Position p5 = tree.insertLeft(p2, new Character('*'));

Position p6 = tree.insertRight(p2, new Integer(6));

Position p7 = tree.insertLeft(p3, new Character('+'));

Position p8 = tree.insertRight(p3, new Integer(3));

Position p9 = tree.insertLeft(p4, new Character('-'));

Page 33: Postorder traversal

Position p10 = tree.insertRight(p4, new Integer(2));

Position p11 = tree.insertLeft(p5, new Integer(3));

Position p12 = tree.insertRight(p5, new Character('-'));

Position p13 = tree.insertLeft(p7, new Integer(3));

Position p14 = tree.insertRight(p7, new Integer(1));

Position p15 = tree.insertLeft(p9, new Integer(9));

Position p16 = tree.insertRight(p9, new Integer(5));

Position p17 = tree.insertLeft(p12, new Integer(7));

Position p18 = tree.insertRight(p12, new Integer(4));

Page 34: Postorder traversal

Algorithm ArithCalculation(T, v) – return a numberBegin

if v is a leaf node, then return v’s value;else {

a = ArithCalculation(T, v’s leftChild);b = ArithCalculation(T, v’s rightChild);if v == ‘+’, then return a + b;if v == ‘-’, then return a - b;if v == ‘*’, then return a * b;if v == ‘/’, then return a / b;}

End

Page 35: Postorder traversal

public double calculation(Position v) {double v1 = 0; double v2 = 0;if (hasLeft(v)) v1 = calculation(left(v));if (hasRight(v)) v2 = calculation(right(v));if (((BTPosition)v).element() instanceof Integer)

return ((Integer)((BTPosition)v).element()).intValue();else { char i = ((Character)((BTPosition)v).element()).charValue();switch(i) {case '-': return v1 - v2;

case '+': return v1 + v2;case '/': return v1/v2;case '*': return v1*v2;default: { System.out.println("Unrecognized symbol");return 0;}}

} }

Page 36: Postorder traversal

/** Returns whether a node has a left child. */public boolean hasLeft(Position v)

//throws InvalidPositionException {Position vv = checkPosition(v);return (vv.getLeft() != null); }

/** Returns whether a node has a right child. */public boolean hasRight(Position v) {

//throws InvalidPositionException {Position vv = checkPosition(v);return (vv.getRight() != null); }

/** If v is a good binary tree node, cast to BTPosition,else throw exception */protected BTPosition checkPosition(Position v)

throws InvalidPositionException{ if (v == null || !(v instanceof BTPosition))

throw newInvalidPositionException("The position is invalid");return (BTPosition) v; }