CS notes 10

CS notes 10 - End while Inorder Algorithm: If root == null...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Binary Trees Monday, March 07, 2011 3:46 PM Expression Trees Evaluating expression trees T is String See BST.java Storing and retrieving a binary tree To file->from file In general (any binary tree, not just expr trees) there is more than one tree for every post/pre/in order expression To get around this, we must use more than one o Pre+In o Pre A B C Root In B A C L R Non-recursive traversal code Preorder Algorithm: If root == null return Push root on stack While stack is not empty do Node <- pop stack Visit node Push node.right, if not null Push node.left, if not null
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: End while Inorder Algorithm: If root == null return Push (root,1) on stack While stack is not empty (node,n)&lt;-pop If n==1 then If node.left==null then Visit node Push (node.right,1), if not null Else Push (node,2) Push (node.left,1) End if Else if n=2 Visit node Push (node.right,1), if not null End if End while Postorder Tail recursion Recursion called at the end of the method to traverse through Not really recursion- just a waste Remove and replace with a loop...
View Full Document

Page1 / 2

CS notes 10 - End while Inorder Algorithm: If root == null...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online