39
November 2, 2006 Trees 1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node<ElementType> toAdd = new Node<ElementType>(newItem); //if the list is empty we just add a new first node if (this.isEmpty()) _first = toAdd; else { Node<ElementType> curr = _first; Node<ElementType> prev = null; while (curr != null) { /* we’re not there yet. Increment (“bop”) the pointers and keep looking */ if (newItem.compareTo(curr.getData()) > 0) { prev = curr; curr = curr.getNext(); } /* We’re at the right place! Insert the new node and update the pointers */ else { toAdd.setNext(curr); /* The only time prev would be null is if we are inserting before the old _first in which case we need to update the _first reference */ if (prev != null) prev.setNext(toAdd); else _first = toAdd; return; } } /* We ran off the end of the list and need to insert the node at the end */ prev.setNext(toAdd); } }

November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

Embed Size (px)

Citation preview

Page 1: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 1 of 39

Insert Code// In class LinkedListpublic void insert(ElementType newItem) { Node<ElementType> toAdd = new

Node<ElementType>(newItem); //if the list is empty we just add a new first node if (this.isEmpty()) _first = toAdd; else {

Node<ElementType> curr = _first; Node<ElementType> prev = null;

while (curr != null) { /* we’re not there yet. Increment (“bop”) the

pointers and keep looking */ if (newItem.compareTo(curr.getData()) > 0) {

prev = curr;curr = curr.getNext();

} /* We’re at the right place! Insert the new node

and update the pointers */ else { toAdd.setNext(curr);

/* The only time prev would be null is if we are inserting before the old _first in which case we need to update the _first reference

*/if (prev != null) prev.setNext(toAdd);else

_first = toAdd; return; } } /* We ran off the end of the list and need to

insert the node at the end */ prev.setNext(toAdd); }

}

Page 2: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

TREES

• Definitions, Terminology, and Properties

• Binary Trees

• Search Trees: Improving Search Speed

• Traversing Binary Search Trees

Page 3: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 3 of 39

Binary Trees

• Each internal node has only 2 children– so, each internal node has degree 2

• Recursive definition of binary tree: binary tree is either an– external node (leaf), or– internal node (root) and two binary trees (left

subtree and right subtree)

Page 4: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 4 of 39

Properties of Binary Trees

