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

## Presentation on theme: "Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -"— Presentation transcript:

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 v w postorder(T,v) postorder(T,w)

Postorder Traversal of a Binary Tree

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 u 1, u 2, …, u n be the children of u; for (j = n; j >= 1; j--) S.push(u j ); }

Storing a tree onto disk a df ehbk … a d e 01234567890123456789 … file: node 0 node 1 node 2 i = 0 i = 1 i = 2

Storing a tree onto disk a df ehbk … a d e 01234567890123456789 … file: node 0 node 1 node 2 i = 0 i = 1 i = 2 1 2 h 3 … 4

A B + -tree 5 3 7 8 679125813 p internal = 3, p leaf = 2. 156129738 data file

Storing a tree onto disk We search a tree in preorder and use a special stack data structure to control the traversal in such a way the parent address can be recorded. data flag to indicate left or right child parent address

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 u 1, u 2 be the left and right child of u, respectively; S.push((u 2, 1, i)); S.push((u 1, 0, i)); i++; } -1 indicates that the corresponding node is the root.

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 the postorder searching method.

public class { public static void main(String[] args) { generate a tree; ArithCalculation(…) { … } … } public static double ArithCalculation(LinkedBinaryTree t, BTNode v) { … } }

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('-'));

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));

Algorithm ArithCalculation(T, v) – return a number Begin 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

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; }

/** 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 new InvalidPositionException("The position is invalid"); return (BTPosition) v; }

Download ppt "Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -"

Similar presentations