Polynomial Addition using Linked Lists // Java - java

I have an assignment for my Data Structures class. In the assignment, we have to implement polynomial addition using linked lists. I think I have it down but eclipse is giving me a null pointer exception. My problem is with the add method, though I included the whole class for context. Multiply I will tackle after.. Please help.
class PolynomialLinkedList{
private static class PNode{
private int coe;
private int exp;
private PNode next;
public PNode(int c, int e){
this(c, e, null);
}
public PNode(int c, int e, PNode n){
coe = c;
exp = e;
next = n;
}
public void setCoe(int c){ coe = c;}
public void setExp(int e){ exp = e;}
public void setNext(PNode n){ next = n;}
public int getCoe(){ return coe;}
public int getExp(){ return exp;}
public PNode getNext(){ return next;}
}
private PNode first;
private PNode last;
public PolynomialLinkedList(){
first = last = null;
}
public PolynomialLinkedList(int c, int e){
PNode tempn = new PNode(c, e);
first = last = tempn;
}
public void print(){
if (first == null){
System.out.println();
return;
}
PNode temp = first;
String ans = "";
while (temp != null){
if (temp.getCoe() > 0) {
if (temp != first) ans = ans + " + ";
ans = ans + temp.getCoe();
}
else if (temp.getCoe() < 0) ans = ans + " - " + temp.getCoe() * -1;
if (temp.getExp() != 0){
ans = ans + "X^" + temp.getExp();
}
temp = temp.getNext();
}
System.out.println(ans);
}
public PolynomialLinkedList add(PolynomialLinkedList s){
PolynomialLinkedList sum = new PolynomialLinkedList();
PNode temp1 = this.first;
PNode temp2 = s.first;
PNode tempAns = new PNode(0,0);
if(temp1.exp != temp2.exp) {
while(temp1.exp > temp2.exp) {
tempAns.setCoe(temp1.coe);
tempAns.setExp(temp1.exp);
temp1 = temp1.getNext();
tempAns = sum.first.getNext();
}
while(temp1.exp < temp2.exp) {
tempAns.setCoe(temp2.coe);
tempAns.setExp(temp2.exp);
temp2 = temp2.getNext();
tempAns = sum.first.getNext();
}
}
else if(temp1.exp == temp2.exp) {
while(temp1.exp == temp2.exp) {
tempAns.setCoe((temp1.coe + temp2.coe));
tempAns.setExp(temp1.exp);
temp1 = temp1.getNext();
temp2 = temp2.getNext();
tempAns = sum.first.getNext();
}
}
return sum;
}
public PolynomialLinkedList multiply(PolynomialLinkedList s){
PolynomialLinkedList product = new PolynomialLinkedList();
//implement this method
return product;
}
}

Related

How to implement an AVL tree without storing height inside the node?