• (# leaf nodes) = (# internal nodes) + 1

• (# nodes at level i) 2i

• (# leaf nodes) 2(height)

• (height) log2(# leaf nodes)

• Binary tree is full when each level contains a complete set of nodes– thus, adding anything to the tree would increase its

heightLevel

0

1

2

3

4

Page 5: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 5 of 39

Binary Search Tree

• Binary search tree stores keys in internal nodes such that, for every node, keys in left subtree are smaller, and keys in right subtree are larger

• Example:

• Below is also binary search tree but much less balanced. Gee, it kinda looks like a linked list!

Page 6: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 6 of 39

Binary Search Tree Class

• What do binary search trees know to do?– much the same as linked lists: search, insert, delete, isEmpty

public class BinarySearchTree<ElementType extends Comparable<ElementType> > {

private Node<ElementType> _root;

private BinarySearchTree() {_root = new LeafNode<ElementType>();// more about LeafNodes coming up!

}public ElementType search(ElementType itemToFind){

// . . .}public void insert(ElementType newData) { // . . . }public void delete(ElementType itemToDelete) { // . . . }public boolean isEmpty() { // . . .}

}

• We had smart stacks and queues with dumb internal nodes; now we’re going to have a dumb tree with smart internal nodes that will delegate using recursion– Thus tree will delegate to _root (first internal node)

which will delegate to its apropriate child– different from with stacks, queues, linear linked lists:

we’re going to have specialized Node subclasses

Page 7: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 7 of 39

Binary Search Tree: Generic Node

• Methods common to all binary tree nodes in abstract base class:

abstract public class Node< ElementType extendsComparable<ElementType> > {

abstract public ElementType search(ElementType

itemToFind);abstract public Node<ElementType> insert

(ElementType newData);abstract public Node<ElementType> delete

(ElementType itemToDelete);

abstract public ElementType swapKey (ElementType newKey);

abstract public boolean isEmpty();}

•swapKey– used in delete when trying to delete a node with 2

children– helper method for delete(), not in BinarySearchTree class as tree operation

• make specific nodes which extend generic Node– different pattern from previous data structures– The subclasses will implement these abstract

methods

Page 8: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 8 of 39

• Represents node with two children that holds Comparable<ElementType> data– left child contains either subtree, all of whose data is less than

parent’s data, or a leaf node– right child contains either subtree, all of whose data is greater

than parent’s data, or a leaf node

public class InternalNode< ElementType extends Comparable<ElementType> > extends Node<ElementType> {

private ElementType _data;private Node<ElementType> _left;private Node<ElementType> _right;public InternalNode(ElementType newData,

Node<ElementType> newLeftNode, Node<ElementType>

newRightNode) {_data = newData;_left = newLeftNode;_right = newRightNode;

}// will redefine superclass methods soon . . .

}

Binary Search Tree: Internal Node

Node<ElementType> Node<ElementType>

InternalNode<ElementType> _data _left _right

ElementType _key

Page 9: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 9 of 39

Binary Search Tree: Leaf Node

• Represents leaf within tree; similar to last node in a linked list

– tree typically has many leaf nodes, whereas linked list only had one

public class LeafNode< ElementType extends Comparable<ElementType>

>

extends Node<ElementType> { // no instance variables // simply use default constructor // will redefine superclass methods soon . . .

}

• Note: we will ignore the data associated with each key, and just focus on the keys in this lecture (for ease and clarity)

LeafNode<ElementType>

Page 10: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 10 of 39

Searching a Binary Tree

if searchkey == nodekeyfound it!

else if searchkey < nodekeysearch left

else if searchkey > nodekeysearch right

• Example: search for key ‘I’

• Search path - start in middle M and choose shortest path to data I– Similar to 20 questions in sorted vector– log2N time -- Woo Hoo!

Page 11: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 11 of 39

• What if we want to know if 224 is in Tree?

• Tree says “Hey Root! Ya got 224?”

• 123 says: “Let’s see. I’m not 224. But if 224 is in tree, it would be to my right. I’ll ask my right child and return its answer.”

• 252 says: “I’m not 224. I better ask my left child and return its answer.”

• 224 says: “224? That’s me! Yo, caller (252) here’s your answer.” (returning non-null indicates that query is in tree)

• 252 says: “Yo, caller (123)! Here’s your answer.”

• 123 says: “Here’s your answer.”

Searching Simulation

Page 12: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 12 of 39

Searching Code

• BinarySearchTree is dumb so it delegates to root

// in BinarySearchTree:public ElementType search(ElementType

itemToFind) {return _root.search(itemToFind);

}

• LeafNode returns null (indicates nothing found):

// in LeafNode:public ElementType search(ElementType

itemToFind) {return null;

}

Page 13: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 13 of 39

Searching Code: Internal Node

• InternalNode checks its data– if same, return the data– else, delegate!

// in InternalNode:

public ElementType search(ElementType itemToFind) {

ElementType result = null;

if (_data.compareTo(itemToFind) == 0)

result = _data;

else if(_data.compareTo(itemToFind) > 0)

result = _left.search(itemToFind);

else if(_data.compareTo(itemToFind) < 0)

result = _right.search(itemToFind);

return result;

}

• Smart node approach makes our code clean, simple and elegant

– non-recursive method is much messier, involving explicit bookkeeping of the tree using a current and a next pointer

• We used the non-recursive method for LinkedLists but trees can get a lot more complicated

Page 14: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 14 of 39

Insertion into a Binary Tree

• Method:– find leaf where key should appear in tree; it will

place itself in the tree as follows:– leaf makes new InternalNode with key as _data, itself(this) as _left child, and new LeafNode as _right child

– in tree, replace this leaf with new internal node by having leaf return to its parent a reference to new internal node

• Example: Insert key Z

• log2N time, Yay!– just a search with some reference manipulations

at end

new place for node

Page 15: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 15 of 39

Insertion (cont.)

• Another example: Insert N

Page 16: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 16 of 39

Insertion Code

• Let’s see the insertion code in the Tree, for InternalNodes and for LeafNodes

// in BinarySearchTree:public void insert(ElementType newData) { _root = _root.insert(newData); // delegate}

// in InternalNode:public Node<ElementType> insert(ElementType

newData) { if (_data.compareTo(newData) > 0)

/* _left is set to whatever its child returns (either original child itself or new node)*/

_left = _left.insert(newData); else

_right = _right.insert(newData); return this;}

// in LeafNode:public Node<ElementType> insert(ElementType

newData) { //see S7 for constructor for InternalNode return new InternalNode<ElementType>(newData,

this,new LeafNode<ElementType>());

}

