finalReference

finalReference - 15-121 Final Exam Page 1 of 415-121:...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: 15-121 Final Exam Page 1 of 415-121: Introduction to Data Structures Final Exam - Fall 2009 APPENDIX Listing 1: The BST Class. import java.util.*; public class BST { private Node root; public BST() { root = null; } public void insert(Comparable data){ root = insert(root, data); } private Node insert(Node p, Comparable toInsert){ if (p == null) return new Node(toInsert); if (toInsert.compareTo(p.data) == 0) return p; if (toInsert.compareTo(p.data) < 0) p.left = insert(p.left, toInsert); else p.right = insert(p.right, toInsert); return p; } public boolean search(Comparable toSearch){ return search(root, toSearch); } private boolean search(Node p, Comparable toSearch){ if (p == null) return false; else if (toSearch.compareTo(p.data) == 0) return true; else if (toSearch.compareTo(p.data) < 0) return search(p.left, toSearch); else return search(p.right, toSearch); } private static class Node /* The inner Node class */ { private Comparable data; private Node left, right; public Node(Comparable data){ left = null; right = null; this.data = data; } public Node(Comparable data, Node l, Node r){ left = l; right = r; this.data = data; } public String toString() { return data.toString(); } } } 15-121 Final Exam Page 2 of 4Listing 2: The Set Interface (implemented by HashSet). public interface Set<AnyType> { /* If obj is not present in the set, adds obj and returns true. Otherwise, returns false. */ public boolean add(AnyType obj); /* Adds all of the elements in the specified collection to this set if they're not already present. */ this set if they're not already present....
View Full Document

Page1 / 4

finalReference - 15-121 Final Exam Page 1 of 415-121:...

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

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