I am currently learning binary search tree. For a school assignment, I want to implement an self-balancing binary search tree (I choose AVL tree); however, the Node class cannot be modified. I cannot use the common implementation of the AVL tree (I can't store height inside the node).
This is the source code that I implemented (using HashMap).
public class Tree {
static class Student {
String id;
String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
public String toString() {return id + ", " + name;}
}
private class Node {
Student e;
public Node lc, rc; // left child; right child
#SuppressWarnings("unused")
public Node(Student data) {
this.e = data;
}
public String toString() {
return e.toString();
}
}
Node root;
public HashMap<Node, Integer> map = new HashMap<>();
public void insert(Student s) {
root = insert(root, s);
}
public Node insert(Node curNode, Student s){
if (curNode == null){
Node newNode = new Node(s);
map.put(newNode, 1);
return newNode;
}
else if (s.name.compareTo(curNode.e.name) < 0)
curNode.lc = insert(curNode.lc, s);
else if (s.name.compareTo(curNode.e.name) > 0)
curNode.rc = insert(curNode.rc, s);
else return curNode;
int l, r;
map.put(curNode, max(nheight(curNode.rc),
nheight(curNode.lc)) + 1);
int balance = getBalance(curNode);
if (balance > 1 && s.name.compareTo(curNode.e.name) < 0)
return rightRotate(curNode);
if (balance < -1 && s.name.compareTo(curNode.e.name) > 0)
return leftRotate(curNode);
if(balance > 1 && s.name.compareTo(curNode.e.name) > 0){
curNode.lc = leftRotate(curNode.lc);
return rightRotate(curNode);
}
if(balance < -1 && s.name.compareTo(curNode.e.name) < 0){
curNode.rc = rightRotate(curNode.rc);
return leftRotate(curNode);
}
return curNode;
}
public int max(int a, int b){
return a > b ? a : b;
}
public int nheight(Node curRoot){
if (curRoot == null) return 0;
return map.get(curRoot);
}
public int getBalance(Node curNode){
if (curNode == null) return 0;
return nheight(curNode.lc) - nheight(curNode.rc);
}
public Node rightRotate(Node y){
Node x = y.lc;
Node T2 = x.rc;
x.rc = y;
y.lc = T2;
map.put(y, max(nheight(y.lc), nheight(y.rc)) + 1);
map.put(x, max(nheight(x.lc), nheight(x.rc)) + 1);
return x;
}
public Node leftRotate(Node x){
Node y = x.rc;
Node T2 = y.lc;
y.lc = x;
x.rc = T2;
map.put(x, max(nheight(x.lc), nheight(x.rc)) + 1);
map.put(y, max(nheight(y.lc), nheight(y.rc)) + 1);
return y;
}
}
I tried using a HashMap<Node, Integer> to store the height of each node, and using recursive method to calculate the height and balance factor every time. For small number of nodes, the above two methods would work; however, for large node size (>=1000000) those methods will not work. Is there any other data structures I can use the keep track of the height of each node?
This is the code I used to test. I randomly created 1000000 students with names and ids and insert them into the AVL tree.
public class Main {
public static void main(String[] args) {
Tree tree = new Tree();
String[] surnames = {"Chan", "Leung", "Li", "Lai", "Cheung", "Yeung", "Tang", "Chow", "Fung", "Tsang", "Kwok", "Chu", "Liu", "Wong", "Mak"};
SecureRandom random = new SecureRandom();
String[] names = new String[1000000];
for (int j = 0; j < names.length; j++) {
StringBuilder a = new StringBuilder();
for(int i = 0; i < 5; i ++) {
a.append((char)('a' + random.nextInt(25)));
}
names[j] = surnames[random.nextInt(surnames.length)] + " " + a.toString();
}
int id = 22222222;
for (String name : names) {
id += random.nextInt(100);
tree.insert(new Tree.Student(String.valueOf(id), name));
}
}
}
The code works fine when the size of the String is 1000 (String[1000]). It gets error when the size of String gets larger. For example, when it gets to 1000000. The error code is below.
Exception in thread "main" java.lang.NullPointerException: Cannot read field "lc" because "y" is null
Problem found. A careless mistake. I was focusing too much on the size of the input. The actual problem is with the insertion and rotations.
The amended code.
public Node insert(Node curNode, Student s){
if (curNode == null){
Node newNode = new Node(s);
map.put(newNode, 1);
return newNode;
}
else if (s.name.compareTo(curNode.e.name) < 0)
curNode.lc = insert(curNode.lc, s);
else if (s.name.compareTo(curNode.e.name) > 0)
curNode.rc = insert(curNode.rc, s);
else return curNode;
int l, r;
map.put(curNode, max(nheight(curNode.rc), nheight(curNode.lc)) + 1);
int balance = getBalance(curNode);
if (balance > 1 && s.name.compareTo(curNode.lc.e.name) < 0)
return rightRotate(curNode);
if (balance < -1 && s.name.compareTo(curNode.rc.e.name) > 0)
return leftRotate(curNode);
if(balance > 1 && s.name.compareTo(curNode.lc.e.name) > 0){
curNode.lc = leftRotate(curNode.lc);
return rightRotate(curNode);
}
if(balance < -1 && s.name.compareTo(curNode.rc.e.name) < 0){
curNode.rc = rightRotate(curNode.rc);
return leftRotate(curNode);
}
return curNode;
}

Wacky Linked List behavior Java Eclipse Mars.1

The full code is a 2-3 tree implementation that requires me to be able to search for a book by any key(ISSN, Author[0], Author[1], Author[0] + "," + Author[1], or Title) The tree works by creating 5 copies of each book, each designated with one of the keys previously mentioned, and inserting them into their respective leaves. If a new book being inserted matches a key that has already been inserted into the tree (ie., James Henry, and James Joyce both would have a copy of their books inserted into the tree with key "james"), then instead of that new book being added into a leaf node at the bottom of the tree, it is added into a linked list inside of the node that already contains a copy all books with key == "james".
Class Tree23:
package tree23;
import bookClasses.*;
/**
* #author Chris
* #usage This Class builds a 2-3 tree that sorts Book entries based on ISSN value
*/
public class Tree23 {
public Node23 Root;
// PUBLIC FUNCTIONS //
public Tree23(){
Root = new Node23(true);
}
public void Insert(int issn, String title, String author){
String issnKey = null;
String titleKey = null;
String authorKey = null;
String author0Key = null;
String author1Key = null;
issnKey = String.valueOf(issn);
titleKey = title.toLowerCase();
authorKey = author.toLowerCase();
if(author.split(",").length == 2){
author0Key = author.split(",")[0].toLowerCase();
author1Key = author.split(",")[1].toLowerCase();
}
Book[] newBooks = new Book[5];
for(int i = 0; i < 5; i++){
newBooks[i] = new Book(issn, title, author);
}
InsertByKey(newBooks[0], issnKey);
InsertByKey(newBooks[1], titleKey);
InsertByKey(newBooks[2], authorKey);
if(author.split(",").length == 2){
InsertByKey(newBooks[3], author0Key);
InsertByKey(newBooks[4], author1Key);
}
}
public BookList GetBooksByAttribute(char attribute, String key){
BookList matches = null;
switch(attribute){
case 'i':
matches = GetBooksByISSN(Integer.valueOf(key));
break;
case 'n':
matches = GetBooksByAuthor(key.toLowerCase());
break;
case 't':
matches = GetBooksByTitle(key.toLowerCase());
break;
default:
break;
}
return matches;
}
// PRIVATE FUNCTIONS //
/*Insert Functions*/
private void InsertByKey(Book newBook, String key){
if(key.compareTo("james") == 0){
if(newBook.Title.toLowerCase().compareTo("finnegans wake") == 0){
System.out.print("");
}
}
//check for a node with matching key
Node23 targetNode = GetNodeByKey(Root, key);
//else find leaf for insert
if(targetNode == null){
targetNode = GetLeafByKey(Root, key);
}
//insert into target node
targetNode.AddBook(newBook, key);
if(key.compareTo("james") == 0){
System.out.print("");
}
if(targetNode.GetSize() == 3){
BalanceNodeOverflow(targetNode);
}
}
/*Search Functions*/
private BookList GetBooksByKey(Node23 n, String key){
BookList bList = null;
int match = n.GetMatchingBookListByKey(key);
if(match != -1){
bList = n.Books[match];
}
else{
int nextChild = DetermineNextTraversal(n, key);
bList = GetBooksByKey(n.Children[nextChild], key);
}
return bList;
}
private Node23 GetSuccessorLeaf(Node23 n, int index){
Node23 leaf = n.Children[index + 1];
while(!leaf.IsLeaf()){
leaf = leaf.Children[0];
}
return leaf;
}
private BookList GetBooksByISSN(int issn){
BookList keyMatches = GetBooksByKey(Root, String.valueOf(issn));
BookList issnMatches = null;
if(keyMatches != null){
issnMatches = keyMatches.GetBooksByISSN(issn);
}
return issnMatches;
}
private BookList GetBooksByAuthor(String author){
BookList keyMatches = GetBooksByKey(Root, String.valueOf(author));
BookList authorMatches = null;
if(keyMatches != null){
authorMatches = keyMatches.GetBooksByAuthor(author);
}
return authorMatches;
}
private BookList GetBooksByTitle(String title){
BookList keyMatches = GetBooksByKey(Root, String.valueOf(title));
BookList titleMatches = null;
if(keyMatches != null){
titleMatches = keyMatches.GetBooksByTitle(title);
}
return titleMatches;
}
private Node23 GetLeafByKey(Node23 n, String key){
//assumes that there is no node matching key, traverses from root to leaf node by key
//return key
Node23 leaf = n;
if(!n.IsLeaf()){
int nextChild = n.GetNextChildByKey(key);
leaf = GetLeafByKey(n.Children[nextChild], key);
}
return leaf;
}
private Node23 GetNodeByKey(Node23 n, String key){
Node23 match = null;
int nextChild;
int matchingBookList = n.GetMatchingBookListByKey(key);
if(matchingBookList >= 0){
match = n;
}
else{
nextChild = n.GetNextChildByKey(key);
if(nextChild != -1){
match = GetNodeByKey(n.Children[nextChild], key);
}
}
return match;
}
private int DetermineNextTraversal(Node23 n, String key){
int nextChild;
if(n.IsLeaf()){
nextChild = -1;
}
else if(n.GetSize() == 0){
nextChild = -1;
}
else if(n.GetSize() == 1){
if(n.Books[0].CompareTo(key) > 0){
nextChild = 0;
}
else{
nextChild = 1;
}
}
else{
if(n.Books[0].CompareTo(key) > 0){
nextChild = 0;
}
else if(n.Books[1].CompareTo(key) > 0){
nextChild = 1;
}
else{
nextChild = 2;
}
}
return nextChild;
}
/*Delete Functions*/
private void DeleteBookFromTree(Book b){
Node23 match;
int originalSize;
/*findNodeMatchingISSN*/
match = GetNodeByKey(Root, String.valueOf(b.ISSN));
originalSize = match.GetSize();
match.RemoveMatchingBook(b, String.valueOf(b.ISSN));
if(originalSize > match.GetSize()){
BalanceNodeUnderflow(match);
}
/*author[0]*/
match = GetNodeByKey(Root, b.Author[0]);
originalSize = match.GetSize();
match.RemoveMatchingBook(b, b.Author[0]);
if(originalSize > match.GetSize()){
BalanceNodeUnderflow(match);
}
if(b.Author.length == 2){
/*author[1]*/
match = GetNodeByKey(Root, b.Author[1]);
originalSize = match.GetSize();
match.RemoveMatchingBook(b, b.Author[1]);
if(originalSize > match.GetSize()){
BalanceNodeUnderflow(match);
}
/*author[0],author[1]*/
match = GetNodeByKey(Root, new String(b.Author[1] + "," + b.Author[2]));
originalSize = match.GetSize();
match.RemoveMatchingBook(b, new String(b.Author[1] + "," + b.Author[2]));
if(originalSize > match.GetSize()){
BalanceNodeUnderflow(match);
}
}
/*title*/
match = GetNodeByKey(Root, b.Title);
originalSize = match.GetSize();
match.RemoveMatchingBook(b, b.Author[1]);
if(originalSize > match.GetSize()){
BalanceNodeUnderflow(match);
}
}
/*Tree Balancing Functions*/
private void BalanceNodeOverflow(Node23 fullNode){
Node23[] splitNodes;
if(fullNode == Root){
Node23 newRoot = new Node23(false);
fullNode.Parent = newRoot;
splitNodes = fullNode.SplitNode();
fullNode.Parent.AddBookList(fullNode.RemoveBookList(0));;
newRoot.AddSplitNodeChildAt(0, splitNodes);
Root = newRoot;
}
else{
int position = fullNode.WhichChildAmI();
splitNodes = fullNode.SplitNode();
fullNode.Parent.AddBookList(fullNode.RemoveBookList(0));
fullNode.Parent.AddSplitNodeChildAt(position, splitNodes);
if(fullNode.Parent.GetSize() == 3){
BalanceNodeOverflow(fullNode.Parent);
}
}
}
private void RotateLeft1To0(Node23 parent){
Node23[] children = parent.Children;
children[0].AddBookList(parent.RemoveBookList(0));
parent.AddBookList(children[1].RemoveBookList(0));
children[1].ShiftBooksLeftInto(0);
}
private void RotateLeft2To1(Node23 parent){
Node23[] children = parent.Children;
children[1].AddBookList(parent.RemoveBookList(1));
parent.AddBookList(children[2].RemoveBookList(0));
children[2].ShiftBooksLeftInto(0);
}
private void RotateRight0To1(Node23 parent){
Node23[] children = parent.Children;
children[1].AddBookList(parent.RemoveLeftMostBookList());
parent.AddBookList(children[0].RemoveRightMostBookList());
}
private void RotateRight1To2(Node23 parent){
Node23[] children = parent.Children;
children[2].AddBookList(parent.RemoveRightMostBookList());
parent.AddBookList(children[1].RemoveRightMostBookList());
}
private void BalanceNodeUnderflow(Node23 n){
Node23 leaf;
int emptyPosition = n.FirstNullBookList();
if(!n.IsLeaf()){
leaf = GetSuccessorLeaf(n, emptyPosition);
n.AddBookList(leaf.RemoveBookList(0));
}
else{
leaf = n;
}
if(leaf.GetSize() == 0){
FillHole(leaf);
}
}
private void RedistributeParent3(Node23 hole, int holePosition){
Node23 parent = hole.Parent;
Node23[] siblings = parent.Children;
switch(holePosition){
case 0:
if(siblings[1].GetSize() == 2){
RotateLeft1To0(parent);
if(!hole.IsLeaf()){
hole.AddChildAt(1, siblings[1].RemoveLeftMostChild());
siblings[1].ShiftChildrenLeftInto(0);
}
}
else if(siblings[2].GetSize() == 2){
RotateLeft1To0(parent);
RotateLeft2To1(parent);
if(!hole.IsLeaf()){
hole.AddChildAt(1, siblings[1].RemoveLeftMostChild());
siblings[1].ShiftChildrenLeftInto(0);
siblings[1].AddChildAt(1, siblings[2].RemoveLeftMostChild());
siblings[2].ShiftChildrenLeftInto(0);
}
}
else{
RotateLeft1To0(parent);
hole.AddBookList(parent.RemoveBookList(0));
parent.ShiftBooksLeftInto(0);
siblings[1].AddBookList(siblings[2].RemoveBookList(0));
if(!hole.IsLeaf()){
hole.AddChildAt(1, siblings[1].RemoveLeftMostChild());
siblings[1].ShiftChildrenLeftInto(0);
hole.AddChildAt(2, siblings[1].RemoveLeftMostChild());
siblings[1].AddChildAt(0, siblings[2].RemoveLeftMostChild());
siblings[1].AddChildAt(1, siblings[2].RemoveLeftMostChild());
}
parent.RemoveRightMostChild();
}
break;
case 1:
if(siblings[0].GetSize() == 2){
RotateRight0To1(parent);
if(!hole.IsLeaf()){
siblings[1].ShiftChildrenRightInto(1);
siblings[1].AddChildAt(0, siblings[0].RemoveRightMostChild());
}
}
else if(siblings[2].GetSize() == 2){
RotateLeft2To1(parent);
if(!hole.IsLeaf()){
siblings[1].AddChildAt(1, siblings[2].RemoveLeftMostChild());
siblings[2].ShiftBooksLeftInto(0);
}
}
else{
RotateLeft2To1(parent);
siblings[1].ShiftBooksLeftInto(0);
RotateLeft1To0(parent);
parent.ShiftBooksLeftInto(0);
if(!hole.IsLeaf()){
siblings[0].AddChildAt(2, siblings[1].RemoveLeftMostChild());
siblings[1].AddChildAt(0, siblings[2].RemoveLeftMostChild());
siblings[1].AddChildAt(1, siblings[2].RemoveRightMostChild());
}
parent.RemoveRightMostChild();
}
break;
case 2:
if(siblings[0].GetSize() == 2){
RotateRight1To2(parent);
RotateRight0To1(parent);
if(!hole.IsLeaf()){
siblings[2].ShiftChildrenRightInto(1);
siblings[2].AddChildAt(0, siblings[1].RemoveRightMostChild());
siblings[1].ShiftBooksRightInto(1);
siblings[1].AddChildAt(0, siblings[0].RemoveRightMostChild());
}
}
else if(siblings[1].GetSize() == 2){
RotateRight1To2(parent);
if(!hole.IsLeaf()){
siblings[2].ShiftChildrenRightInto(1);
siblings[2].AddChildAt(0, siblings[1].RemoveRightMostChild());
}
}
else{
RotateLeft1To0(parent);
siblings[1].AddBookList(parent.RemoveRightMostBookList());
if(!hole.IsLeaf()){
siblings[0].AddChildAt(2, siblings[1].RemoveLeftMostChild());
siblings[1].ShiftChildrenLeftInto(0);
siblings[1].AddChildAt(1, siblings[2].RemoveLeftMostChild());
}
parent.RemoveRightMostChild();
}
break;
default:
break;
}
}
private void RedistributeParent2(Node23 hole, int holePosition){
Node23 parent = hole.Parent;
Node23[] siblings = parent.Children;
switch(holePosition){
case 0:
hole.AddBookList(parent.RemoveBookList(0));
parent.AddBookList(siblings[1].RemoveBookList(0));
siblings[1].ShiftBooksLeftInto(0);
break;
case 1:
hole.AddBookList(parent.RemoveBookList(0));
parent.AddBookList(siblings[0].RemoveBookList(1));
break;
default:
break;
}
}
private void Merge(Node23 hole, int holePosition){
Node23 parent = hole.Parent;
Node23[] sibling = parent.Children;
if(holePosition == 0){
sibling[0].AddBookList(sibling[1].RemoveBookList(0));
}
sibling[0].AddBookList(parent.RemoveBookList(0));
if(!hole.IsLeaf()){
sibling[0].AddChildAt(1, sibling[1].RemoveLeftMostChild());
sibling[0].AddChildAt(2, sibling[1].RemoveRightMostChild());
}
parent.RemoveRightMostChild();
if(parent.GetSize() == 0){
BalanceNodeUnderflow(parent);
}
}
private void FillHole(Node23 hole){
Node23[] siblings;
Node23 parent;
int holePosition;
if(hole == Root){
Root = hole.Children[0];
}else{
parent = hole.Parent;
siblings = parent.Children;
holePosition = hole.WhichChildAmI();
if(parent.GetSize() == 2){
RedistributeParent3(hole, holePosition);
}
else{
if(siblings[0].GetSize() == 2 || siblings[1].GetSize() == 2){
RedistributeParent2(hole, holePosition);
}else{
Merge(hole, holePosition);
}
}
}
}
}
package tree23;
import bookClasses.*;
/**
* #Node
*This Node is designed for a 2-3 Tree.
*The Node holds two Book objects and points to three children nodes of the same type.
*The node is also designed to hold an overflow Book and overflow child, until the node is split.
*/
public class Node23 {
public BookList[] Books;
private boolean isLeaf;
private int size;
public Node23 Parent;
public Node23[] Children;
//Constructors
public Node23(boolean isleaf){
Books = new BookList[3];
Children = new Node23[4];
isLeaf = isleaf;
size = 0;
}
public Node23[] SplitNode(){
Node23[] splitNodes = new Node23[2];
splitNodes[0] = new Node23(isLeaf);
splitNodes[1] = new Node23(isLeaf);
splitNodes[0].Parent = Parent;
splitNodes[1].Parent = Parent;
splitNodes[0].AddBookList(RemoveLeftMostBookList());
splitNodes[1].AddBookList(RemoveRightMostBookList());
if(!isLeaf){
Children[0].Parent = splitNodes[0];
Children[1].Parent = splitNodes[0];
splitNodes[0].Children[0] = Children[0];
splitNodes[0].Children[1] = Children[1];
Children[2].Parent = splitNodes[1];
Children[3].Parent = splitNodes[1];
splitNodes[1].Children[0] = Children[2];
splitNodes[1].Children[1] = Children[3];
}
return splitNodes;
}
//Book Functions
public int FirstNullBookList(){
int emptyPos = -1;
for(int i = 0; i < 2; i++){
if(Books[i] == null){
emptyPos = -1;
}
}
return emptyPos;
}
public int GetNextChildByKey(String key){
int nextChild = -1;
if(!isLeaf){
if(size == 1){
if(Books[0].CompareTo(key) > 0){
nextChild = 0;
}
else{
nextChild = 1;
}
}
else{
if(Books[0].CompareTo(key) > 0){
nextChild = 0;
}
else if(Books[1].CompareTo(key) > 0){
nextChild = 1;
}
else{
nextChild = 2;
}
}
}
return nextChild;
}
public int GetMatchingBookListByKey(String key){
int match = -1;
for(int i = 0; i < size; i++){
if(Books[i].CompareTo(key) == 0)
match = i;
}
return match;
}
public void TakeAllBookLists(Node23 n){
for(int i = 0; i < 3; i++){
if(n.Books[i] != null){
AddBookList(n.RemoveBookList(i));
}
}
}
public void ShiftBooksRightFrom(int shiftStart){
for(int i = 2; i > shiftStart; i--){
Books[i] = Books[i - 1];
}
Books[shiftStart] = null;
}
public void ShiftBooksRightInto(int shiftEnd){
for(int i = shiftEnd; i > 0; i--){
Books[i] = Books[i - 1];
}
Books[0] = null;
}
public void ShiftBooksLeftFrom(int shiftStart){
for(int i = 0; i < shiftStart; i++){
Books[i] = Books[i+1];
}
Books[shiftStart] = null;
}
public void ShiftBooksLeftInto(int shiftEnd){
for(int i = shiftEnd; i < 2; i++){
Books[i] = Books[i + 1];
}
Books[2] = null;
}
public void AddBookList(BookList newBookList){
if(size == 0){
Books[0] = newBookList;
}
else{
if(newBookList.CompareTo(Books[0]) < 0){
ShiftBooksRightFrom(0);
Books[0] = newBookList;
}
else{
if(size == 1){
Books[1] = newBookList;
}
else{
if(newBookList.CompareTo(Books[1]) < 0){
ShiftBooksRightFrom(1);
Books[1] = newBookList;
}
else{
Books[2] = newBookList;
}
}
}
}
size++;
}
public void AddBook(Book newBook, String key){
if(size == 0){
AddBookList(new BookList(newBook, key));
}
else{
int match = GetMatchingBookListByKey(key);
if(match != -1){
Books[match].AddBook(newBook);
}
else{
AddBookList(new BookList(newBook, key));
}
}
}
public BookList RemoveRightMostBookList(){
BookList bList = null;
for(int i = 2; i > 0; i--){
if(Books[i] != null){
bList = Books[i];
Books[i] = null;
size--;
break;
}
}
return bList;
}
public BookList RemoveLeftMostBookList(){
BookList bList = null;
bList = Books[0];
ShiftBooksLeftInto(0);
size--;
return bList;
}
public BookList RemoveBookList(int position){
BookList bList = Books[position];
ShiftBooksLeftInto(position);
size--;
return bList;
}
public void RemoveMatchingBook(Book b, String key){
if(Books[0] != null){
if(Books[0].CompareTo(key) == 0){
Books[0].DeleteBooksByISSN(b.ISSN);
}
}
else if(Books[1] != null){
if(Books[1].CompareTo(key) == 0){
Books[1].DeleteBooksByISSN(b.ISSN);
}
}
else if(Books[2] != null){
if(Books[2].CompareTo(key) == 0){
Books[2].DeleteBooksByISSN(b.ISSN);
}
}
}
//Child-Node Functions
public Node23 RemoveLeftMostChild(){
Node23 n = null;
int i = 0;
while(n == null && i <= 3){
if(Children[i] != null){
n = Children[i];
Children[i] = null;
}
i++;
}
return n;
}
public Node23 RemoveRightMostChild(){
Node23 n = null;
int i = 3;
while(n == null && i >= 0 ){
if(Children[i] != null){
n = Children[i];
Children[i] = null;
}
i--;
}
return n;
}
public void AddChildAt(int index, Node23 newChildNode){
Children[index] = newChildNode;
}
public void AddSplitNodeChildAt(int position, Node23[] splitNode){
ShiftChildrenRightFrom(position + 1);
AddChildAt(position, splitNode[0]);
AddChildAt(position + 1, splitNode[1]);
}
public void ShiftChildrenRightFrom(int shiftStart){
for(int i = 3; i > shiftStart; i--){
Children[i] = Children[i - 1];
}
Children[shiftStart] = null;
}
public void ShiftChildrenRightInto(int shiftEnd){
for(int i = shiftEnd; i > 0; i--){
Children[i] = Children[i - 1];
}
Children[0] = null;
}
public void ShiftChildrenLeftFrom(int shiftStart){
for(int i = 0; i < shiftStart; i++){
Children[i] = Children[i + 1];
}
Children[shiftStart] = null;
}
public void ShiftChildrenLeftInto(int shiftEnd){
for(int i = shiftEnd; i < 2; i++){
Children[i] = Children[i + 1];
}
Children[2] = null;
}
//Attribute Functions
public int CountChildren(){
int count = 0;
for(int i = 0; i < 3; i++){
if(Children[i] != null){
count++;
}
}
return count;
}
public boolean IsRoot(){
boolean isRoot = false;
if(Parent == null){
isRoot = true;
}
return isRoot;
}
public int WhichChildAmI(){
int childNum = -1;
if(!IsRoot()){
for(int i = 0; i < Parent.size + 1; i++){
if(Parent.Children[i] == this){
childNum = i;
}
}
}
return childNum;
}
public boolean IsLeaf(){
return isLeaf;
}
public int GetSize(){
return size;
}
}
Class Book: This is the Book class that makes the links of the linked list within each Node23
package bookClasses;
public class Book{
public int ISSN;
public String Title;
public String[] Author;
public Book Next;
public Book(){}
/**
* #param ISSN
* #param Title
* #param Author
*/
public Book(int issn, String title, String author){
ISSN = issn;
Title = title;
Author = author.split(",");
Next = null;
}
private String AuthorToString(){
String author = Author[0];
if(Author.length == 2){
author = author + ", " + Author[1];
}
return author;
}
public void Display(){
System.out.println("\tISSN: " + ISSN);
System.out.println("\tTITLE: " + Title);
System.out.println("\tAUTHOR: " + AuthorToString());
}
public boolean MatchAuthor(String author){
boolean match = false;
if(Author.length == 2){
if(author.compareTo(Author[0].toLowerCase()) == 0
|| author.compareTo(Author[1].toLowerCase()) == 0){
match = true;
}
}
if(author.split(",") == Author){
match = true;
}
return match;
}
}
Class BookList: This is the linked List inside of each node containing copies all books with a matching key
package bookClasses;
public class BookList {
public String Key;
public Book Head;
private int Size;
public BookList(){
Head = null;
Size = 0;
};
public BookList(Book b, String key){
Size = 1;
Key = key;
Head = b;
}
//Insert Functions
public void AddBook(Book newBook){
newBook.Next = Head;
Head = newBook;
Size++;
}
//Search Functions
public BookList GetBooksByISSN(int issn){
BookList books = new BookList();
Book b = Head;
for(int i = 0; i < Size; i++){
if(issn == b.ISSN){
books.AddBook(b);
}
}
return books;
}
public BookList GetBooksByTitle(String title){
BookList matches = new BookList();
Book b = Head;
for(int i = 0; i < Size; i++){
if(title.compareTo(b.Title.toLowerCase()) == 0){
matches.AddBook(b);
}
}
return matches;
}
public BookList GetBooksByAuthor(String author){
BookList books = new BookList();
Book b = Head;
for(int i = 0; i < Size; i++){
if(b.MatchAuthor(author)){
books.AddBook(b);
}
b = b.Next;
}
return books;
}
//Delete Functions
public void DeleteBooksByAuthor(String author){
Book b = Head;
if(b.MatchAuthor(author)){
Head = b.Next;
Size--;
}
else{
while(b.Next != null){
if(b.Next.MatchAuthor(author)){
b.Next = b.Next.Next;
Size--;
}
else{
b = b.Next;
}
}
}
}
public void DeleteBooksByTitle(String title){
Book b = Head;
if(b.Title.toLowerCase().compareTo(title.toLowerCase()) == 0){
Head = b.Next;
Size--;
}
else{
while(b.Next != null){
if(b.Next.Title.toLowerCase().compareTo(title.toLowerCase()) == 0){
b.Next = b.Next.Next;
Size--;
}
else{
b = b.Next;
}
}
}
}
public void DeleteBooksByISSN(int issn){
Book b = Head;
if(b.ISSN == issn){
Head = b.Next;
Size--;
}
else{
while(b.Next != null){
if(b.Next.ISSN == issn){
b.Next = b.Next.Next;
Size--;
}
else{
b = b.Next;
}
}
}
}
//Other Methods
public void Displayall(){
Book b = Head;
for(int i = 0; i < Size; i++){
b.Display();
b = b.Next;
}
}
public int CompareTo(BookList bookList){
return (Key.compareTo(bookList.Key));
}
public int CompareTo(String key){
return (Key.compareTo(key));
}
public int Size(){
return Size;
}
}
Heres the input file - the 1 specifies INSERT
1
60
The Black Dahlia
James,Ellroy
1
98
The Turn of the Screw
Henry,James
1
147
The Wings of the Dove
Henry,James
1
151
A Portrait of the Artist as a Young Man
James,Joyce
1
157
The Postman Always Rings Twice
James,M.
1
184
How Late It Was, How Late
James,Kelman
1
186
A Disaffection
James,Kelman
1
320
The Ambassadors
Henry,James
1
326
What Maisie Knew
Henry,James
1
327
The Golden Bowl The Golden Bowl
Henry,James
1
353
The Charwoman's Daughter
James,Stephens
1
362
Ulysses
James,Joyce
1
478
Finnegans Wake
James,Joyce
After inserting ulysses
The Black Dahlia
The Turn of the Screw
The Wings of the Dove
A Portrait of the Artist as a Young Man
The Postman Always Rings Twice
How Late It Was, How Late
A Disaffection
The Ambassadors
What Maisie Knew
The Golden Bowl The Golden Bowl
The Charwoman's Daughter
Ulysses <==Head
Now here's where it gets wacky:
if I try and insert this entry:
478
Finnegans Wake
James,Joyce
bringing the size of the list from 12, to 13,
the new order of the list becomes:
The Black Dahlia
The Turn of the Screw
The Wings of the Dove
A Portrait of the Artist as a Young Man
The Postman Always Rings Twice
How Late It Was, How Late
A Disaffection
The Ambassadors
What Maisie Knew
The Golden Bowl The Golden Bowl
Ulysses
Finnegans Wake <=======HEAD
The Charwomans daughter has dissappeared.
This change happens when Head = newBook;
So, I thought it might have something to do with my hardware, and the computer I originally had this project stored on uses an AMD processor, so I got on my intel laptop and accessed the AMD desktop over the network. I copied the project file into my workspace on the laptop - Just a simple drag and drop.
First I run the code on the intel laptop,, and as expected, it doesnt work, then I run the code on the desktop again, and NOW IT WORKS. No changes were made.
After the code worked on the desktop, it suddenly worked on the laptop.

Arrival time cannot be sorted correctly in Java

Greeting to everyone. I currently work on a program that sorting the emergency number of patients(the number that assigned by nurse when they enter the emergency room and this number determines the seriousness of their sickness too). However, if there are more than 1 patient who hold the same emergency numbers(eg: 2 patients hold emergency number 1), the one who came earlier should receive the treatment first. For this reason, I have 2 sortings, one is to sort the emergency number in ascending order and the other is to sort the time in ascending order too. But unfortunately the second sorting cannot work correctly.The following are the explanations for the type of emergency numbers:
Emergency number : 1 – Immediately life threatening
Emergency number : 2 – Urgent, but not immediately life threatening
Emergency number : 3 – Less urgent
So,now comes the coding part(Please note that this is a linkedlist)
Interface:
public interface ListInterface<T> {
public boolean add(T newEntry);
public boolean add(int newPosition, T newEntry);
public T remove(int givenPosition);
public void clear();
public boolean replace(int givenPosition, T newEntry);
public T getEntry(int givenPosition);
public boolean contains(T anEntry);
public int getLength();
public boolean isEmpty();
public boolean isFull();
}
LList class:
/**
* LList.java
* A class that implements the ADT list by using a chain of nodes,
* with the node implemented as an inner class.
*/
public class LList<T> implements ListInterface<T> {
private Node firstNode; // reference to first node
private int length; // number of entries in list
public LList() {
clear();
}
public final void clear() {
firstNode = null;
length = 0;
}
public boolean add(T newEntry) {
Node newNode = new Node(newEntry); // create the new node
if (isEmpty()) // if empty list
firstNode = newNode;
else { // add to end of nonempty list
Node currentNode = firstNode; // traverse linked list with p pointing to the current node
while (currentNode.next != null) { // while have not reached the last node
currentNode = currentNode.next;
}
currentNode.next = newNode; // make last node reference new node
}
length++;
return true;
}
public boolean add(int newPosition, T newEntry) { // OutOfMemoryError possible
boolean isSuccessful = true;
if ((newPosition >= 1) && (newPosition <= length+1)) {
Node newNode = new Node(newEntry);
if (isEmpty() || (newPosition == 1)) { // case 1: add to beginning of list
newNode.next = firstNode;
firstNode = newNode;
}
else { // case 2: list is not empty and newPosition > 1
Node nodeBefore = firstNode;
for (int i = 1; i < newPosition - 1; ++i) {
nodeBefore = nodeBefore.next; // advance nodeBefore to its next node
}
newNode.next = nodeBefore.next; // make new node point to current node at newPosition
nodeBefore.next = newNode; // make the node before point to the new node
}
length++;
}
else
isSuccessful = false;
return isSuccessful;
}
public T remove(int givenPosition) {
T result = null; // return value
if ((givenPosition >= 1) && (givenPosition <= length)) {
if (givenPosition == 1) { // case 1: remove first entry
result = firstNode.data; // save entry to be removed
firstNode = firstNode.next;
}
else { // case 2: givenPosition > 1
Node nodeBefore = firstNode;
for (int i = 1; i < givenPosition - 1; ++i) {
nodeBefore = nodeBefore.next; // advance nodeBefore to its next node
}
result = nodeBefore.next.data; // save entry to be removed
nodeBefore.next = nodeBefore.next.next; // make node before point to node after the
} // one to be deleted (to disconnect node from chain)
length--;
}
return result; // return removed entry, or
// null if operation fails
}
public boolean replace(int givenPosition, T newEntry) {
boolean isSuccessful = true;
if ((givenPosition >= 1) && (givenPosition <= length)) {
Node currentNode = firstNode;
for (int i = 0; i < givenPosition - 1; ++i) {
// System.out.println("Trace| currentNode.data = " + currentNode.data + "\t, i = " + i);
currentNode = currentNode.next; // advance currentNode to next node
}
currentNode.data = newEntry; // currentNode is pointing to the node at givenPosition
}
else
isSuccessful = false;
return isSuccessful;
}
public T getEntry(int givenPosition) {
T result = null;
if ((givenPosition >= 1) && (givenPosition <= length)) {
Node currentNode = firstNode;
for (int i = 0; i < givenPosition - 1; ++i) {
currentNode = currentNode.next; // advance currentNode to next node
}
result = currentNode.data; // currentNode is pointing to the node at givenPosition
}
return result;
}
public boolean contains(T anEntry) {
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null)) {
if (anEntry.equals(currentNode.data))
found = true;
else
currentNode = currentNode.next;
}
return found;
}
public int getLength() {
return length;
}
public boolean isEmpty() {
boolean result;
if (length == 0)
result = true;
else
result = false;
return result;
}
public boolean isFull() {
return false;
}
public String toString() {
String outputStr = "";
Node currentNode = firstNode;
while (currentNode != null) {
outputStr += currentNode.data + "\n";
currentNode = currentNode.next;
}
return outputStr;
}
private class Node {
private T data;
private Node next;
private Node(T data) {
this.data = data;
this.next = null;
}
private Node(T data, Node next) {
this.data = data;
this.next = next;
}
} // end Node
} // end LList
Patient class:
public class Patient {
private int emergencyNo;
private int queueTime;
private String patientName;
private String patientIC;
private String patientGender;
private String patientTelNo;
private String patientAdd;
private String visitDate;
public Patient() {
}
public Patient(int emergencyNo, int queueTime, String patientName, String patientIC, String patientGender, String patientTelNo, String patientAdd, String visitDate)
{
this.emergencyNo = emergencyNo;
this.queueTime = queueTime;
this.patientName = patientName;
this.patientIC = patientIC;
this.patientGender = patientGender;
this.patientTelNo = patientTelNo;
this.patientAdd = patientAdd;
this.visitDate = visitDate;
}
//set methods
public void setQueueTime(int queueTime)
{
this.queueTime = queueTime;
}
public boolean setEmergencyNo(int emergencyNo)
{
boolean varEmergencyNo = true;
if (emergencyNo != 1 && emergencyNo != 2 && emergencyNo != 3)
{
varEmergencyNo = false;
System.out.println("Emergency number is in invalid format!");
System.out.println("Emergency number is either 1, 2 or 3 only!");
System.out.println("\n");
}
else
{
this.emergencyNo = emergencyNo;
}
return varEmergencyNo;
}
public boolean setPatientName(String patientName)
{
boolean varPatientName = true;
if (patientName.equals("") || patientName.equals(null))
{
varPatientName = false;
System.out.println("The patient name cannot be empty!\n");
}
else
{
this.patientName = patientName;
}
return varPatientName;
}
public boolean setPatientIC(String patientIC)
{
boolean varPatientIC = true;
if(!patientIC.matches("^[0-9]{12}$"))
{
varPatientIC = false;
System.out.println("IC is in invalid format!");
System.out.println("It must consist of 12 numbers only!\n");
}
else
{
this.patientIC = patientIC;
}
return varPatientIC;
}
public boolean setPatientGender(String patientGender)
{
boolean varPatientGender = true;
if(!patientGender.equals("F") && !patientGender.equals("f") && !patientGender.equals("M") && !patientGender.equals("m"))
{
varPatientGender = false;
System.out.println("Gender is in invalid format!");
System.out.println("It must be either 'M' or 'F' only!\n");
}
else
{
this.patientGender = patientGender;
}
return varPatientGender;
}
public boolean setPatientTelNo(String patientTelNo)
{
boolean varPatientTelNo = true;
if((!patientTelNo.matches("^01[02346789]\\d{7}$")) && (!patientTelNo.matches("^03\\d{8}$")))
{
varPatientTelNo = false;
System.out.println("Invalid phone number!");
System.out.println("It must be in the following format : 0167890990 / 0342346789!\n");
System.out.print("\n");
}
else
{
this.patientTelNo = patientTelNo;
}
return varPatientTelNo;
}
public boolean setPatientAdd(String patientAdd)
{
boolean varPatientAdd = true;
if (patientAdd.equals("") || patientAdd.equals(null))
{
varPatientAdd = false;
System.out.println("The patient address cannot be empty!\n");
}
else
{
this.patientAdd = patientAdd;
}
return varPatientAdd;
}
public void setVisitDate(String visitDate)
{
this.visitDate = visitDate;
}
//get methods
public int getQueueTime()
{
return this.queueTime;
}
public int getEmergencyNo()
{
return this.emergencyNo;
}
public String getPatientName()
{
return this.patientName;
}
public String getPatientIC()
{
return this.patientIC;
}
public String getPatientGender()
{
return this.patientGender;
}
public String getPatientTelNo()
{
return this.patientTelNo;
}
public String getPatientAdd()
{
return this.patientAdd;
}
public String getVisitDate()
{
return this.visitDate;
}
#Override
public String toString()
{
return (this.emergencyNo + "\t\t" + this.patientName + "\t\t" + this.patientIC +
"\t\t" + this.patientGender + "\t\t" + this.patientTelNo + "\t\t" + this.patientAdd + "\t\t" + this.visitDate);
}
public String anotherToString()
{
return (this.emergencyNo + "\t\t\t\t\t\t" + this.patientName + "\t\t\t " + this.visitDate);
}
}
EmergencyCmp(Comparator)--->use for sorting the emergency numbers of the patients
import java.util.Comparator;
public class EmergencyCmp implements Comparator<Patient>
{
#Override
public int compare(Patient p1, Patient p2)
{
if(p1.getEmergencyNo() > p2.getEmergencyNo())
{
return 1;
}
else
{
return -1;
}
}
}
QueueCmp(Comparator)--->use for sorting the arrival time of the patients
import java.util.Comparator;
public class QueueCmp implements Comparator<Patient>
{
#Override
public int compare(Patient p1, Patient p2)
{
if(p1.getQueueTime() > p2.getQueueTime())
{
return 1;
}
else
{
return -1;
}
}
}
Main function:
import java.util.Calendar;
import java.util.Scanner;
import java.util.Arrays;
import java.util.*;
public class DSA {
public DSA() {
}
public static void main(String[] args) {
//patient's attributes
int emergencyNo;
int queueTime;
String patientName;
String patientIC;
String patientGender;
String patientTelNo;
String patientAdd;
String visitDate;
//counter
int j = 0;
int x = 0;
int y = 0;
int z = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int countEnteredPatient = 1;
int totalCount = 0;
//calendar
int nowYr, nowMn, nowDy, nowHr, nowMt, nowSc;
//others
boolean enterNewPatient = true;
String continueInput;
boolean enterNewPatient1 = true;
String continueInput1;
boolean continueEmergencyNo;
Scanner scan = new Scanner(System.in);
ListInterface<Patient> patientList = new LList<Patient>();
ListInterface<Patient> newPatientList = new LList<Patient>();
Patient[] patientArr1 = new Patient[10000];
Patient[] patientArr2 = new Patient[10000];
Patient[] patientArr3 = new Patient[10000];
Patient tempoPatient;
do{
//do-while loop for entering new patient details after viewing patient list
System.out.println("Welcome to Hospital Ten Stars!\n");
do{
//do-while loop for entering new patient details
System.out.println("Entering details of patient " + countEnteredPatient);
System.out.println("===================================\n");
Calendar calendar = Calendar.getInstance();
nowYr = calendar.get(Calendar.YEAR);
nowMn = calendar.get(Calendar.MONTH);
nowDy = calendar.get(Calendar.DAY_OF_MONTH);
nowHr = calendar.get(Calendar.HOUR);
nowMt = calendar.get(Calendar.MINUTE);
nowSc = calendar.get(Calendar.SECOND);
queueTime = calendar.get(Calendar.MILLISECOND);
visitDate = nowDy + "/" + nowMn + "/" + nowYr + ", " + nowHr + ":" + nowMt + ":" + nowSc;
//input emergency number
do{
tempoPatient = new Patient();
continueEmergencyNo = false;
int EmergencyNoOption;
try
{
do{
System.out.print("Please select 1 – Immediately life threatening, 2 – Urgent, but not immediately life threatening or 3 – Less urgent(Eg: 1) : ");
EmergencyNoOption = scan.nextInt();
scan.nextLine();
System.out.print("\n");
}while(tempoPatient.setEmergencyNo(EmergencyNoOption) == false);
}
catch(InputMismatchException ex)
{
System.out.print("\n");
System.out.println("Invalid input detected.");
scan.nextLine();
System.out.print("\n");
continueEmergencyNo = true;
}
}while(continueEmergencyNo);
//input patient name
do{
System.out.print("Patient name(Eg: Christine Redfield) : ");
patientName = scan.nextLine();
System.out.print("\n");
}while(tempoPatient.setPatientName(patientName) == false);
//input patient ic no
do{
System.out.print("Patient IC number(Eg: 931231124567) : ");
patientIC = scan.nextLine();
System.out.print("\n");
}while(tempoPatient.setPatientIC(patientIC) == false);
//input patient gender
do{
System.out.print("Patient gender(Eg: M) : ");
patientGender = scan.nextLine();
System.out.print("\n");
}while(tempoPatient.setPatientGender(patientGender) == false);
//input patient tel. no
do{
System.out.print("Patient tel.No(without'-')(Eg: 0162345678/0342980123) : ");
patientTelNo = scan.nextLine();
System.out.print("\n");
}while(tempoPatient.setPatientTelNo(patientTelNo) == false);
//input patient address
do{
System.out.print("Patient address(Eg: 4-C9 Jln Besar 123, Taman Besar, 56000 Kuala Lumpur) : ");
patientAdd = scan.nextLine();
System.out.print("\n");
}while(tempoPatient.setPatientAdd(patientAdd) == false);
tempoPatient.setQueueTime(queueTime);
tempoPatient.setVisitDate(visitDate);
patientList.add(tempoPatient);
//decide whether want to enter a new patient or not
do{
System.out.print("Do you want to enter another new patient?(Eg: Y/N) : ");
continueInput = scan.nextLine();
if(continueInput.equals("Y") || continueInput.equals("y"))
{
enterNewPatient = true;
System.out.print("\n");
}
else if(continueInput.equals("N") || continueInput.equals("n"))
{
enterNewPatient = false;
}
else
{
System.out.println("\n");
System.out.println("Please enter Y/N only.\n");
}
}while(!continueInput.equals("Y") && !continueInput.equals("y") && !continueInput.equals("N") && !continueInput.equals("n"));
countEnteredPatient++;
}while(enterNewPatient); //end do-while loop for entering new patient details
System.out.println("\nWaiting list of patient will be displayed soon.\n");
try{
Thread.sleep(1000);
}
catch (Exception e)
{
}
System.out.println("Waiting list of patients");
System.out.println("========================\n");
System.out.println("Number\t\tEmergency number\t\tPatient name\t\t ArrivalTime");
System.out.println("============================================================================");
for(int i = 1; i <= patientList.getLength(); i++)
{
System.out.println(i + "\t\t\t" + patientList.getEntry(i).anotherToString());
}
do{
System.out.print("\nSo, now do you want to enter another new patient?(Eg: Y/N) : ");
continueInput1 = scan.nextLine();
if(continueInput1.equals("Y") || continueInput1.equals("y"))
{
enterNewPatient1 = true;
System.out.print("\n");
}
else if(continueInput1.equals("N") || continueInput1.equals("n"))
{
enterNewPatient1 = false;
}
else
{
System.out.println("\n");
System.out.println("Please enter Y/N only.\n");
}
}while(!continueInput1.equals("Y") && !continueInput1.equals("y") && !continueInput1.equals("N") && !continueInput1.equals("n"));
}while(enterNewPatient1);//end do-while loop for entering new patient details after viewing patient list
System.out.println("\nNow rearranging the list based on the seriouness and their arrival time.");
try{
Thread.sleep(1000);
}
catch (Exception e)
{
}
//create an unsorted array
Patient[] tempoPatientArr = new Patient[patientList.getLength()];
//copy the contents of patientList into tempoPatientArr
for(int i = 1; i <= patientList.getLength(); i++ )
{
tempoPatientArr[i-1] = patientList.getEntry(i);
}
//sort tempoPatientArr
Arrays.sort(tempoPatientArr, new EmergencyCmp());
//the above part until this comment line does not have problem
//check the emergency no and then categorise accordingly
for(int i = 0; i < tempoPatientArr.length; i++)
{
if(tempoPatientArr[i].getEmergencyNo() == 1)
{
patientArr1[x] = tempoPatientArr[i];
x++;
}
else if(tempoPatientArr[i].getEmergencyNo() == 2)
{
patientArr2[y] = tempoPatientArr[i];
y++;
}
else if(tempoPatientArr[i].getEmergencyNo() == 3)
{
patientArr3[z] = tempoPatientArr[i];
z++;
}
}
//to check how many !null elements by using count for 3 sub-arrays
for(int i = 0; i < patientArr1.length; i++)
{
if(patientArr1[i] != null)
{
count1++;
}
else
{
break;
}
}
for(int i = 0; i < patientArr2.length; i++)
{
if(patientArr2[i] != null)
{
count2++;
}
else
{
break;
}
}
for(int i = 0; i < patientArr3.length; i++)
{
if(patientArr3[i] != null)
{
count3++;
}
else
{
break;
}
}
//new array with elimination of null values
Patient[] newPatientArr1 = new Patient[count1];
Patient[] newPatientArr2 = new Patient[count2];
Patient[] newPatientArr3 = new Patient[count3];
//copy the contents of old sub arrays(the arrays with null values) into the new sub arrays(without null values)
for(int i = 0; i < newPatientArr1.length; i++)
{
newPatientArr1[i] = patientArr1[i];
}
for(int i = 0; i < newPatientArr2.length; i++)
{
newPatientArr2[i] = patientArr2[i];
}
for(int i = 0; i < newPatientArr3.length; i++)
{
newPatientArr3[i] = patientArr3[i];
}
totalCount = count1 + count2 + count3;
//array that used to combine all the sub-arrays
Patient[] newPatientArr = new Patient[totalCount];
//sort all sub new arrays
Arrays.sort(newPatientArr1, new QueueCmp());
Arrays.sort(newPatientArr2, new QueueCmp());
Arrays.sort(newPatientArr3, new QueueCmp());
//combine the contents of sub new arrays into the newPatientArr array
do{
for (int i = 0; i < count1; i++)
{
newPatientArr[j] = newPatientArr1[i];
j++;
}
for (int b = 0; b < count2; b++)
{
newPatientArr[j] = newPatientArr2[b];
j++;
}
for (int c = 0; c < count3; c++)
{
newPatientArr[j] = newPatientArr3[c];
j++;
}
}while(j < totalCount);
//relink the nodes
for(int i = 0; i < newPatientArr.length; i++)
{
newPatientList.add(newPatientArr[i]);
}
System.out.println("\nSorted waiting list of patients");
System.out.println("===============================\n");
System.out.println("Number\t\tEmergency number\t\tPatient name\t\t ArrivalTime");
System.out.println("============================================================================");
for(int i = 1; i <= newPatientList.getLength(); i++)
{
System.out.println(i + "\t\t\t" + newPatientList.getEntry(i).anotherToString());
}
}
}
Interface and LList class definitely do not have problems. So everyone can skip the 2 parts.
For the main function, I have a comment like this:
//the above part until this comment line does not have problem
When you all see the comment, that means the previous code does not have problem and you all may skip it and below is an attachment of the result that I got earlier:
So, from the picture you all can see that the sorting of arrival time is not correct. I hope that I can know why does this problem occurs since I cannot figure it out by myself. Thanks to all of you first.
So, after taking the advice of #Scott Hunter, I have made the following modification to the EmergencyCmp:
#Override
public int compare(Patient p1, Patient p2)
{
int value = 0;
if(p1.getEmergencyNo() > p2.getEmergencyNo())
{
value = 1;
}
else if(p1.getEmergencyNo() < p2.getEmergencyNo())
{
value = -1;
}
else if(p1.getEmergencyNo() == p2.getEmergencyNo())
{
if(p1.getQueueTime() > p2.getQueueTime())
{
return 1;
}
else
{
return -1;
}
}
return value;
}
However, the time sorting still produce a wrong result.
As I understand it (which I may not; you provided a LOT of extraneous stuff), it looks like you are trying to perform 2 distinct sorts, one after the other, such that the second is undoing the work of the first. Instead, you should define a single Comparator which compares emergency numbers and, only if they are the same, compares arrival times.