The old child node will return to the parent what the parent’s new child should be.

Page 17: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 17 of 39

Insertion Simulation

• Insert: 224 _root = _root.insert(newData);

• 123 says: “I’m an internal node. Hmm ... I am less than 224. I’ll let my right child deal with it.”

// . . .

if (_data.compareTo(newData) > 0)_left = _left.insert(newData);

else _right = _right.insert(newData);

return this; // root’s right child is still 123

• 252 says: “I’m an internal node. I am greater than 224. I’ll pass it on to my left child.”

if (_data.compareTo(newData) > 0)_left = _left.insert(newData);

// . . . return this; // 123’s right child is still 252

Page 18: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 18 of 39

Insertion Simulation (cont.)

• Little left leaf says: “You belong here, 224. Let me make an internal node for you.

» I will be your left child and I’ll make a new leaf node to be your right child. I’ll return a link to you to my parent so you can take my place.”

return new InternalNode<ElementType>(newData,

this, new LeafNode<ElementType>());

• 252 assigns returned link to new 224 node as its left child (not caring whether child has changed) and passes link to itself to 123, which doesn’t care either and assigns it to its right child

• Finally, 123 returns itself to root, and everyone is happy

224

“Oh, where am I to go?”

Page 19: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 19 of 39

Notes on Trees

• Different arrival order of nodes results in different trees– that is, if you insert a node referencing data value of 18

into empty tree, that node will become root– if you then insert a node referencing data value of 12, it

will become left child of node that was previously inserted (which referenced 18)

– however, if you insert node referencing 12 into an empty tree, it will become root node

– then, if you insert one referencing 18, that node will become right child of root

– so, even with same nodes, differing order of arrival into tree will result in two completely different trees!

• When searching for a value, reaching another value that is greater than the one being searched for does not mean that the value being searched for is not present in tree (whereas it does in linear linked lists)– it may well still be contained in left subtree of node

of greater value that has just been encountered– thus, where you might have given up in linked lists, you

can’t give up here until you reach a leaf

Page 20: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 20 of 39

Deletion: Cases for Deletion (1/8)

• Trivial case: node to delete has two leaf children– just replace internal node and its leaf children with

single leaf node– does not matter which child chosen to replace with

• Delete P– P has two leaf children– P returns left child and is therefore replaced by it

Page 21: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 21 of 39

Deletion: More cases for Deletion (2/8)

• Harder case: node to delete has one leaf child and one subtree child– just replace internal node and its leaf child with

subtree child– must choose correct child to replace with

• Delete O– O has one leaf child– Choose the correct child with which to replace O,

the subtree– P replaces O with the subtree returned by O

Page 22: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 22 of 39

Deletion: Last Case for Deletion (3/8)

• Really hard case: node to delete has two subtree children– this is tricky, because we do not know which

