Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrote SequentialSearchST class and comments #2

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 25 additions & 6 deletions src/BasicTestClient.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import edu.greenriver.sdev333.BST;
import edu.greenriver.sdev333.OrderedSymbolTable;
import edu.greenriver.sdev333.SymbolTable;
import java.util.Scanner;

Expand All @@ -14,9 +16,8 @@ public static void main(String[] args) {

Scanner input = new Scanner(inputString);

// You can replace the implementation with any class that implements
// SymbolTable interface
SymbolTable<String, Integer> st = new TreeMapWrapper<>();
// OrderedSymbolTable interface
OrderedSymbolTable<String, Integer> st = new BST<>();

int i = 0;
while (input.hasNext()) {
Expand All @@ -25,8 +26,26 @@ public static void main(String[] args) {
i++;
}

for (String s : st.keys()) {
System.out.println(s + " " + st.get(s));
}
// Test min
System.out.println("Min key: " + st.min());

// Test max
System.out.println("Max key: " + st.max());

// Test floor
String floorKey = "F";
System.out.println("Floor of " + floorKey + ": " + st.floor(floorKey));

// Test ceiling
String ceilingKey = "C";
System.out.println("Ceiling of " + ceilingKey + ": " + st.ceiling(ceilingKey));

// Test select
int k = 3;
System.out.println("Key with rank " + k + ": " + st.select(k));

// Test rank
String rankKey = "E";
System.out.println("Rank of " + rankKey + ": " + st.rank(rankKey));
}
}
8 changes: 6 additions & 2 deletions src/FrequencyCounter.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import edu.greenriver.sdev333.BST;
import edu.greenriver.sdev333.BinarySearchST;
import edu.greenriver.sdev333.SequentialSearchST;
import edu.greenriver.sdev333.SymbolTable;

import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;


/**
* Frequency Table is a symbol-table client
* This is a rewriting of what is on p. 372 in Sedgewick and Wayne, Algorithms, 4th edition
Expand All @@ -13,11 +17,11 @@
*/
public class FrequencyCounter {
public static final int MINLEN = 1;
public static final String FILENAME = "tale.txt";
public static final String FILENAME = "tinyTale.txt";
public static void main(String[] args) {
System.out.println("Hello world!");

SymbolTable<String, Integer> st = new TreeMapWrapper<>();
SymbolTable<String, Integer> st = new SequentialSearchST<>();

try {
Scanner input = new Scanner(new File(FILENAME));
Expand Down
130 changes: 125 additions & 5 deletions src/edu/greenriver/sdev333/BST.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@
* @param <ValueType>
*/
public class BST<KeyType extends Comparable<KeyType>, ValueType> implements OrderedSymbolTable<KeyType, ValueType> {

//field
private Node root;

//helper class
private class Node {
private KeyType key;
private ValueType val;
private Node left;
private Node right;
private int N; //number of nodes in the subtree rooted here

public Node(KeyType key, ValueType val, int N) {
this.key = key;
this.val = val;
this.N = N;

}
}
@Override
public void put(KeyType key, ValueType value) {

root = put(root, key, value);
}

@Override
Expand All @@ -22,31 +41,132 @@ public int size() {
return 0;
}

private Node put(Node current, KeyType key, ValueType value) {
if (current == null) {
return new Node(key, value, 1);
}
int comp = key.compareTo(current.key);
if (comp < 0) {
current.left = put(current.left, key, value);
}
else if (comp > 0) {
current.right = put(current.right, key, value);
}
else {
current.val = value;
}
current.N = 1 + size(current.left) + size(current.right);
return current;
}

@Override
public KeyType min() {
return null;
if (root == null) {
return null;
}
Node current = root;
while (current.left != null) {
current = current.left;
}
return current.key;
}

@Override
public KeyType max() {
return null;
if (root == null) {
return null;
}
Node current = root;
while (current.right != null) {
current = current.right;
}
return current.key;
}

@Override
public KeyType floor(KeyType key) {
return null;
Node x = floor(root, key);
if (x == null) {
return null;
}
return x.key;
}

private Node floor(Node x, KeyType key) {
if (x == null) {
return null;
}
int comp = key.compareTo(x.key);
if (comp == 0) {
return x;
}
if (comp < 0) {
return floor(x.left, key);
}
Node t = floor(x.right, key);
if (t != null) {
return t;
}
else {
return x;
}
}

@Override
public KeyType ceiling(KeyType key) {
return null;
Node x = ceiling(root, key);
if (x == null) {
return null;
}
return x.key;
}

private Node ceiling(Node x, KeyType key) {
if (x == null) {
return null;
}
int comp = key.compareTo(x.key);
if (comp == 0) {
return x;
}
if (comp > 0) {
return ceiling(x.right, key);
}
Node t = ceiling(x.left, key);
if (t != null) {
return t;
}
else {
return x;
}
}

@Override
public int rank(KeyType key) {
return rank(key, root);
}

private int rank(KeyType key, Node x) {
if (x == null) {
return 0;
}
int comp = key.compareTo(x.key);
if (comp < 0) {
return rank(key, x.left);
}
else if (comp > 0) {
return 1 + size(x.left) + rank(key, x.right);
}
else {
return size(x.left);
}
}

private int size(Node left) {
return 0;
}


@Override
public KeyType select(int k) {
return null;
Expand Down
Loading