{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

finalReference - 15-121 Final Exam Page 1 of 4 15-121...

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

View Full Document Right Arrow Icon
15-121 Final Exam Page 1 of 4 15-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(); } } }
Background image of page 1

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

View Full Document Right Arrow Icon
15-121 Final Exam Page 2 of 4 Listing 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. */
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}