Browse Source

Finished add and min

master
Raphael Roberts 6 years ago
parent
commit
5cf9644dcc
  1. 35
      BST.java

35
BST.java

@ -1,7 +1,6 @@
// Binary Search Tree class
// Xiwei Wang
import java.util.*;
public class BST {
@ -31,7 +30,31 @@ public class BST {
// This non-recursive method takes a string and inserts it into the binary
// search tree, keeping the tree ordered.
public void add(String value) {
// TODO: implement this method using a non-recursive solution
BSTNode to_add = new BSTNode(value);
if (m_root == null)
m_root = to_add;
else {
BSTNode current = m_root;
BSTNode parent = null;
int comparison = 0;
while (current != null) {
comparison = value.compareTo(current.getInfo());
if (comparison > 0) {
parent = current;
current = current.getRight();
}
else if (comparison < 0) {
parent = current;
current = current.getRight();
}
else
return;
}
if (comparison > 0)
parent.setRight(to_add);
else
parent.setLeft(to_add);
}
}
// This non-recursive method returns a string that represents the in-order traversal
@ -46,16 +69,16 @@ public class BST {
// are not allowed to create any additional structures, including but not
// limited to arrays, stacks, queues, or other trees.
public String min() {
// TODO: implement this method using a non-recursive solution
return ""; // replace this statement with your own return
BSTNode current = m_root;
while (current.getLeft() != null)
current = current.getLeft();
return current.getInfo();
}
// This method takes a reference to the root of the expression, evaluates
// the tree, and returns the result as an int.
public int evaluate(BSTNode node) {
// TODO: implement this method using a non-recursive solution
return -1; // replace this statement with your own return
}
}
Loading…
Cancel
Save