So I've stumped on this current problem I'm working on. Basically, I need to add an element to my array based binary search tree. According to my text it is similar to the compareTo method. I'm not even sure what direction to head in. I'm a complete noob when it comes to OOP so any help would be appreciated.
package lab9;
public class BinarySearchTreeArray<E> {
Entry<E> [] tree;
Entry<E> root;
int size;
public BinarySearchTreeArray()
{
tree = null;
size = 0;
}
public int size()
{
return size;
}
public boolean contains(Object obj)
{
Entry<E> temp = root;
int comp;
if (obj == null)
throw new NullPointerException();
while (obj != null)
{
comp = ((Comparable)obj).compareTo (temp.element);
if (comp == 0)
return true;
else if (comp < 0)
temp = temp.left;
else
temp = temp.right;
}//while
return false;
}//contains method
/*
* From the text:
* The definition of the add (E element) method is only a little more
* complicated than the definition of contains (Object obj). Basically,
* the add method starts at the root and branches down the tree
* searching for the element; if the search fails, the element is
* inserted as a leaf.
*/
public void add(E e)
{
Entry<E> node = new Entry<E>(e);
if (tree[parent] == null)
{
tree[0] = node;
size++;
}
else
{
tree[1] = node;
size++;
}
}//add method
/****************************************************************/
protected static class Entry<E>
{
private E element;
private Entry<E> parent, left, right;
public Entry(E e){this.element = element; left = right = null;}
public Entry<E> getLeft(){return left;}
public Entry<E> getRight(){return right;}
}
/****************************************************************/
public static void main(String[] args) {
BinarySearchTreeArray<String> bsta1 = new BinarySearchTreeArray<String>();
BinarySearchTreeArray<Integer> bsta2 = new BinarySearchTreeArray<Integer>();
bsta1.add("dog");
bsta1.add("tutle");
bsta1.add("cat");
bsta1.add("ferrit");
bsta1.add("shark");
bsta1.add("whale");
bsta1.add("porpoise");
bsta2.add(3);
bsta2.add(18);
bsta2.add(4);
bsta2.add(99);
bsta2.add(50);
bsta2.add(23);
bsta2.add(5);
bsta2.add(101);
bsta2.add(77);
bsta2.add(87);
}
}
The add method is indeed similar to your contains method. In a typical binary tree represented with structs/objects you would access the right and left subtrees using pointers (as in your example temp.left and temp.right). But, since you have a tree in an array you need to access that array by index, so the question is : How to access the index that corresponds to the left/right subtrees?
For that, you can use the following expression left = parent * 2 and right = parent * 2 + 1. I will provide you with one example of the add method that would add elements to a tree represented as an array of integers, where -1 represents no values or null in java.
public void add(E e)
{
Entry<E> node = new Entry<E>(e);
index = 0;
int comp;
boolean not_add = true;
while(not_add)
{
if (tree[index] == null) //if this node is empty
{
tree[index] = node;
size++;
not_add = true;
}
comp = ((Comparable)e).compareTo (tree[index].element);
if(comp == 0) not_add = true; // Same value
else if (comp < 0) index = index * 2; // should be insert on the left
else index = index * 2 + 1; // should be insert on the right
}
}
Related
This question already has answers here:
What causes "Can't find Symbol" and how to fix it?
(5 answers)
Closed 5 years ago.
I can't seem to make my program that implements a Binary Search Tree (using User input) and search for a value, to print out the number of iterations that were necessary to actually find this value.
I've created a method called 'getLastIterationCount()' that returns the number of iterations but when I want to print it out in my main method, I get an error on the line 'System.out.println(getLastIterationCount());'. I think that my method is not in the right place but I'm not sure whats missing. Any ideas how I could make this program work?
/* Class Node */
class Node
{
Node left, right;
int data;
/* Constructor */
public Node(int n)
{
left = null;
right = null;
data = n;
}
/* Function to get data from node */
public int getData()
{
return data;
}
/* Function to get left node */
public Node getLeft()
{
return left;
}
/* Function to get right node */
public Node getRight()
{
return right;
}
}
/* Class BST */
class BST
{
private Node root;
private int iterations;
/* Constructor */
public BST()
{
root = null;
}
/* Functions to insert data */
public void insert(int data)
{
root = insert(root, data);
}
/* Function to insert data recursively */
private Node insert(Node node, int data)
{
if (node == null)
node = new Node(data);
else
{
if (data <= node.data)
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
}
return node;
}
/* Functions to search for an element */
public boolean search(int val)
{
iterations=0;
iterations++;
return search(root, val);
}
/* Function to search for an element recursively */
private boolean search(Node r, int val)
{
iterations=0;
boolean found = false;
while ((r != null) && !found)
{
int rval = r.getData();
if (val < rval){
r = r.getLeft();
}
else if (val > rval){
r = r.getRight();
}
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
public int getLastIterationCount(){
return iterations;
}
}
/* Class LinkedListBST */
public class LinkedListBST
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of BST */
BST bst = new BST();
System.out.println("Linked List Binary Search Tree Test\n");
char ch;
/* Accept input */
do
{
System.out.println("Enter integer element to insert");
bst.insert( scan.nextInt() );
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
System.out.println("\nEnter an element to be searched: ");
Scanner sc = new Scanner(System.in);
System.out.println("Search result : " + bst.search(sc.nextInt()));
System.out.println(getLastIterationCount()); //ISSUE IS HERE
sc.close();
}
}
You are accessing the method getLastIterationCount()without the object. Please call it using bst.getLastIterationCount()
You should call the method using the object bst that you have instantiated as below :
bst.getLastIterationCount();
I think your search code is already close to being correct. First, initialize the counter at the external entry point to search:
public boolean search(int val) {
iterations = 1;
return search(root, val);
}
Then, at each call to the private internal search method, increment the counter by one:
private boolean search(Node r, int val) {
++iterations; // increment counter by one for current iteration
boolean found = false;
while ((r != null) && !found) {
int rval = r.getData();
if (val < rval){
r = r.getLeft();
}
else if (val > rval) {
r = r.getRight();
}
else {
found = true;
break;
}
found = search(r, val);
}
return found;
}
I have assumed here that you want the number of recursive calls necessary to find the value. If instead you wanted the height at which an item was found, it is a different question.
Edit:
I noticed that you have a getter method for returning the count. It is an instance method and you should therefore call it as such:
bst.getLastIterationCount()
I've read all the posts related to this question, yet I am still having a very hard time understanding how to implement the algorithm.
I have a fully written BST with methods that recursively add, delete etc Student objects(the BST class is written for generics, though). I need to write a method that returns the rank of a student in the binary search tree based off their GPA, yet I do not understand how I should implement this. The Student class has a getGPA() method, so I know ill need to use that somehow.
Should I store the elements into an array then search for the index? If so, what's the best algorithm to accomplish this? Is there a better way to do this?
Here is the important parts of my BST class:
public int size()
// Returns the number of elements in this BST.
{
return recSize(root);
}
private int recSize(BSTNode<T> tree)
// Returns the number of elements in tree.
{
if (tree == null)
return 0;
else
return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1;
}
private boolean recContains(T element, BSTNode<T> tree)
// Returns true if tree contains an element e such that
// e.compareTo(element) == 0; otherwise, returns false.
{
if (tree == null)
return false; // element is not found
else if (element.compareTo(tree.getInfo()) < 0)
return recContains(element, tree.getLeft()); // Search left subtree
else if (element.compareTo(tree.getInfo()) > 0)
return recContains(element, tree.getRight()); // Search right subtree
else
return true; // element is found
}
public boolean contains (T element)
// Returns true if this BST contains an element e such that
// e.compareTo(element) == 0; otherwise, returns false.
{
return recContains(element, root);
}
private T recGet(T element, BSTNode<T> tree)
// Returns an element e from tree such that e.compareTo(element) == 0;
// if no such element exists, returns null.
{
if (tree == null)
return null; // element is not found
else if (element.compareTo(tree.getInfo()) < 0)
return recGet(element, tree.getLeft()); // get from left subtree
else
if (element.compareTo(tree.getInfo()) > 0)
return recGet(element, tree.getRight()); // get from right subtree
else
return tree.getInfo(); // element is found
}
public T get(T element)
// Returns an element e from this BST such that e.compareTo(element) == 0;
// if no such element exists, returns null.
{
return recGet(element, root);
}
private BSTNode<T> recAdd(T element, BSTNode<T> tree)
// Adds element to tree; tree retains its BST property.
{
if (tree == null)
// Addition place found
tree = new BSTNode<T>(element);
else if (element.compareTo(tree.getInfo()) <= 0)
tree.setLeft(recAdd(element, tree.getLeft())); // Add in left subtree
else
tree.setRight(recAdd(element, tree.getRight())); // Add in right subtree
return tree;
}
public void add (T element)
// Adds element to this BST. The tree retains its BST property.
{
root = recAdd(element, root);
}
private void inOrder(BSTNode<T> tree)
// Initializes inOrderQueue with tree elements in inOrder order.
{
if (tree != null)
{
inOrder(tree.getLeft());
inOrderQueue.enqueue(tree.getInfo());
inOrder(tree.getRight());
}
}
private void preOrder(BSTNode<T> tree)
// Initializes preOrderQueue with tree elements in preOrder order.
{
if (tree != null)
{
preOrderQueue.enqueue(tree.getInfo());
preOrder(tree.getLeft());
preOrder(tree.getRight());
}
}
private void postOrder(BSTNode<T> tree)
// Initializes postOrderQueue with tree elements in postOrder order.
{
if (tree != null)
{
postOrder(tree.getLeft());
postOrder(tree.getRight());
postOrderQueue.enqueue(tree.getInfo());
}
}
public int reset(int orderType)
// Initializes current position for an iteration through this BST
// in orderType order. Returns current number of nodes in the BST.
{
int numNodes = size();
if (orderType == INORDER)
{
inOrderQueue = new LinkedUnbndQueue<T>();
inOrder(root);
}
else
if (orderType == PREORDER)
{
preOrderQueue = new LinkedUnbndQueue<T>();
preOrder(root);
}
if (orderType == POSTORDER)
{
postOrderQueue = new LinkedUnbndQueue<T>();
postOrder(root);
}
return numNodes;
}
After several failures, it seems I have found a workable solution. Instead of storing in a generic array with a fixed size, I decided to try an ArrayList instead. I use a recursive method to add each node inorder to the array list:
//recursive method used to copy BST to array list
public void recArrayList(BSTNode<T> tree, ArrayList<T> list)
{
if (tree == null)
{
return;
}
recArrayList(tree.getLeft(), list);
list.add(tree.getInfo());
recArrayList(tree.getRight(), list);
}
//converts BST to arraylist used for finding GPA rank
public ArrayList<T> toArrayList()
{
stuList = new ArrayList<T>();
recArrayList(root, stuList);
return stuList;
}
And finally in the class using the BST:
//returns the rank of student in BST based off GPA
public int rank(Student stu)
{
ArrayList<Student> stuList = stuGPA.toArrayList();
for (i = 0; i < stuList.size(); i++)
{
if (stuList.get(i) == stu)
{
return i + 1;
}
}
return 0;
}
I dont like using == to compare references so i'm working on changing that, but for now this seems to be functioning correctly. Ty for the comments, it inspired me to go a different route.
NOTICE: this is homework-related, but I'm not tagging it as such because the 'homework' tag is marked as obselete (?)
Using the following class that implements a binary tree...
class TreeNode
{
private Object value;
private TreeNode left, right;
public TreeNode(Object initValue)
{
value = initValue;
left = null;
right = null;
}
public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight)
{
value = initValue;
left = initLeft;
right = initRight;
}
public Object getValue()
{
return value;
}
public TreeNode getLeft()
{
return left;
}
public TreeNode getRight()
{
return right;
}
public void setValue(Object theNewValue)
{
value = theNewValue;
}
public void setLeft(TreeNode theNewLeft)
{
left = theNewLeft;
}
public void setRight(TreeNode theNewRight)
{
right = theNewRight;
}
}
I need to calculate the number of nodes in the binary tree that are "only children," this being defined as a node that doesn't have another node stemming from its parent.
This is what I have so far:
public static int countOnlys(TreeNode t)
{
if(t == null)
return 0;
if(isAnOnlyChild(t))
return 1;
return countOnlys(t.getLeft()) + countOnlys(t.getRight());
}
I don't know how to implement the boolean method isAnOnlyChild(TreeNode t)
Could someone please help me?
You are pretty close and have the traversal looking good but in your Treenode you do not have a link between a child and its parent. So You can not tell from say a left child if a sibling (right child) exists.
You could have a parent Treenode (along with left and right) so you could check how many children a given node's parent has. Or as ajp15243 suggested, instead use a method that checks how many children a given node has.
Some pseudo code of the latter:
//we still need to check if that only child has its own children
if hasOnlyChild(t)
return 1 + checkOnlys(left) + checkOnlys(right)
else
return checkOnlys(left) + checkOnlys(right)
As you have already noticed, one solution is to count number of parents that have only one child. This should work:
public static int countOnlys(TreeNode t)
{
if(t == null || numberOfChildren(t)==0){
return 0;
}
if(numberOfChildren(t)==1){
return 1+ countOnlys(t.getLeft()) + countOnlys(t.getRight());
}
if(numberOfChildren(t)==2 ){
return countOnlys(t.getLeft()) + countOnlys(t.getRight());
}
return 0;
}
public static int numberOfChildren (TreeNode t){
int count = 0;
if(t.getLeft() != null ) count++;
if(t.getRight() != null) count++;
return count;
}
A parent has an only child if exactly one of the children is non-null (which implies that exactly one of its children is null):
((t.getLeft() == null || t.getRight() == null)) && !(t.getLeft() == null && t.getRight() == null)
You don't test, however, the node when you visit it as the recursive code traverses the tree. (This is similar to the Visitor pattern.) What you do is test for an only child when you are sitting on the parent. It's actually a logical exclusive-or test because one and only one of the children needs to be non-null to detect that this node has an only child.
So the algorithm is to
visit each node in the tree.
count it, if the node has only one child
That's it. The rest is plumbing.
public static int onlyChild(TreeNode t){
int res = 0;
if( t != null){
// ^ means XOR
if(t.getLeft() == null ^ t.getRight() == null){
res = 1;
}
res += onlyChild(t.getLeft()) + onlyChild(t.getRight()));
}
return res;
}
whenever you traverse a binary tree, think recursively. this should work.
public static int countOnlys(TreeNode t)
{
if(t == null)
return 0;
if (t.getLeft()==null&&t.getRight()==null)
return 1;
return countOnlys(t.getLeft())+countOnlys(t.getRight());
}
public int countNode(Node root) {
if(root == null)
return 0;
if(root.leftChild == null && root.rightChild == null)
return 0;
if(root.leftChild == null || root.rightChild == null)
return 1 + countNode(root.leftChild) + countNode(root.rightChild);
else
return countNode(root.leftChild) + countNode(root.rightChild);
}
I've been trying to write a recursive string method for a binary search tree that returns a multiple line representation of a tree with preorder path info.
Each node should be prefaced by a series of < and > characters showing the path that leads from the root to that node. I'm not sure how to use a string prefix parameter that is extended by one character with each successive call.
The method should be able reproduce this example:
Tree:
15
/ \
12 18
/ / \
10 16 20
\ \
11 17
Expected Print Output:
15
<12
<<10
<<>11
>18
><16
><>17
>>20
I'm new to recursion and so far my actual print output isn't close enough after hours of messing with the code:
18
<17
<10
>15
<11
>12
16
20
Here's my tree node class that works properly:
/**
* A single binary tree node.
* <p>
* Each node has both a left or right child, which can be null.
*/
public class TreeNode<E> {
private E data;
private TreeNode<E> left;
private TreeNode<E> right;
/**
* Constructs a new node with the given data and references to the
* given left and right nodes.
*/
public TreeNode(E data, TreeNode<E> left, TreeNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
}
/**
* Constructs a new node containing the given data.
* Its left and right references will be set to null.
*/
public TreeNode(E data) {
this(data, null, null);
}
/** Returns the item currently stored in this node. */
public E getData() {
return data;
}
/** Overwrites the item stored in this Node with the given data item. */
public void setData(E data) {
this.data = data;
}
/**
* Returns this Node's left child.
* If there is no left left, returns null.
*/
public TreeNode<E> getLeft() {
return left;
}
/** Causes this Node to point to the given left child Node. */
public void setLeft(TreeNode<E> left) {
this.left = left;
}
/**
* Returns this nodes right child.
* If there is no right child, returns null.
*/
public TreeNode<E> getRight() {
return right;
}
/** Causes this Node to point to the given right child Node. */
public void setRight(TreeNode<E> right) {
this.right = right;
}
}
Here's my binary search tree class with the toFullString() method near the bottom:
import java.util.*;
/**
* A binary search tree (BST) is a sorted ADT that uses a binary
* tree to keep all elements in sorted order. If the tree is
* balanced, performance is very good: O(n lg n) for most operations.
* If unbalanced, it performs more like a linked list: O(n).
*/
public class BinarySearchTree<E extends Comparable<E>> {
private TreeNode<E> root = null;
private int size = 0;
/** Creates an empty tree. */
public BinarySearchTree() {
}
public BinarySearchTree(Collection<E> col) {
List<E> list = new ArrayList<E>(col);
Collections.shuffle(list);
for (int i = 0; i < list.size() ; i++) {
add(list.get(i));
}
}
/** Adds the given item to this BST. */
public void add(E item) {
this.size++;
if (this.root == null) {
//tree is empty, so just add item
this.root = new TreeNode<E>(item);
}else {
//find where to insert, with pointer to parent node
TreeNode<E> parent = null;
TreeNode<E> curr = this.root;
boolean wentLeft = true;
while (curr != null) { //will execute at least once
parent = curr;
if (item.compareTo(curr.getData()) <= 0) {
curr = curr.getLeft();
wentLeft = true;
}else {
curr = curr.getRight();
wentLeft = false;
}
}
//now add new node on appropriate side of parent
curr = new TreeNode<E>(item);
if (wentLeft) {
parent.setLeft(curr);
}else {
parent.setRight(curr);
}
}
}
/** Returns the greatest (earliest right-most node) of the given tree. */
private E findMax(TreeNode<E> n) {
if (n == null) {
return null;
}else if (n.getRight() == null) {
//can't go right any more, so this is max value
return n.getData();
}else {
return findMax(n.getRight());
}
}
/**
* Returns item from tree that is equivalent (according to compareTo)
* to the given item. If item is not in tree, returns null.
*/
public E get(E item) {
return get(item, this.root);
}
/** Finds it in the subtree rooted at the given node. */
private E get(E item, TreeNode<E> node) {
if (node == null) {
return null;
}else if (item.compareTo(node.getData()) < 0) {
return get(item, node.getLeft());
}else if (item.compareTo(node.getData()) > 0) {
return get(item, node.getRight());
}else {
//found it!
return node.getData();
}
}
/**
* Removes the first equivalent item found in the tree.
* If item does not exist to be removed, throws IllegalArgumentException().
*/
public void remove(E item) {
this.root = remove(item, this.root);
}
private TreeNode<E> remove(E item, TreeNode<E> node) {
if (node == null) {
//didn't find item
throw new IllegalArgumentException(item + " not found in tree.");
}else if (item.compareTo(node.getData()) < 0) {
//go to left, saving resulting changes made to left tree
node.setLeft(remove(item, node.getLeft()));
return node;
}else if (item.compareTo(node.getData()) > 0) {
//go to right, saving any resulting changes
node.setRight(remove(item, node.getRight()));
return node;
}else {
//found node to be removed!
if (node.getLeft() == null && node.getRight() == null) {
//leaf node
return null;
}else if (node.getRight() == null) {
//has only a left child
return node.getLeft();
}else if (node.getLeft() == null) {
//has only a right child
return node.getRight();
}else {
//two children, so replace the contents of this node with max of left tree
E max = findMax(node.getLeft()); //get max value
node.setLeft(remove(max, node.getLeft())); //and remove its node from tree
node.setData(max);
return node;
}
}
}
/** Returns the number of elements currently in this BST. */
public int size() {
return this.size;
}
/**
* Returns a single-line representation of this BST's contents.
* Specifically, this is a comma-separated list of all elements in their
* natural Comparable ordering. The list is surrounded by [] characters.
*/
#Override
public String toString() {
return "[" + toString(this.root) + "]";
}
private String toString(TreeNode<E> n) {
//would have been simpler to use Iterator... but not implemented yet.
if (n == null) {
return "";
}else {
String str = "";
str += toString(n.getLeft());
if (!str.isEmpty()) {
str += ", ";
}
str += n.getData();
if (n.getRight() != null) {
str += ", ";
str += toString(n.getRight());
}
return str;
}
}
public String toFullString() {
StringBuilder sb = new StringBuilder();
toFullString(root, sb);
return sb.toString();
}
/**
* Preorder traversal of the tree that builds a string representation
* in the given StringBuilder.
* #param n root of subtree to be traversed
* #param sb StringBuilder in which to create a string representation
*/
private void toFullString(TreeNode<E> n, StringBuilder sb)
{
if (n == null)
{
return;
}
sb.append(n.getData().toString());
sb.append("\n");
if (n.getLeft() != null) {
sb.append("<");
} else if (n.getRight() != null) {
sb.append(">");
}
if (n.getLeft() != null || n.getRight() != null)
{
toFullString(n.getLeft(), sb);
toFullString(n.getRight(), sb);
}
}
/**
* Tests the BST.
*/
public static void main(String[] args) {
Collection collection = new ArrayList();
collection.add(15);
collection.add(12);
collection.add(18);
collection.add(10);
collection.add(16);
collection.add(20);
collection.add(11);
collection.add(17);
BinarySearchTree bst = new BinarySearchTree(collection);
//System.out.println(bst);
String temp = bst.toFullString();
System.out.println(temp);
}
}
Any help with the recursive toFullString method will be greatly appreciated.
There are two levels you need to think about when designing a recursive solution.
How do I deal with the current item in the recursion?
How do I go from this item to the next one, and what information gets passed on?
Since we are printing out each item in the tree, there is going to be a single print statement inside of each recursive call. However, the string printed in each row includes information about previous steps that were traversed to reach the current node. How do we deal with this information? It needs to be passed down from previous recursive calls to the next one.
Here is a set of possible rules:
If the current item is a leaf, print out the current result.
If the current item has a left node, add <, and recursively act on the left node.
If the current item has a right node, add >, and recursively act on the right node.
What do we add < and > to? We need a parameter to carry along the current previous steps that have occurred in the recursion from recursive call to call. You don't want to simply print out < and > as you traverse the tree, because you need to remember the current set of steps to print out the prefix to every single node. There could be a second helper method that takes the same parameters as the original toFullString(), but a custom third parameter to carry the current set of previous steps.
So the logic might look something like this:
If there is no current prefix, initialize prefix to "", since the root initially has no steps to reach it.
If the current node is a leaf, add a line of output with the current prefix + leaf value.
If the current node has a left node, recursively call toFullString() on the left node, and add < to the current prefix string, which is handed down.
If the current node has a right node, recursively call toFullString() on the right node, and add > to the current prefix string, which is handed down.
I am trying to implement a Linked List Sequence in Java however my method for inserting an object at the start of the sequence inserts the object twice and I don't understand why.
If anyone can help it would be really appreciated.
The current output of my test program is
30
30
but I want it to be 30
Thanks in advance
Below is the code for the sequence and the method for insertFirst
public class Sequence implements SequenceInterface{
private Node listHead;
private Node listTail;
protected class Node{
protected Object datum;
protected Node next;
public Node(Object o, Node n) {
datum = o;
next = n;
}
}
//Constructor
public Sequence(){
listHead = null;
listTail = null;
}
public void insertFirst(Object o) {
if(listHead == null){
listHead = new Node(o, listTail);
listTail = listHead;
}else{
Node oldLH = listHead;
listHead = new Node(o, oldLH);
}
}
}
Here is my test program
public class SequenceTest {
/**
* #param args
* #throws Exception
*/
public static void main(String[] args) throws Exception {
Sequence s = new Sequence();
s.insertFirst(new Integer(30));
for(int i=0; i<2; i++){
System.out.println(s.element(i));
}
}
}
It looks like it is only added once, but is being printed twice.
If you unroll the loop:
for(int i=0; i<2; i++){
System.out.println(s.element(i));
}
you essentially get:
System.out.println(s.element(0));
System.out.println(s.element(1));
so if this is unexpected behaviour, what we really need to look at to diagnose it is the element() method.
What do you want to happen when s.element(2) is called and 's' only contains one element? Should it throw an index out of range exception? Should it wrap or crop to within range when it is indexed past the lists range (presumably the current behaviour)?
Assuming you are trying to create a singly linked list, you've got this backwards:
listHead = new Node(o, listTail);
listTail = listHead;
When you execute that, you are assigning the new node to both listHead and listTail. You should have
listTail = listHead;
listHead = new Node(o, listTail);
public void insertFirst(Object o) {
Node newNode = new Node(o, listHead);
if (listHead == null) {
listHead = newNode;
listTail = listHead;
} else {
listHead = newNode;
}
}
This is the final version tested that works perfectly! Hope this helps:
class SequenceDLListException extends Exception {
SequenceDLListException() {
super();
}
SequenceDLListException(String s) {
super(s);
}
}
/**
* <dl>
* <dt>Purpose: Implementation of Sequence ADT.
* <dd>
*
* <dt>Description:
* <dd>This class is an implementation of the Sequence using an linked list as
* the underlying data structure. The capacity is therefore unlimited and
* overflow does not need to be checked.
* </dl>
*
*
* #version $Date: 2015/01/30
*/
public class SequenceDLList {
/**
* Member class Node encapsulates the nodes of the linked list in
* which the stack is stored. Each node contains a data item and a
* reference to another node - the next in the linked list.
*
*
*/
public Node lastNode() {
return listTail;
}
public Node firstNode() {
return listHead;
}
protected class Node {
public Node(Object o) {
this(o, null, null);
}
/**
* I added another null because it has to point at the beginning and at the end
* to some kind of terminator to facilitate the traversal of the list
*/
public Node(Object o, Node p, Node n) {
datum = o;
prev =p;
next = n;
}
/**
* I added another Node because every node has two links now with the Doubly Linked List
*/
//The Node data structure consists of two object references.
//One for the datum contained in the node and the other for
//the next node in the list.
protected Object datum;
protected Node next;
protected Node prev;
}
//We use object references to the head and tail of the list (the head
//and tail of the sequence, respectively).
private Node listHead;
private Node listTail;
//Only require a single constructor, which sets both object
//references to null.
/**
* Constructs an empty sequence object.
*/
public SequenceDLList() {
listHead = null;
listTail = null;
}
/**
* Adds a new item at the start of the sequence.
*/
public void insertFirst(Object o) {
//There is a special case when the sequence is empty.
//Then the both the head and tail pointers needs to be
//initialized to reference the new node.
if (listHead == null) {
listHead = new Node(o, listHead, null);
listTail = listHead;
}
//In the general case, we simply add a new node at the start
//of the list via the head pointer.
else {
listHead = new Node(o, listHead, null);
}
}
/**
* Adds a new item at the end of the sequence.
*/
public void insertLast(Object o) {
//There is a special case when the sequence is empty.
//Then the both the head and tail pointers needs to be
//initialized to reference the new node.
if (listHead == null) {
listHead = new Node(o, listHead, null);
listTail = listHead;
}
//In the general case, we simply add a new node to the end
//of the list via the tail pointer.
else {
listTail.next = new Node(o, listTail, null);
listTail = listTail.next;
}
}
/**
* Adds a new item at a specified position in the sequence.
*/
public void insert(Object o, int index) throws SequenceDLListException {
//Check the index is positive.
if (index < 0) {
throw new SequenceDLListException("Indexed Element out of Range");
}
//There is a special case when the sequence is empty.
//Then the both the head and tail pointers needs to be
//initialized to reference the new node.
if (listHead == null) {
if (index == 0) {
listHead = new Node(o, listHead, null);
listTail = listHead;
} else {
throw new SequenceDLListException("Indexed element is out of range");
}
}
//There is another special case for insertion at the head of
//the sequence.
else if (index == 0) {
listHead = new Node(o, listHead, null);
}
//In the general case, we need to chain down the linked list
//from the head until we find the location for the new
//list node. If we reach the end of the list before finding
//the specified location, we know that the given index was out
//of range and throw an exception.
else {
Node nodePointer = listHead;
int i = 1;
while (i < index) {
nodePointer = nodePointer.next;
i += 1;
if (nodePointer == null) {
throw new SequenceDLListException("Indexed Element out of Range");
}
}
Node newNode = new Node(o, nodePointer, nodePointer.next);
if (nodePointer.next != null) {
nodePointer.next.prev = newNode;
}
//Now we've found the node before the position of the
//new one, so we 'hook in' the new Node.
nodePointer.next = newNode;
//Finally we need to check that the tail pointer is
//correct. Another special case occurs if the new
//node was inserted at the end, in which case, we need
//to update the tail pointer.
if (nodePointer == listTail) {
listTail = newNode;
}
}
}
/**
* Removes the item at the start of the sequence.
*/
public void deleteFirst() throws SequenceDLListException {
//Check there is something in the sequence to delete.
if (listHead == null) {
throw new SequenceDLListException("Sequence Underflow");
}
//There is a special case when there is just one item in the
//sequence. Both pointers then need to be reset to null.
if (listHead.next == null) {
listHead = null;
listTail = null;
}
//In the general case, we just unlink the first node of the
//list.
else {
listHead = listHead.next;
listHead.prev = null;
}
}
/**
* Removes the item at the end of the sequence.
*/
public void deleteLast() throws SequenceDLListException {
//Check there is something in the sequence to delete.
if (listHead == null) {
throw new SequenceDLListException("Sequence Underflow");
}
//There is a special case when there is just one item in the
//sequence. Both pointers then need to be reset to null.
if (listHead.next == null) {
listHead = null;
listTail = null;
}
//In the general case, we need to chain all the way down the
//list in order to reset the link of the second to last
//element to null.
else {
Node nodePointer = listHead;
while (nodePointer.next != listTail) {
nodePointer = nodePointer.next;
}
//Unlink the last node and reset the tail pointer.
nodePointer.next = null;
listTail = nodePointer;
}
}
/**
* Removes the item at the specified position in the sequence.
*/
public void delete(int index) throws SequenceDLListException {
//Check there is something in the sequence to delete.
if (listHead == null) {
throw new SequenceDLListException("Sequence Underflow");
}
//Check the index is positive.
if (index < 0) {
throw new SequenceDLListException("Indexed Element out of Range");
}
//There is a special case when there is just one item in the
//sequence. Both pointers then need to be reset to null.
if (listHead.next == null) {
if (index == 0) {
listHead = null;
listTail = null;
} else {
throw new SequenceDLListException("Indexed element is out of range.");
}
}
//There is also a special case when the first element has to
//be removed.
else if (index == 0) {
deleteFirst();
}
//In the general case, we need to chain down the list to find
//the node in the indexed position.
else {
Node nodePointer = listHead;
int i = 1;
while (i < index) {
nodePointer = nodePointer.next;
i += 1;
if (nodePointer.next == null) {
throw new SequenceDLListException("Indexed Element out of Range");
}
}
//Unlink the node and reset the tail pointer if that
//node was the last one.
if (nodePointer.next == listTail) {
listTail = nodePointer;
}
//When we remove the node we have to change the reference as well!
if (nodePointer.next.next != null) {
nodePointer.next.next.prev = nodePointer;
}
nodePointer.next = nodePointer.next.next;
}
}
/**
* Returns the item at the start of the sequence.
*/
public Object first() throws SequenceDLListException {
if (listHead != null) {
return listHead.datum;
} else {
throw new SequenceDLListException("Indexed Element out of Range");
}
}
/**
* Returns the item at the end of the sequence.
*/
public Object last() throws SequenceDLListException {
if (listTail != null) {
return listTail.datum;
} else {
throw new SequenceDLListException("Indexed Element out of Range");
}
}
/**
* Returns the item at the specified position in the sequence.
*/
public Object element(int index) throws SequenceDLListException {
//Check the index is positive.
if (index < 0) {
throw new SequenceDLListException("Indexed Element out of Range");
}
//We need to chain down the list until we reach the indexed
//position
Node nodePointer = listHead;
int i = 0;
while (i < index) {
if (nodePointer.next == null) {
throw new SequenceDLListException("Indexed Element out of Range");
} else {
nodePointer = nodePointer.next;
i += 1;
}
}
return nodePointer.datum;
}
/**
* Tests whether there are any items in the sequence.
*/
public boolean empty() {
return (listHead == null);
}
/**
* Returns the number of items in the sequence.
*/
public int size() {
//Chain down the list counting the elements
Node nodePointer = listHead;
int size = 0;
while (nodePointer != null) {
size += 1;
nodePointer = nodePointer.next;
}
return size;
}
/**
* Empties the sequence.
*/
public void clear() {
listHead = null;
listTail = null;
}
}