child should replace its parent– rather than deal with merging trees, we will

swap the data with a node that doesn’t have two children

• To help, use an auxiliary method swapData– swaps the data in the current node with the

data in the right-most node in the left subtree– this node has key value less than all of the

nodes in the left subtree, and greater key value than the nodes in the right subtree

– since it is a right-most node, it has at most one child

Page 23: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 23 of 39

Deletion: Really Complicated Case (4/8)

• Delete R– R has two internal children– swap R with the right-most node in the left subtree,

Q– delete R (in its new position) using the one-child

case

Page 24: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 24 of 39

Deletion: Pseudocode for delete (5/8)

• For InternalNode:

if nodekey == deletekeyif one subtree is empty return the other treeelse swap with the right-most node in the left subtree tell left to try to delete

else if nodekey > deletekeytell left to try to delete

else nodekey < deletekeytell right to try to delete

return this

• For LeafNode:– return reference to this leaf

Page 25: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 25 of 39

Deletion: Code for Deletion (6/8)

• Starts as usual:

// in BinarySearchTree:public void delete(ElementType itemToDelete) {_root = _root.delete(itemToDelete);

}

• Leaf node handles trivial case:

// in LeafNode:public Node<ElementType> delete(ElementType

itemToDelete) {return this;

}

Page 26: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 26 of 39

Deletion: Internal Node Code for Deletion (7/8)

• Internal node handles bulk of work– simplified by helper method: swapKey– also simplified because does not need to worry

about children: LeafNode handles special cases

// in InternalNode:public Node<ElementType> delete(ElementType

itemToDelete) {

// defaultNode<ElementType> nodeToReturn = this;

if (_data.compareTo(itemToDelete) == 0)// code to perform deletion on next slide

// otherwise, let my children handle itelse if (_data.compareTo(itemToDelete) > 0)_left = _left.delete(itemToDelete);

else if (_data.compareTo(itemToDelete) < 0)_right = _right.delete(itemToDelete);

return nodeToReturn;}

Page 27: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 27 of 39

Deletion: More Code for delete (8/8)

// in InternalNode’s delete method:// if the left node is a leaf, delete itif (_left.isEmpty())nodeToReturn = _right;

else if (_right.isEmpty())nodeToReturn = _left;

// Difficult case: no leaf childrenelse { // get the data from the node we swapped _data = _left.swapKey(_data);

// delete the swapped node // have to call delete again to ensure // pointers get set properly _left = _left.delete(itemToDelete);

}

Page 28: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 28 of 39

Code for swapKey

• swapKey returns the right most node of the left subtree:

public ElementType swapKey(ElementType

newKey){

// If we have a right child, ask it

// to swap

if(!_right.isEmpty()) {

return _right.swapKey(newKey);

}

// Else, this is the node to swap

else {

ElementType oldData = _data;

_data = newKey;

return oldData;

}

}

Page 29: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 29 of 39

Implementing isEmpty

• BinarySearchTree acts as usual and delegates message to _root

• InternalNode will return false, LeafNode will return true– true means asked a LeafNode, so there are no InternalNodes in the tree (it is empty)

– false means asked an InternalNode, so there is at least one InternalNode in the tree (it is not empty)

Page 30: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 30 of 39

Traversing a Binary Tree

• We often want to access every node in tree– so far, we can only search for a single item– we can use a traversal algorithm to perform some

arbitrary operation on every node in tree

• Many ways to traverse nodes in tree– order children are visited is important– three traversal types: inorder, preorder, postorder

• Exploit recursion!– subtree has same structure as tree

Page 31: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 31 of 39

Inorder Traversal of Binary Search Tree

• Inorder traversal– traverse left subtree– output root– traverse right subtree

• In this ordered tree, prints keys alphabetically.

public void print() { _left.print(); // method to write a letter; not // included in slides:

this.writeCharacter(); _right.print();}

Page 32: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 32 of 39

Preorder Traversal

