27 - CMPSCI 187: Programming With Data Structures Lecture...

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

View Full Document Right Arrow Icon
CMPSCI 187: Programming With Data Structures Lecture #27: Expression Trees 18 November 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
Expression Trees • L&C’s BinaryTreeADT Interface • Some Uses of Binary Trees • Review of LinkedBinaryTree and ArrayBinaryTree Classes • Expression Trees and the ExpressionTree Class • Evaluating an Expression Tree • The PostFx Evaluator Revisited
Background image of page 2
L&C’s BinaryTreeADT Interface • Just as with lists, we have an ADT defning operations that are common to all binary trees. For each type o± binary tree, we can then extend this inter±ace to add the appropriate new operations. • Note that this inter±ace has no operations to change the tree, because each extension will have its own methods to add or remove nodes. public interface BinaryTreeADT<T> { public T getRoot ( ); public boolean isEmpty ( ); public int size ( ); public boolean contains (T target); public T find (T target); public String toString ( ); public Iterator<T> interatorInOrder ( ); public Iterator<T> interatorPreOrder ( ); public Iterator<T> interatorPostOrder ( ); public Iterator<T> interatorLevelOrder ( );}
Background image of page 3

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

View Full DocumentRight Arrow Icon
Some Uses Of Binary Trees • A binary tree can be used to implement an ordered list more efFciently than an array. Next week we will see binary search trees , binary trees where every node contains an element from some comparable class. If x is the element at a node n, we make sure that all the nodes in the subtree under n’s left child are less than n, and all the nodes in the right subtree are greater than n. We can then insert or delete an element by going down the tree to Fnd the right place for the action, and the edit to the tree does not require shifting. • If the tree is balanced, this will be O(log n) time instead of O(n) time for the
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

27 - CMPSCI 187: Programming With Data Structures Lecture...

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

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