BST Data Structure -- Class project -- accessing nested classes - java

The problem: apply a binary search tree to a "person" object isntead of a int value. The person object is composed of a name and a weight. The value of the person object that the tree will organize and search on is the "name" (string) of a person.
Here is a (leaf) tree Node:
public class Node {
private Node leftChild;
private Node rightChild;
private Node parent;
private Person person;
private int height;
public Node(Node p, Node l, Node r, Person paul, int h) {
leftChild = l;
rightChild = r;
person = paul;
height = h;
parent = p;
}
public Node(Person paul, Node p) {
this(p, null, null, paul, 0);
}
public Node (Person paul) {
this(null, null, null, paul, 0);
}
public boolean isBalanced() {
if(leftChild == null && rightChild == null)
return true;
else if (leftChild == null)
return rightChild.getHeight() == 0;
else if (rightChild == null)
return leftChild.getHeight() == 0;
return Math.abs(leftChild.getHeight()- rightChild.getHeight()) < 2;
}
public void setParent(Person parent) {
parent = new Person( parent.getName(), parent.getWeight());
}
public Node getParent() {
return parent;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getLeftChild() {
return leftChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public Node getRightChild() {
return rightChild;
}
public void setPerson(Person person) {
Person = new Person(person.getName(), person.getWeight());
}
public Person getPerson() {
return new Person(person.getName(), person.getWeight());
}
public void setHeight() {
if(leftChild == null && rightChild == null)
height = 0;
else if (leftChild == null)
height = rightChild.getHeight() + 1;
else if (rightChild == null)
height = leftChild.getHeight() + 1;
else
height= leftChild.getHeight() >= rightChild.getHeight()?
leftChild.getHeight() + 1: rightChild.getHeight() +1;
}
public int getHeight() {
setHeight();
return height;
}
}
Here is the BinarySearchTree:
public class BinarySearchTree {
private Node root;
public BinarySearchTree(Node root) {
this.root = root;
}
public BinarySearchTree() {
this.root = (Node)null;
}
void setRoot(Node r) {
root = r;
}
Node getRoot() {
return root;
}
public Node findParent(Person person, Node node) {
if (node.getPerson() == person)//Error in code here
return (Node)null; // root itself
else if(node.getLeftChild() != null && node.getLeftChild().getPerson() == person)
return node;
else if(node.getRightChild() != null && node.getRightChild().getPerson() == person)
return node;
else if (node.getPerson().getName().compareTo(" a ") > 0 ){
return findParent(person, node.getLeftChild());}
else
return findParent(person, node.getRightChild());
}
public Node insertNode(Person person) {
return insertNode(person, null, null);
}
public Node insertNode(Person person, Node node, Node parent) {
if(node == (Node)null)
node = new Node(parent, null, null, person, 0);
else if (person.getName().compareTo(node.getPerson().getName()) < 0)
node.setLeftChild(insertNode(person,node.getLeftChild(), node));
else if (person.getName().compareTo(node.getPerson().getName()) > 0)
node.setRightChild(insertNode(person, node.getRightChild(), node));
node.setHeight();
return node;
}
/* in-order traversal for showing inside of tree */
public void traverseInOrder(Node node) {
if(node.getLeftChild() != (Node)null)
traverseInOrder(node.getLeftChild());
System.out.print("Value: " + '\n' + node.getPerson().toString() + ", Height: " +
node.getHeight() + ", Parent: ");
Node n = findParent(node.getPerson(), getRoot());
if(n == (Node)null)
System.out.println("root");
else
System.out.println(n.getPerson().toString() + "");
if(node.getRightChild()!= (Node)null)
traverseInOrder(node.getRightChild());
}
}
public Node(Person paul, Node p) {
this(p, null, null, paul, 0);
}
public Node (Person paul) {
this(null, null, null, paul, 0);
}
public boolean isBalanced() {
if(leftChild == null && rightChild == null)
return true;
else if (leftChild == null)
return rightChild.getHeight() == 0;
else if (rightChild == null)
return leftChild.getHeight() == 0;
return Math.abs(leftChild.getHeight()- rightChild.getHeight()) < 2;
}
public void setParent(Person parent) {
parent = new Person( parent.getName(), parent.getWeight());
//or is the parent supposed to be a null pointer ????
}
public Node getParent() {
return parent;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getLeftChild() {
return leftChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public Node getRightChild() {
return rightChild;
}
public void setPerson(Person Person) {
Person = new Person(person.getName(), person.getWeight());
}
public Person getPerson() {
return new Person(person.getName(), person.getWeight());
}
public void setHeight() {
if(leftChild == null && rightChild == null)
height = 0;
else if (leftChild == null)
height = rightChild.getHeight() + 1;
else if (rightChild == null)
height = leftChild.getHeight() + 1;
else
height= leftChild.getHeight() >= rightChild.getHeight()?
leftChild.getHeight() + 1: rightChild.getHeight() +1;
}
public int getHeight() {
setHeight();
return height;
}
}

The problem is that
if (node.getPerson() == person)
tests if these objects have the same address in memory, not if they have the same "logical" value. You should replace with:
if (node.getPerson().getName().equals(person.getName()))

Use String.compareTo() with person names' as the parameters.

Related

Insert method in Binary Search Tree

when I insert nodes into the binary tree and go test it out in the main by printing it in order the output is always wrong I have tried other solutions but it just keeps getting into bigger problems and in the output, it will be displayed in the wrong order I check the display its perfect but the problem is with the insert method (insert method insert non repeated keys).
class BSTNode<T> {
public int key;
public T data;
public BSTNode<T> left, right;
public BSTNode(int k, T val) {
key = k;
data = val;
left = right = null;
}
public BSTNode(int k, T val, BSTNode<T> l, BSTNode<T> r) {
key = k;
data = val;
left = l;
right = r;
}
}
public class BST<T> {
BSTNode<T> root, current;
public BST() {
root = current = null;
}
public boolean empty() {
return root == null;
}
public boolean full() {
return false;
}
public T retrieve() {
return current.data;
}
public boolean findkey(int tkey) {
BSTNode<T> p = root, q = root;
if (empty())
return false;
int nb = 0;
while (p != null) {
q = p;
nb++;
if (p.key == tkey) {
current = p;
return true;
} else if (tkey < p.key)
p = p.left;
else
p = p.right;
}
current = q;
return false;
}
public boolean insert(int key, T val) {
BSTNode<T> p = current, q = current;
while (p != null) {
q = p;
if (p.key == key) {
return false;
} else if (key < p.key)
p = p.left;
else
p = p.right;
}
p = new BSTNode<T>(key, val);
if (empty()) {
root = current = p;
return true;
} else {
// current is pointing to parent of the new key
if (key < current.key)
current.left = p;
else
current.right = p;
current = p;
return true;
}
}
public void display() {
if (root == null)
System.out.println("BST IS EMPTY");
else
displayin(root);
System.out.println();
}
private void displayin(BSTNode<T> p) {
if (p != null) {
displayin(p.left);
System.out.print(p.key + " " + p.data + " , ");
displayin(p.right);
}
}
}
In your insert method, the first code part:
while (p != null) {
q = p;
if (p.key == key) {
current = p;
return true;
} else if (key < p.key)
p = p.left;
else
p = p.right;
}
simply returns true when the key you want to write to is found. It does however not insert val here at all. You could have detected this by calling the insert method on some tree and then printing that tree, comparing the outcome that you had expected with the actual outcome. In automated form, you could do this with JUnit tests.
I think I know want you want (now). I have changed the "display"-Method to toString, rewrote insert and implemented a form of bfs for toString.
import java.util.ArrayList;
class BSTNode<T> {
public int key;
public T data;
public BSTNode<T> left, right;
public BSTNode(int k, T val) {
key = k;
data = val;
left = right = null;
}
public BSTNode(int k, T val, BSTNode<T> l, BSTNode<T> r) {
key = k;
data = val;
left = l;
right = r;
}
#Override
public String toString() {
return "[" + key + ":" + data + "]";
}
}
public class BST<T> {
BSTNode<T> root, current;
public BST() {
root = current = null;
}
public boolean empty() {
return root == null;
}
public T retrieve() {
return current.data;
}
public boolean findkey(int tkey) {
BSTNode<T> current = root;
while (current != null) {
if (current.key == tkey)
return true;
if (tkey < current.key)
current = current.left;
else
current = current.right;
}
return false;
}
public boolean insert(int key, T val) {
BSTNode<T> node = new BSTNode<>(key, val);
if (root == null) {
root = node;
} else {
BSTNode<T> current = root;
while (current != null) {
if (key == current.key)
return false;
if (key < current.key) {
if (current.left == null) {
current.left = node;
return true;
}
current = current.left;
} else {
if (current.right == null) {
current.right = node;
return true;
}
current = current.right;
}
}
}
return false;
}
public String toString() {
if (root == null)
return "BST IS EMPTY";
ArrayList<BSTNode<T>> visited = new ArrayList<>();
ArrayList<BSTNode<T>> queue = new ArrayList<>();
queue.add(root);
while (!queue.isEmpty()) {
BSTNode<T> vertex = queue.remove(0);
if (vertex != null) {
queue.add(vertex.left);
queue.add(vertex.right);
}
visited.add(vertex);
}
return visited.toString();
}
}

Interval search in Binary Tree

Can someone help me with the interval search in binary tree.
I understand how to check left side of the tree,but I have troubles with chicking right side of it.
This is my code by now.
private boolean search(BSTNode r, int from,int till){
boolean found = false;
int arr[];
arr=new int[10];
int i=0;
while (r != null)
{
int rval = r.getData();
if (from < rval && till >rval) {
r = r.getLeft();
arr[i]=rval;
i++;
}else
r=r.getRight();
}
return found;
}
This is full class of BSTNode.
From and till it is range of interval(from
class BSTNode
{
BSTNode left, right;
int data;
/* Constructor */
public BSTNode()
{
left = null;
right = null;
data = 0;
}
/* Constructor */
public BSTNode(int n)
{
left = null;
right = null;
data = n;
}
/* Function to set left node */
public void setLeft(BSTNode n)
{
left = n;
}
/* Function to set right node */
public void setRight(BSTNode n)
{
right = n;
}
/* Function to get left node */
public BSTNode getLeft()
{
return left;
}
/* Function to get right node */
public BSTNode getRight()
{
return right;
}
You can search by using queue like this:
public boolean search(Integer from, Integer till) {
return search(root, from, till);
}
private boolean search(BSTNode root, Integer from, Integer till) {
boolean found = false;
Queue<BSTNode> queue = new ArrayDeque<>();
queue.add(root);
List<Integer> list = new ArrayList<>();
while (!queue.isEmpty()) {
BSTNode node = queue.poll();
int data = node.getData();
if (from < data && till > data) {
found = true;
list.add(data);
}
if (node.getLeft() != null)
queue.add(node.getLeft());
if (node.getRight() != null)
queue.add(node.getRight());
}
System.out.println(list);
return found;
}
, full code
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
// Tree building...
tree.insert(50);
tree.insert(40);
tree.insert(20);
tree.insert(10);
tree.insert(50);
System.out.println(tree.search(10, 30));
}
static class BinarySearchTree {
private BSTNode root;
public void insert(Integer item) {
root = insert(root, item);
}
private BSTNode insert(BSTNode node, Integer item) {
if (node == null) {
return new BSTNode(item);
} else if (item.compareTo(node.data) == 0) {
return node;
} else if (item.compareTo(node.data) < 0) {
node.setRight(insert(node.r, item));
return node;
} else {
node.setLeft(insert(node.l, item));
return node;
}
}
public Integer find(Integer target) {
return find(root, target);
}
private Integer find(BSTNode node, Integer target) {
if (node == null) {
return null;
}
Integer cmd = target.compareTo(node.data);
if (cmd == 0) {
return node.data;
} else if (cmd < 0) {
return find(node.getRight(), target);
} else {
return find(node.getLeft(), target);
}
}
public boolean search(Integer from, Integer till) {
return search(root, from, till);
}
private boolean search(BSTNode root, Integer from, Integer till) {
boolean found = false;
Queue<BSTNode> queue = new ArrayDeque<>();
queue.add(root);
List<Integer> list = new ArrayList<>();
while (!queue.isEmpty()) {
BSTNode node = queue.poll();
int data = node.getData();
if (from < data && till > data) {
found = true;
list.add(data);
}
if (node.getLeft() != null)
queue.add(node.getLeft());
if (node.getRight() != null)
queue.add(node.getRight());
}
System.out.println(list);
return found;
}
}
static class BSTNode {
Integer data;
BSTNode l = null;
BSTNode r = null;
public BSTNode(Integer data) {
super();
this.data = data;
}
public Integer getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BSTNode getLeft() {
return l;
}
public void setLeft(BSTNode l) {
this.l = l;
}
public BSTNode getRight() {
return r;
}
public void setRight(BSTNode r) {
this.r = r;
}
#Override
public String toString() {
return "BSTNode [data=" + data + ", l=" + l + ", r=" + r + "]";
}
}
, the output
[20]
true

My setter is not setting

So I am trying to implement BST (Binary Search Tree) I have made an add method which adds TreeNodes to an tree[TreeNode] array
Here is the TreeNode class where I'm trying to set the parent as well as the left and right nodes, I inspected with a debugger and I'm not sure why but it is not setting the Parent var and also it only add one or the other in the leftChild and rightChild fields.
The setter in question is this one
//set parent
public void setParent(TreeNode t)
{
this.parent = t.parent;
}
I cant understand when i call it from the PAS43DEPQ class it does not set properly.
class TreeNode implements Comparable<TreeNode>
{
private Integer value;
private TreeNode leftChild;
private TreeNode rightChild;
private TreeNode parent;
//constructors
public TreeNode(){}
public TreeNode(Integer v){this.value = v;}
public TreeNode(TreeNode t){
this.value = t.value;
this.parent = t.parent;
this.leftChild = t.leftChild;
this.rightChild = t.rightChild;
}
public TreeNode (Comparable c){this.value = (int) c;}
//set parent
public void setParent(TreeNode t)
{
this.parent = t.parent;
}
//get parent
public TreeNode getParent()
{
return this.parent;
}
//get value
public int getValue(){return value;}
//set value
public void setValue(Integer i){ this.value = i;}
//get left node
public TreeNode getLeftChild(){return leftChild;}
//get right node
public TreeNode getRightChild(){return rightChild;}
//set left child
public void setLeftChild(TreeNode t) {this.leftChild = t;}
//set right child
public void setRightChild(TreeNode t) {this.rightChild = t;}
public TreeNode find(int n)
{
//this statement runs if the current node is == the value being searched.
if(this.value == n)
return this;
//this returns values left of the root then performs a recursive call if not found
if(value < this.value && leftChild != null)
return leftChild.find(n);
//this does the same as above except looks on the right side of the root
if(rightChild != null)
return rightChild.find(n);
//this returns if value is not found
return null;
}
#Override
public int compareTo(TreeNode o)
{
if (this.value == o.value)
{
return 0;// if value equal
}
if (this.value > o.value) //if value greater
{
return 1;
}
if (this.value < o.value)
{
return -1; //if value less
}
return 99;
}
}
Here is the class where I add from:
public class PAS43DEPQ implements DEPQ
{
private TreeNode[] tree = new TreeNode[100];
int index = 0;
#Override
public Comparable inspectLeast() {
return null;
}
#Override
public Comparable inspectMost() {
return null;
}
/*
right: (2 * n) + 2
left: (2 * n) + 1
parent: (1 - n) / 2
*/
public int right()
{
return (2 * index) + 2;
}
public int left()
{
return (2 * index) + 1;
}
public int parent()
{
return Math.round((index - 1) / 2);
}
#Override
public void add(Comparable c)
{
// Root node
if (tree[0] == null) {
tree[0] = new TreeNode(c);
return;
}
//this while loop is for tree traversal
while(tree[index] != null) {
if( c.compareTo(tree[index].getValue()) == 0) {
index += right() - index;
continue;
}
if( c.compareTo(tree[index].getValue()) > 0) {
index += right() - index;
continue;
}
if( c.compareTo(tree[index].getValue()) < 0) {
index += left() - index;
continue;
}
}
//this part is for place the new node
if(tree[index] == null) {
tree[index] = new TreeNode(c);
tree[index].setParent(tree[parent()]);
if( c.compareTo(tree[index].getValue()) == 0)
tree[parent()].setRightChild(tree[index]);
if( c.compareTo(tree[index].getValue()) > 0)
tree[parent()].setRightChild(tree[index]);
if( c.compareTo(tree[index].getValue()) < 0)
tree[parent()].setLeftChild(tree[index]);
index = 0;
}
return;
}
#Override
public Comparable getLeast() {
return null;
}
#Override
public Comparable getMost() {
return null;
}
#Override
public boolean isEmpty() {
return (tree[0] == null) ? true : false;
}
#Override
public int size() {
return tree.length;
}
}
I cant seam to work out why the parent isnt being set the the the line
"tree[index].setParent(tree[parent()])"
is being called? any ides on why this is happening?
The set method should be like this
//set parent
public void setParent(TreeNode t)
{
this.parent = t;
}
This method will make TreeNode t as the parent of the Current Node referenced by this.
The statement which you are using sets parent of TreeNode t as the parent of the current node.

AVL Tree gives me O(c^n) instead of O(log n)

I've created an AVLTree in java and the add method should be of O(log n)... However my add method seems to give me a graph of O(c^n) or an exponential graph instead of a logarithmic graph. Here is the graph of running time vs input size:
Can anyone help figure out as to why this happens?
Here is the code for my AVLTree:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import cw1.rs10.lib.IAVLTree;
public class AVLTree<K, V> implements IAVLTree<K, V>
{
public class Node {
private K key;
private ArrayList<V> values;
private Node left, right;
private int height;
public Node(K key, V value) {
this.key = key;
this.values = new ArrayList<V>();
this.left = null;
this.right = null;
this.height = 0;
values.add(value);
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public ArrayList<V> getValues() {
return values;
}
public void addValue(V value) {
values.add(value);
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setLeft(Node left) {
this.left = left;
}
public void setRight(Node right) {
this.right = right;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
private Node rootNode;
private Comparator<K> comparator;
//Unused
public AVLTree() {
}
public AVLTree(Comparator<K> comparator) {
this.rootNode = null;
this.comparator = comparator;
}
#Override
public V add(K k, V v) {
Node n = rootNode = add(k, v, rootNode);
if(n != null)
return v;
else
return null;
}
private Node add(K key, V value, Node node) {
if(node == null)
return new Node(key, value);
if(comparator.compare(key, node.getKey()) < 0) {
node.setLeft(add(key, value, node.getLeft()));
if(height(node.getLeft()) - height(node.getRight()) == 2) {
if(comparator.compare(key, node.getLeft().getKey()) < 0)
node = rotateLeft(node);
else
node = doubleRotateLeft(node);
}
} else if(comparator.compare(key, node.getKey()) > 0) {
node.setRight(add(key, value, node.getRight()));
if(height(node.getRight()) - height(node.getLeft()) == 2) {
if(comparator.compare(key, node.getRight().getKey()) > 0)
node = rotateRight(node);
else
node = doubleRotateRight(node);
}
} else {
//Handle duplicate
node.getValues().add(value);
}
node.setHeight( Math.max(height(node.getLeft()), height(node.getRight())) + 1 );
return node;
}
#Override
public V remove(K key, V value) throws Exception {
Node node = rootNode = remove(key, value, rootNode);
if(node != null)
return value;
else
return null;
}
private Node remove(K key, V value, Node node) {
//If node with key contains one or less values, remove the whole key
//Else remove value from node with key
if(node == null) return null;
else if(comparator.compare(key, node.getKey()) < 0) {
node.setLeft(remove(key, value, node.getLeft()));
if(height(node.getLeft()) - height(node.getRight()) == 2) {
if(comparator.compare(key, node.getLeft().key) < 0)
node = rotateLeft(node);
else
node = doubleRotateLeft(node);
}
} else if(comparator.compare(key, node.getKey()) > 0) {
node.setRight(remove(key, value, node.getRight()));
if(height(node.getRight()) - height(node.getLeft()) == 2) {
if(comparator.compare(key, node.getRight().key) < 0)
node = rotateRight(node);
else
node = doubleRotateRight(node);
}
} else {
if(node.getValues().size() > 1) {
node.getValues().remove(value);
return node;
} else {
if(node.getLeft() == null && node.getRight() == null)
return null;
if(node.getLeft() == null) return node.getRight();
if(node.getRight() == null) return node.getLeft();
Node smallestNode = smallestNode(node.getRight());
node = smallestNode;
node.setRight(remove(key, value, node.getRight()));
return node;
}
}
return node;
}
#Override
public Iterator<V> find(K key) {
Node n = search(key, rootNode);
if(n != null) {
ArrayList<V> values = n.getValues();
return values.iterator();
} else {
return new ArrayList<V>().iterator();
}
}
private Node search(K key, Node node) {
while(node != null) {
if(comparator.compare(key, node.getKey()) < 0)
node = node.getLeft();
else if(comparator.compare(key, node.getKey()) > 0)
node = node.getRight();
else
return node;
}
return null;
}
#Override
public Iterator<V> removeAll(K key) {
Node n = search(key, rootNode);
ArrayList<V> values = n.getValues();
try {
remove(n.getKey(), null);
} catch (Exception e) {
e.printStackTrace();
}
return values.iterator();
}
#Override
public Iterator<V> listAll() {
ArrayList<V> entries = new ArrayList<V>();
listAll(rootNode, entries);
return entries.iterator();
}
private void listAll(Node n, ArrayList<V> entries) {
if(n != null) {
listAll(n.getLeft(), entries);
entries.addAll(n.getValues());
listAll(n.getRight(), entries);
}
}
#Override
public int height() {
return height(rootNode);
}
//Custom Methods
/**
* A method to test if the tree is logically empty
*
* #return true if empty, false if not
*/
public boolean isEmpty() {
return rootNode == null;
}
/**
* Logically empties the tree by setting the rootNode to null
*/
public void empty() {
rootNode = null;
}
public void inOrderTraversal(Node node) {
if(node != null) {
inOrderTraversal(node.getLeft());
System.out.print(node.getKey() + ", ");
inOrderTraversal(node.getRight());
}
}
public int height(Node node) {
if(node == null) return -1;
else return node.height;
}
public Node getRootNode() {
return rootNode;
}
public Node smallestNode(Node node) {
if(node.getLeft() == null)
return node;
return smallestNode(node.getLeft());
}
private Node rotateLeft(Node node2) {
Node node1 = node2.getLeft();
node2.setLeft(node1.getRight());
node1.setRight(node2);
node2.setHeight(Math.max(height(node2.getLeft()), height(node2.getRight())) + 1);
node1.setHeight(Math.max(height(node1.getLeft()), node2.getHeight()) + 1);
return node1;
}
private Node rotateRight(Node node1) {
Node node2 = node1.getRight();
node1.setRight(node2.getLeft());
node2.setLeft(node1);
node1.setHeight(Math.max(height(node1.getLeft()), height(node1.getRight())) + 1);
node2.setHeight(Math.max(height(node2.getRight()), node1.getHeight()) + 1);
return node2;
}
private Node doubleRotateLeft(Node node3) {
node3.setLeft(rotateRight(node3.getLeft()));
return rotateLeft(node3);
}
private Node doubleRotateRight(Node node1) {
node1.setRight(rotateLeft(node1.getRight()));
return rotateRight(node1);
}
}
The interface for my AVLTree:
import java.util.Iterator;
public interface IAVLTree<K,V>
{
public V add(K k, V v);
public V remove(K k, V v);
public Iterator<V> removeAll(K k);
public Iterator<V> find(K k);
public Iterator<V> listAll();
public int height();
}
And finally, my testing code:
public class AVLTest
{
private static long startTime, endTime;
private static int amountOfCommands = 10000;
public static void main(String[] args) {
AVLTree<String, Integer> tree = new AVLTree<String, Integer>(String.CASE_INSENSITIVE_ORDER);
try {
startTime = System.currentTimeMillis();
for (int i = 1; i <= amountOfCommands; i++) {
String key = "K" + i;
tree.add(key, i);
}
endTime = System.currentTimeMillis();
} catch(Exception e) {
e.printStackTrace();
}
long runningTime = endTime - startTime;
System.out.println("Running Time: " + runningTime + "ms\nNo. of Commands: " + amountOfCommands);
}
}
You are measuring it wrong. Your test code measures the time to add all of the elements in the tree, instead of just one.
startTime = System.currentTimeMillis();
for (int i = 1; i <= amountOfCommands; i++) {
String key = "K" + i;
tree.add(key, i);
}
endTime = System.currentTimeMillis();
What you want to measure is the time it takes to add one node to the tree as a function of the number of nodes already in the tree.
for (int i = 1; i < amountOfCommands; i++) { // note the < instead of <=
String key = "K" + i;
tree.add(key, i);
}
String key = "K" + amountOfCommands;
startTime = System.currentTimeMillis();
tree.add(key, amountOfCommands);
endTime = System.currentTimeMillis();
Of course you can run your test more efficiently if you reuse the same tree for all measurements. I'll leave it to you.

Getting error: Incomparable types - Object and int

My code is below but when I build the project, Netbeat gives me this warning:
Error: incomparable types: Object and int
if (p.info == x) {
Does anyone know how to fix this error? Thanks.
Here's error code:
public void insert(int x) {
if (root == null) {
root = new Node(x);
return;
}
Node f, p;
p = root;
f = null;
while (p != null) {
if (p.info == x) {
System.out.println(" The key " + x + " already exists, no insertion");
return;
}
f = p;
if (x < (int) p.info) {
p = p.left;
} else {
p = p.right;
}
}
if (x < (int) f.info) {
f.left = new Node(x);
} else {
f.right = new Node(x);
}
}
Here's all the code I have so far:
package ass6;
import java.util.ArrayList;
public class Ass6 {
public static void main(String[] args) {
BinarySearchTree bsTree = new BinarySearchTree();
String[] c = {"C6", "C2", "C7", "C1", "C5", "C9", "C4", "C8", "C3"};
bsTree.insertMany(c);
System.out.println("Calculate level of all nodes:");
bsTree.breadth_first_traverse3();
System.out.println("");
bsTree.calLevel(bsTree.root, 1);
bsTree.breadth_first_traverse();
System.out.println("");
System.out.println("\nCalculate the height of the tree:");
bsTree.calheight(bsTree.root);
bsTree.breadth_first_traverse1();
System.out.println("");
System.out.println("Calculate balance factor of all nodes:");
bsTree.breadth_first_traverse3();
System.out.println("");
bsTree.calbaFactor(bsTree.root);
bsTree.breadth_first_traverse2();
bsTree.AVL(bsTree.root);
System.out.println("");
System.out.println("Balance a binary search tree:");
bsTree.breadth_first_traverse3();
System.out.println("");
bsTree.balance();
bsTree.breadth_first_traverse3();
System.out.println("");
}
}
class BinarySearchTree {
Node root;
int height;
int i;
void clear() {
root = null;
}
public BinarySearchTree() {
root = null;
}
Node search(Node p, int x) {
if (p == null) {
return (null);
}
if ((int) p.info == x) {
return (p);
}
if (x < (int) p.info) {
return (search(p.left, x));
} else {
return (search(p.right, x));
}
}
public void insertMany(int a[]) {
for (int x : a) {
insert(x);
}
}
public void insertMany(String a[]) {
for (String x : a) {
insert(x);
}
}
public void insert(String x) {
if (root == null) {
root = new Node(x);
return;
}
Node f, p;
p = root;
f = null;
while (p != null) {
if (p.info == x) {
System.out.println(" The key " + x + " already exists, no insertion");
return;
}
f = p;
if (x.compareTo(p.info.toString()) < 0) {
p = p.left;
} else {
p = p.right;
}
}
if (x.compareTo(f.info.toString()) < 0) {
f.left = new Node(x);
} else {
f.right = new Node(x);
}
}
public void insert(int x) {
if (root == null) {
root = new Node(x);
return;
}
Node f, p;
p = root;
f = null;
while (p != null) {
if (p.info == x) {
System.out.println(" The key " + x + " already exists, no insertion");
return;
}
f = p;
if (x < (int) p.info) {
p = p.left;
} else {
p = p.right;
}
}
if (x < (int) f.info) {
f.left = new Node(x);
} else {
f.right = new Node(x);
}
}
public void visitLevel(Node x) {
System.out.print("(" + x.info + "," + x.level + "), ");
}
public void visit(Node x) {
System.out.print(x.info + ", ");
}
public void visitheight(Node x) {
System.out.print("(" + x.info + "," + x.height + "), ");
}
public void visitbafator(Node x) {
System.out.print("(" + x.info + "," + x.baFactor + "), ");
}
public void breadth_first_traverse() {
MyQueue queue = new MyQueue();
Node p = root;
if (p != null) {
queue.enqueue(p);
}
while (!queue.isEmpty()) {
Node q = (Node) queue.dequeue();
visitLevel(q);
if (q.left != null) {
queue.enqueue(q.left);
}
if (q.right != null) {
queue.enqueue(q.right);
}
}
}
public void breadth_first_traverse1() {
MyQueue queue = new MyQueue();
Node p = root;
if (p != null) {
queue.enqueue(p);
}
while (!queue.isEmpty()) {
Node q = (Node) queue.dequeue();
visitheight(q);
if (q.left != null) {
queue.enqueue(q.left);
}
if (q.right != null) {
queue.enqueue(q.right);
}
}
System.out.println("");
}
public void breadth_first_traverse2() {
MyQueue queue = new MyQueue();
Node p = root;
if (p != null) {
queue.enqueue(p);
}
while (!queue.isEmpty()) {
Node q = (Node) queue.dequeue();
visitbafator(q);
if (q.left != null) {
queue.enqueue(q.left);
}
if (q.right != null) {
queue.enqueue(q.right);
}
}
System.out.println("");
}
public void breadth_first_traverse3() {
MyQueue queue = new MyQueue();
Node p = root;
if (p != null) {
queue.enqueue(p);
}
while (!queue.isEmpty()) {
Node q = (Node) queue.dequeue();
visit(q);
if (q.left != null) {
queue.enqueue(q.left);
}
if (q.right != null) {
queue.enqueue(q.right);
}
}
}
public void inorder(Node x) {
if (x == null) {
return;
}
inorder(x.left);
visit(x);
inorder(x.right);
}
public void calLevel(Node n, int i) {
if (n.left == null && n.right == null) {
n.level = i;
} else {
n.level = i;
if (n.left != null) {
calLevel(n.left, i + 1);
}
if (n.right != null) {
calLevel(n.right, i + 1);
}
}
}
public int treeheight(Node p) {
if (p == null) {
return 0;
}
int left = treeheight(p.left);
int right = treeheight(p.right);
return 1 + Math.max(left, right);
}
public void calheight(Node n) {
if (n.left == null && n.right == null) {
n.height = 1;
} else {
n.height = treeheight(n);
if (n.left != null) {
calheight(n.left);
}
if (n.right != null) {
calheight(n.right);
}
}
}
public void calbaFactor(Node n) {
if (n.left == null && n.right == null) {
n.baFactor = 0;
} else {
n.baFactor = treeheight(n.right) - treeheight(n.left);
if (n.baFactor > 1 || n.baFactor < -1) {
i++;
}
if (n.left != null) {
calbaFactor(n.left);
}
if (n.right != null) {
calbaFactor(n.right);
}
}
}
public void AVL(Node n) {
if (i > 0) {
System.out.println("The tree is not an AVL tree .");
} else {
System.out.println("The tree is an AVL tree .");
}
}
void inOrder(ArrayList<String> t, Node p) {
if (p == null) {
return;
}
inOrder(t, p.left);
t.add((String) p.info);
inOrder(t, p.right);
}
void balance(ArrayList<String> t, int i, int j) {
if (i > j) {
return;
}
int k = (i + j) / 2;
String x = t.get(k);
insert(x);
balance(t, i, k - 1);
balance(t, k + 1, j);
}
void balance() {
ArrayList<String> t = new ArrayList<>();
inOrder(t, root);
int n = t.size();
clear();
balance(t, 0, n - 1);
}
}
class Node {
Object info;
int level = 1;
int height = 0;
int baFactor = 0;
Node left, right;
public Node(Object inf, Node le, Node ri) {
info = inf;
left = le;
right = ri;
}
public Node(Object inf) {
info = inf;
left = right = null;
}
}
class MyQueue {
ArrayList al = new ArrayList();
MyQueue() {
}
public void enqueue(Object x) {
al.add(x);
}
public Object dequeue() {
Object obj = al.get(0);
al.remove(0);
return obj;
}
public boolean isEmpty() {
if (al.size() <= 0) {
return true;
} else {
return false;
}
}
}
In java, you cannot compare primitive types (int, float, double, byte etc) with instance types (everything derived from Object and then instantiated).
Use p.info.equals(x)
here p.info is an Object,and x is an int so you cannot simply compare with ==. but
.equals will work, becase the value will be compared.
Object obj = Integer.valueOf(3);
int x=3;
if ( obj.equals(x) ) { // true
}

Categories

Resources