CMSC 132A Lecture Notes - Lecture 36: Binary Tree, Iterator

117 views3 pages
CMSC132A Lecture 36: Binary Trees
How would we implement a binary tree? Well, here’s the node class.
Class TreeIterator<X> implements Iterator<X> {
TreeIterator(Tree <X> t) {
}
Public boolean hasNext() {
Return this.tree.accept(new TreeVisitor<X, Boolean() {
Public Boolean VisitLeaf(Leaf<X> leaf) {
Return rights.accept(new Listof(Tree<X>, Boolean();
}
Public Boolean visitCons(Cons<X>){
Return true;
}
Public X visitNode(Node<X> node) {
Return true;
}
));
}
Public X next() {
TreeVisitor<X, X> tv=new TreeVisitor<X, X>() {
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents