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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wrote SequentialSearchST class and comments
jromeall committed Mar 16, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit db2267510b572e697c59811a697813d014a92063
147 changes: 90 additions & 57 deletions src/edu/greenriver/sdev333/BST.java
Original file line number Diff line number Diff line change
@@ -28,98 +28,131 @@ public Node(KeyType key, ValueType val, int N) {
}
@Override
public void put(KeyType key, ValueType value) {

}

@Override
/*public ValueType get(KeyType key) {
//given a key, find the value
//set root to be current, so we have a starting point
Node current = root;
//keep going down the tree as long as not null
while (current != null) {
///to choose whether to go left or right use the compareTo method returns neg, zero, or pos #
//keys are not primitive they are class objects so use the .compareTo
int comp = key.compareTo(current.key);
//so if the comparison is less than we go left and so on
if (comp < 0) {
current = current.left;
}
else if (comp > 0) {
current = current.right;
}
else{
return current.val;
}
}//end of while loop
//if not there
return null;
}
*/

public ValueType get(KeyType key) {
return get(root, key);
root = put(root, key, value);
}

//recursive way
//helper method
private ValueType get(Node current, KeyType key) {
private Node put(Node current, KeyType key, ValueType value) {
if (current == null) {
return null;
return new Node(key, value, 1);
}

int comp = key.compareTo(current.key);

if (comp < 0) {
return get(current.left, key);
current.left = put(current.left, key, value);
}
else if (comp > 0) {
return get(current.right, key);
current.right = put(current.right, key, value);
}
else {
return current.val;
current.val = value;
}
current.N = 1 + size(current.left) + size(current.right);
return current;
}

@Override
public int size() {
public KeyType min() {
if (root == null) {
return 0;
return null;
}
else {
return root.N;
Node current = root;
while (current.left != null) {
current = current.left;
}
}

//helper method for being flexible looking at subtree size given the current spot


@Override
public KeyType min() {
return null;
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 0;
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);
}
}


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