• Preorder traversal– output root– traverse left subtree– traverse right subtree

public void print() { this.writeCharacter(); _left.print(); _right.print();}

Page 33: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 33 of 39

Postorder Traversal

• Postorder traversal– traverse left subtree– traverse right subtree– output root

public void print() { _left.print(); _right.print(); this.writeCharacter();}

• To learn more about the exciting world of trees, take CS16: Introduction to Algorithms and Data Structures

Page 34: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 34 of 39

Prefix, Infix, Postfix Notation for Arithmetic Expressions

• Infix, Prefix and Postfix refer to where the operator goes relative to its operands– Infix: (fully parenthesized)

((1 * 2) + (3 * 4)) - ((5 - 6) + (7 / 8))– Prefix:

- + * 1 2 * 3 4 + - 5 6 / 7 8– Postfix:

1 2 * 3 4 * + 5 6 - 7 8 / + -

• Graphical representation for equation:

November 4, 2004

Page 35: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 35 of 39

Using Prefix, Infix and Postfix Notation

• When you type an equation into a spreadsheet, you use Infix; When you type an equation into many Hewlett-Packard calculators, you use Postfix, also known as “Reverse Polish Notation,” or “RPN.”

• Easier to evaluate Postfix because it has no parentheses and evaluates in a single left-to-right pass.

• Use Dijkstra’s Algorithm to convert from user-entered Infix to easy-to-handle Postfix.

Page 36: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 36 of 39

Dijkstra’s Algorithm• 2 stack algorithm for single-pass Infix to

Postfix conversion, using operator precedence

(a + (b * (c ^ d))) a b c d ^ * +

• Use rule matrix to implement strategyA) operands are pushed onto operand stack;

operators are pushed in precedence order onto the operator stack

B) when precedence order would be disturbed, pop operator stack until order is restored, evaluating each pair of operands popped from the operand stack and pushing the result back onto the operand stack. Note that equal precedence displaces. At the end of the statement all operators are popped.

C) “(“ starts a new substack; “)” pops until its matching “(“

Operand Stack Arithmetic Queue

Operator Stack

Precedence Checker

8 - 4 * 16

3

2

64-56

Page 37: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 37 of 39

Dijkstra’s Algorithm (cont.)

• A: push operator or “(“ on operator stack

• B: operator pop loop: pop operator and 2 operands, evaluate and return result to operand stack, until precedence is again monotonic

• C: “)” pop loop: pop operator and 2 operands, evaluate and return result to operand stack, until “)” cancels its matching “(“

• E: error

Operand Stack Arithmetic Queue

Operator Stack

Precedence CheckerPriority

( 5, 1

^ 4

*/ 3

+- 2

) 0

e empty

A A A A C

A B B B C

A A B B C

A A A B C

A A A A E

Incoming Operator

(

^

*/

+-

e

Top of Stack ( ^ */ +- )

Page 38: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 38 of 39

Executing Dijkstra’s Algorithm

• Infix:

1 * 2 + 3 * 4 - 5 - 6 + 7 / 8

• Postfix:

1 2 * 3 4 * + 5 6 - 7 8 / + -

• Note: needs a little additional work for unary —

Operand Stack Arithmetic Queue

Operator Stack

Precedence Checker

Page 39: November 2, 2006Trees1 of 39 Insert Code // In class LinkedList public void insert(ElementType newItem) { Node toAdd = new Node (newItem); //if the list

November 2, 2006Trees 39 of 39

Announcements

• Textbook Reading Assignment– Chapter 16

• Swarm Late Date TONIGHT at 11:59PM

• Tetris helpsession TONIGHT 8pm – CIT 165 (Motorola)– Design Questions due Sunday 5PM

• WiCS Bagel Lunch Tomorrow with Professor Claire Kenyon– noon on the 4th Floor of the CIT

• WiCS Microsoft Dinner Monday– 7pm CIT 506 RSVP to [email protected]