390 likes | 645 Views
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.
E N D
Postorder traversal • Ed. 2. and 3.: Chapter 6 – • Ed. 4.: Chapter 7 -
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 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); } }
0 1 2 3 4 5 6 7 8 9 a d f e h b k … Storing a tree onto disk file: a i = 0 node 0 d i = 1 node 1 e i = 2 node 2 …
0 1 2 3 4 5 6 7 8 9 a d f e h b k … Storing a tree onto disk file: a i = 0 node 0 1 4 d i = 1 node 1 2 3 e i = 2 node 2 h … …
3 7 8 A B+-tree pinternal = 3, pleaf = 2. 5 1 3 5 6 7 8 9 12 Records in a 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. parent address data flag to indicate left or right child
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 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 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 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; call 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; }