Bstset - Java Bstset - Java: Printed by Owen L. Astrachan
Bstset - Java Bstset - Java: Printed by Owen L. Astrachan
BSTSet.java
Page 1/3
BSTSet.java
Page 2/3
import java.util.*;
5
/** * Simple binary search tree implementation of a set. Operations are O(log n) * in average case and O(n) in the worst case for unbalanced trees. * @author Owen Astrachan */ public class BSTSet<E extends Comparable<E>> implements ISimpleSet<E> {
80
10 85
15
private class TreeNode { E info; TreeNode left; TreeNode right; TreeNode parent;
90
public boolean remove(E element) { TreeNode root = myRoot; while (root != null) { int comp = root.info.compareTo(element); if (comp == 0) { mySize; remove(root); return true; } else if (comp > 0) { root = root.left; } else { root = root.right; } } return false; } private void remove(TreeNode root) { if (root.left == null && root.right == null) { // removing leaf if (root.parent == null) { // removing root? myRoot = null; // tree now empty } else { if (root.parent.left == root) { root.parent.left = null; } else { root.parent.right = null; } } } else if (root.left == null || root.right == null) { // one child, not two TreeNode child = root.left; // only child is left? if (root.left == null) { // nope, its right child = root.right; } if (root.parent == null) { // new root myRoot = child; } else if (root.parent.left == root) { root.parent.left = child; } else { root.parent.right = child; } child.parent = root.parent; } else { // removing node with two children TreeNode successor = root.right; if (successor.left == null) { root.info = successor.info; root.right = successor.right; if (successor.right != null) { successor.right.parent = root; } } else { // immediate right child of removed node has a left child while (successor.left != null) { successor = successor.left; } root.info = successor.info; successor.parent.left = successor.right; if (successor.right != null) { successor.right.parent = successor.parent; } } } } private TreeNode successor(TreeNode t) { if (t == null) return null; // no successor else if (t.right != null) { t = t.right;
20
25
TreeNode(E element, TreeNode lptr, TreeNode rptr, TreeNode p) { info = element; left = lptr; right = rptr; parent = p; } } private int mySize;
95
100
30
35
40
public int size() { return mySize; } public boolean add(E element) { if (myRoot == null) { myRoot = new TreeNode(element, null, null, null); mySize++; return true; } TreeNode root = myRoot; while (root != null) { int comp = root.info.compareTo(element); if (comp == 0) return false; if (comp > 0) { if (root.left == null) { root.left = new TreeNode(element, null, null, root); mySize++; return true; } else { root = root.left; } } else { if (root.right == null) { root.right = new TreeNode(element, null, null, root); mySize++; return true; } else { root = root.right; } } } // can never reach here return false;
115
45
120
50
125
55
130
60
135
65
140
70
145
BSTSet.java
1/5
BSTSet.java
Page 3/3
ISimpleSet.java
Page 1/1
150
155
while (t.left != null) { t = t.left; } return t; } else { TreeNode parent = t.parent; while (parent != null && parent.right == t) { t = parent; parent = t.parent; } return parent; } } public boolean contains(E element) { TreeNode root = myRoot; while (root != null) { int comp = root.info.compareTo(element); if (comp == 0) return true; else if (comp > 0) { root = root.left; } else { root = root.right; } } return false; } public Iterator<E> iterator() { return new TreeIterator(myRoot); }
10
/** * This generic class illustrates simple imlementations * of sets for the purposes of understanding and reasoning * about tradeoffs. For example, we consider hashing, search trees, * AVL trees and tries as set implementations. * @author Owen Astrachan * @version 2.0, October 2006 */ import java.util.Iterator;
160 15
165
20
public interface ISimpleSet<E> extends Iterable<E> { /** * Returns the number of elements in this set. * @return size of set */ public int size(); /** * Adds an element to the set (no duplicates). Returns true if and only if * the element was added, i.e., not already stored in the set. * @param element to be added to set * @return true if element not previously in set, false otherwise */ public boolean add(E element); /** * Removes an element from the set and returns true if removal successful. * @param element to be removed from set * @return true if element removed, false otherwise */ public boolean remove(E element); /** * Returns true if element in set, false otherwise. * @param element is candidate for query in set * @return true if element in set, false otherwise */ public boolean contains(E element); /** * Returns an iterator, necessary for Iterable interface. * @return an iterator over this sets elements */ public Iterator<E> iterator(); }
170
25
175 30
180
190
public TreeIterator(TreeNode root) { while (root.left != null) { root = root.left; } myCurrent = root; myPrevious = null; } public boolean hasNext() { return myCurrent != null; } public E next() { E data = myCurrent.info; myPrevious = myCurrent; myCurrent = successor(myCurrent); return data; } public void remove() { if (myPrevious == null) { throw new IllegalStateException( "cannot remove, no valid next call"); } BSTSet.this.remove(myPrevious); myPrevious = null; mySize; } } }
40
45
195
200
205
210
215
BSTSet.java, ISimpleSet.java
2/5
TrieSet.java
Page 1/3
TrieSet.java
Page 2/3
import java.util.*;
5
Node oldRoot = t.parent; t.parent = null; TrieIterator iter = new TrieIterator(t); while (iter.hasNext()) { list.add(prefix+iter.next()); } t.parent = oldRoot; } public boolean remove(String s) { Node t = myRoot; for (int k = 0; k < s.length(); k++) { char ch = s.charAt(k); t = t.children.get(ch); if (t == null) return false; // no path below? done } if (t.isWord) { t.isWord = false; mySize; return true; } return false; } public boolean contains(String s) { Node t = myRoot; for (int k = 0; k < s.length(); k++) { char ch = s.charAt(k); t = t.children.get(ch); if (t == null) return false; // no path below? done } return t.isWord; // was this marked as a word? } public Iterator<String> iterator() { return new TrieIterator(myRoot); }
10
public static class Node { char info; boolean isWord; Map<Character,Node> children; Node parent;
85
15
20
Node(char ch, Node p) { info = ch; isWord = false; children = new TreeMap<Character,Node>(); parent = p; } } private Node myRoot; // root of entire trie
90
95
25
private int mySize; public TrieSet() { myRoot = new Node(x, null); mySize = 0; } public int size() { return mySize; }
100
30
105
35
110
115
for (int k = 0; k < s.length(); k++) { char ch = s.charAt(k); Node child = t.children.get(ch); if (child == null) { child = new Node(ch, t); t.children.put(ch,child); } t = child; } if (!t.isWord) { t.isWord = true; // walked down path, mark this as a word mySize++; return true; } return false; // alread in set }
135 120
private class TrieIterator implements Iterator<String> { private Node myCurrent; private Node myPrevious; private IdentityHashMap<Node, Object> mySeen = new IdentityHashMap<Node, Object>(); private Object thing = new Object(); StringBuffer path; public TrieIterator(Node root){ myPrevious = null; path = new StringBuffer(); myCurrent = findWordBelow(root); } private Node findWordBelow(Node root){
50
125
55
130
60
65
public void prefixList(ArrayList<String> list, String prefix){ Node t = myRoot; for (int k = 0; k < prefix.length(); k++) { char ch = prefix.charAt(k); t = t.children.get(ch); if (t == null) { return; } }
140
70
145
if (root == null){ return null; } if (root.isWord && !mySeen.containsKey(root)){ mySeen.put(root,thing); return root; } for(Node n : root.children.values()){ path.append(n.info); Node recur = findWordBelow(n); if (recur != null){ return recur; } path.deleteCharAt(path.length()1); }
TrieSet.java
3/5
TrieSet.java
Page 3/3
ISimpleSet.java
Page 1/1
private Node nextWord(Node root){ Node recur = findWordBelow(root); if (recur != null){ return recur; } while (root != null){ Node last = root; root = root.parent; if (root == null) break; // backed up through global root path.deleteCharAt(path.length()1); for(Node n : root.children.values()) { if (!mySeen.containsKey(n)){ path.append(n.info); n = findWordBelow(n); if (n != null){ return n; } } } } return null; } public boolean hasNext() { return myCurrent != null; } public String next() { String s = new String(path); myPrevious = myCurrent; myCurrent = nextWord(myCurrent); return s; } public void remove() { if (myPrevious != null){ myPrevious.isWord = false; myPrevious = null; mySize; } else { throw new IllegalStateException("bad remove from Trie iterator"); } } } }
155
10
/** * This generic class illustrates simple imlementations * of sets for the purposes of understanding and reasoning * about tradeoffs. For example, we consider hashing, search trees, * AVL trees and tries as set implementations. * @author Owen Astrachan * @version 2.0, October 2006 */ import java.util.Iterator;
160
15
165
20
public interface ISimpleSet<E> extends Iterable<E> { /** * Returns the number of elements in this set. * @return size of set */ public int size(); /** * Adds an element to the set (no duplicates). Returns true if and only if * the element was added, i.e., not already stored in the set. * @param element to be added to set * @return true if element not previously in set, false otherwise */ public boolean add(E element); /** * Removes an element from the set and returns true if removal successful. * @param element to be removed from set * @return true if element removed, false otherwise */ public boolean remove(E element); /** * Returns true if element in set, false otherwise. * @param element is candidate for query in set * @return true if element in set, false otherwise */ public boolean contains(E element); /** * Returns an iterator, necessary for Iterable interface. * @return an iterator over this sets elements */ public Iterator<E> iterator(); }
170
25
175
30
180
35
185
40
190
45
195
TrieSet.java, ISimpleSet.java
4/5
SortedArraySet.java
Page 1/1
public class SortedArraySet<E extends Comparable<E>> extends ArraySet<E> { public boolean add(E element) { int index = Collections.binarySearch(myList,element); if (index < 0){ index = (index+1); myList.add(index,element); return true; } return false; } public boolean remove(E element) { int index = Collections.binarySearch(myList,element); if (index < 0){ return false; } myList.remove(index); return true; } public boolean contains(E element) { return Collections.binarySearch(myList,element) >= 0; }
10
15
20
25
30
SortedArraySet.java
5/5