Binary Search Tree Iterate non-keys

I have a bit of a conundrum. I need to print all the values in my BST that are NOT keys. So since the tree is not ordered according to these values, I can't do as I normally have with BSTs in the past. I simply need to look at EVERY node on the tree, compare the non-key value with the value I have entered, and determine whether to print it.
I.E. Student directory where I need to print all GPAs above a 2.0. Since the tree is ordered by Student IDs and not GPA, how do I go through every node and compare GPA and print all nodes that are above 2.0?
If you need to look at my code, the whole thing is here, and it's enormous.
public class StudentBST
{
private static Node root;
static class Node
{
public int studentID;
public String lastName;
public String firstName;
public String major;
public double gpa;
public Node left, right;
public int minValue()
{
if(left == null)
{
return studentID;
}
else
{
return left.minValue();
}
}
public boolean remove(int i, Node node)
{
if(i < this.studentID)
{
if(left != null)
{
return left.remove(i, this);
}
else
{
return false;
}
}
else if(i > this.studentID)
{
if(right != null)
{
return right.remove(i, this);
}
else
{
return false;
}
}
else
{
if(left != null && right != null)
{
this.studentID = right.minValue();
right.remove(this.studentID, this);
}
else if(node.left == this)
{
node.left = (left != null) ? left : right;
}
else if(node.right == this)
{
node.right = (left != null) ? left : right;
}
return true;
}
}
public Node(int i, String l, String f, String m, double g)
{
studentID = i;
lastName = l;
firstName = f;
major = m;
gpa = g;
left = null;
right = null;
}
}
public StudentBST()
{
root = null;
}
private static void insert(int i, String l, String f, String m, double g)
{
root = insert(root, i, l, f, m , g);
}
private static Node insert(Node node, int i, String l, String f, String m, double g)
{
if(node == null)
{
node = new Node(i, l, f, m, g);
}
else
{
if(i <= node.studentID)
{
node.left = insert(node.left, i, l, f, m, g);
}
else
{
node.right = insert(node.right, i, l, f, m, g);
}
}
return(node);
}
public static void printBST()
{
printBST(root);
System.out.println();
}
private static void printBST(Node node)
{
if(node == null)
{
return;
}
printBST(node.left);
System.out.println(node.studentID + ", " + node.lastName + ", " + node.firstName
+ ", " + node.major + ", " + node.gpa);
printBST(node.right);
}
public static boolean remove(int i)
{
if(root == null)
{
return false;
}
else
{
if(root.studentID == i)
{
Node auxRoot = new Node(0, "", "", "", 0);
auxRoot.left = root;
boolean result = root.remove(i, auxRoot);
root = auxRoot.left;
return result;
}
else
{
return root.remove(i, null);
}
}
}
public static void main(String[] args)
{
StudentBST.insert(8, "Costanza", "George", "Napping", 1.60);
StudentBST.insert(10, "Kramer", "Cosmo", "Chemistry", 3.04);
StudentBST.insert(5, "Seinfeld", "Jerry", "Theater", 2.05);
StudentBST.printBST();
Scanner input = new Scanner(System.in);
int option = 9;
while(option != 0)
{
System.out.println("1 - Add new student 2 - Delete student 3 - Print All" +
" 0 - Exit");
option = input.nextInt();
if(option == 1)
{
System.out.println("Enter student ID");
int i = input.nextInt();
input.nextLine();
System.out.println("Enter Last Name");
String l = input.nextLine();
System.out.println("Enter First Name");
String f = input.nextLine();
System.out.println("Enter major");
String m = input.nextLine();
System.out.println("Enter GPA");
Double g = input.nextDouble();
System.out.println("Inserted student record");
StudentBST.insert(i, l, f, m, g);
}
if(option == 2)
{
System.out.println("Enter Student ID to delete");
int i = input.nextInt();
boolean b = StudentBST.remove(i);
if(b)
{
System.out.println("Deletion completed");
}
else
{
System.out.println("Deletion encountered error");
}
}
if(option == 3)
{
StudentBST.printBST();
}
}
}
I think you have the right idea: just walk across the whole tree and print out GPAs higher than a certain threshold. A rough implementation looks like:
public void printGPAs(Node node, double gpa_cutoff) {
if (node == null) {
return;
}
if (node.gpa >= gpa_cutoff) {
System.out.println(node.gpa);
}
printGPAs(node.left);
printGPAs(node.right);
}
If you wanted to print them out in a particular order, the easiest way would be to drop them in to a list as you go along, inserting in the correct place to maintain your desired order.

Making a Tree and Sorting out it's numbers

I have a txtfile with a list of unsorted numbers. I took the list of numbers and made an array. So now i'm trying to make a tree with those numbers and sort them out based off of what on the left and right. Right now its printing, but not sorted. I'm pretty sure the tree isn't being made right but I'm not sure how to fix it. I also want to keep track of any duplicates. In other words I don't want to print out any duplicates but instead just keep track of how many there are. Any help or advice is appreciated. Thanks in advance.
-- I pass the array of numbers in method: dealArray(), which then converts it to a int. From there those #'s are passed in findDuplicate() which I'm not sure I should be doing or not.
BigTree Class:
public class bigTree {
int data; int frequency;
bigTree Left, Right;
public bigTree makeTree(int x) {
bigTree p;
p = new bigTree();
p.data = x;
p.Left = null;
p.Right = null;
return p;
}
public void setLeft(bigTree t, int x) {
if (t.Left != null) {
System.out.println("Error");
}
else {
bigTree q;
q = t.Left;
q = makeTree(x);
}
}
public void setRight(bigTree t, int x) {
if (t.Right != null) {
System.out.println("Error");
} else {
bigTree q;
q = t.Right;
q = makeTree(x);
}
}
public void findDuplicate(int number) {
bigTree tree, p, q;
frequency = 0;
tree = makeTree(number);
//while (//there are still #'s in the list) { //1st while()
p = tree;
q = tree;
while (number != p.data && q != null) { //2nd while()
p = q;
if (number < p.data ) {
q = q.Left;
} else {
q = q.Right;
}
} //end of 2nd while()
if (number == p.data) {
Sort(p);
//System.out.println( p.data );
frequency++;
}
else {
if (number < p.data) {
setLeft(p,number);
}
else {
setRight(p, number);
}
}
//} // End of 1st while()
}
public void Sort(bigTree t) {
if (t.Left != null) {
Sort(t.Left);
}
System.out.println(t.data);
if (t.Right != null) {
Sort(t.Right);
}
//Possible print
}
public void dealArray(String[] x) {
int convert;
for (int i = 0; i < x.length; i++){
convert = Integer.parseInt(x[i]);
findDuplicate(convert);
// System.out.println(x[i]);
}
}
Rather than trying to find duplicates ahead of time, find them while building your Tree.
Your tree should probably consist of a Node like the following:
public class Node {
public int number;
public int count;
public Node left;
public Node right;
}
Build up your Binary Search Tree (There are a million tutorials on this). While adding a new element, if you find that element already exists, increment your count for that node.
Then printing is a matter of pre-order traversal (Again, there are a million tutorials on this)
Use the implementation below for hints on how to do it. Sorry I currently cannot format the code and remove the unnecessary methods.
import java.util.*;
public class TreeNode implements Iterator , Iterable{
public Object value;
public TreeNode prev;
public TreeNode left;
public TreeNode right;
private TreeNode w;
public TreeNode(){
}
public TreeNode(Object value){
this.value= value;
}
public Object[] toArray(){
Object[] a= new Object[this.size()];
int i= 0;
for(Object o : this){
a[i]= o;
i++;
}
return a;
}
public TreeNode delete(Object o){
TreeNode z= this.findNode(o);
if(z==null){
return this;
}
if(z.left != null && z.right != null){
TreeNode z1= z.right.minNode();
z.value= z1.value;
z= z1;
}
if(z.left == null && z.right == null){
TreeNode y= z.prev;
if(y==null){
return null;
}
if(y.right == z){
y.right= null;
}else{
y.left= null;
}
return this;
}
if(z.left == null || z.right == null){
TreeNode y;
if(z.left != null){
y= z.left;
}else{
y= z.right;
}
if(z.prev == null){
y.prev= null;
return y;
}else{
y.prev= z.prev;
if(z.prev.left == z){
z.prev.left= y;
}else{
z.prev.right= y;
}
}
}
return this;
}
public boolean hasNext(){
return w != null;
}
public Object next(){
Object d= w.value;
w= w.nextNode();
return d;
}
public void remove(){
}
public Iterator iterator(){
w= this.minNode();
return this;
}
public Object min(){
if(this.left == null){
return this.value;
}
return this.left.min();
}
public TreeNode minNode(){
if(this.left == null){
return this;
}
return this.left.minNode();
}
public Object max(){
if(this.right == null){
return this.value;
}
return this.right.max();
}
public void print(){
System.out.println(this.value);
if(left != null){
this.left.print();
}
if(right != null){
this.right.print();
}
}
public void printSort(){
if(left != null){
this.left.printSort();
}
System.out.println(this.value);
if(right != null){
this.right.printSort();
}
}
public static String intervals(int n, int p){
String s= " ";
return s.substring(0, n*p);
}
public void printTree(int p){
printTree0(1, p);
}
public void printTree0(int d, int p){
if(this.right != null){
this.right.printTree0(d+1, p);
}
System.out.println(intervals(d, p) + this.value);
if(this.left != null){
this.left.printTree0(d+1, p);
}
}
public boolean add(Object o){
if(this.value.equals(o)){
return false;
}
if( ((Comparable)this.value).compareTo(o) > 0 ){ //left
if(left != null){
left.add(o);
}else{
left= new TreeNode(o);
left.prev= this;
}
}else{ // right
if(right != null){
right.add(o);
}else{
right= new TreeNode(o);
right.prev= this;
}
}
return true;
}
public void addBalanced(Object o){
int l= rang(this.left);
int r= rang(this.right);
boolean ldir= true;
if(l == r){
int ls= size(this.left);
int rs= size(this.right);
if(ls > rs){
ldir= false;
}
}else{
ldir= l <= r;
}
if(ldir){
if(this.left==null){
this.left= new TreeNode(o);
}else{
this.left.addBalanced(o);
}
}else{
if(this.right==null){
this.right= new TreeNode(o);
}else{
this.right.addBalanced(o);
}
}
}
public TreeNode nextNode(){
if(this.right != null){
return this.right.minNode();
}
TreeNode t1= this;
TreeNode t2= this.prev;
while(t2!=null && t2.right==t1){
t1= t2;
t2= t2.prev;
}
return t2;
}
public TreeNode findNode(Object o){
if(this.value.equals(o)){
return this;
}
if( ((Comparable)this.value).compareTo(o) > 0 ){ //left
if(left != null){
return left.findNode(o);
}
}else{ // right
if(right != null){
return right.findNode(o);
}
}
return null;
}
public int size(){
int n= 1;
if(this.left != null){
n= n + this.left.size();
}
if(this.right != null){
n= n + this.right.size();
}
return n;
}
public static int size(TreeNode t){
if(t==null){
return 0;
}
return 1 + TreeNode.size(t.left) + TreeNode.size(t.right);
}
public int rang(){
int l= 0;
int r= 0;
if(left!=null){
l= left.rang();
}
if(right!=null){
r= right.rang();
}
return 1 +( (l > r) ? l : r ) ;
}
public static int rang(TreeNode t){
if(t==null){
return 0;
}
int l= rang(t.left);
int r= rang(t.right);
return 1 +( (l > r) ? l : r ) ;
}
public String toString(){
return "( " + value + " " + left + " " + right + " )";
}
/*
public String toString(){
String s= "( " + this.value + " ";
if(this.left == null && this.right == null){
return s + ")";
}
if(this.left==null){
s= s + "( )";
}else{
s= s + this.left;
}
if(this.right==null){
s= s + "( )";
}else{
s= s + this.right;
}
return s + ")";
}
*/
}

Categories