0% found this document useful (0 votes)
3 views2 pages

CSE123 Quiz 2reference Sheet

The document contains Java methods for manipulating linked lists and binary trees, including weaving two linked lists, finding the middle element, deleting nodes, and reversing a number. It also includes methods for counting leaves, removing leaves, tightening trees, and limiting path sums. Additionally, there are recursive methods for generating arrangements of names and traversing paths in a grid.

Uploaded by

jonnyliu4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

CSE123 Quiz 2reference Sheet

The document contains Java methods for manipulating linked lists and binary trees, including weaving two linked lists, finding the middle element, deleting nodes, and reversing a number. It also includes methods for counting leaves, removing leaves, tightening trees, and limiting path sums. Additionally, there are recursive methods for generating arrangements of names and traversing paths in a grid.

Uploaded by

jonnyliu4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

public void weave(LinkedIntList other) {​

front = weaveNodes(this.front, other.front);​


this.size += other.size;​
other.front = null;​
other.size = 0;​
}​

private ListNode weaveNodes(ListNode n1, ListNode n2) {​
if (n1 == null) {​ public static void reverseNumber(int n) {​
return n2;​ if (n / 10 == 0) {​
}​ System.out.println(n);​
if (n2 == null) {​ } else if (n < 0) {​
return n1;​ System.out.print("-");​
}​ reverseNumber(-n);​
ListNode n1Next = n1.next;​ } else {​
ListNode n2Next = n2.next;​ System.out.print(n%10);​
n1.next = n2;​ reverseNumber(n/10);​
n2.next = weaveNodes(n1Next, n2Next);​ }​
return n1;​ }
} Collections Recursive Backtracking
public int findMiddle() {​ public static void backTravel(int x, int y, List<String> path)
if (front == null) {​ {​
throw new IllegalStateException();​ if (x == 0 && y == 0) {​
}​ System.out.println(path);​
return findMiddle(front, front);​ } else {​
}​ if (x > 0) {​
​ path.add("E");​
private int findMiddle(ListNode slow, ListNode fast) {​ backTravel(x-1, y, path);​
if (fast == null || fast.next == null) {​ path.remove(path.size() - 1);​
return slow.data;​ }​
}​ if (y > 0) {​
return findMiddle(slow.next, fast.next.next);​ path.add("N");​
} backTravel(x, y - 1, path);​
public void delete(int index) {​ path.remove(path.size() - 1);​
if (index < 0 || index >= size) {​ }​
throw new IllegalArgumentException();​ if (x > 0 && y > 0) {​
}​ path.add("NE");​
front = delete(index, front);​ backTravel(x - 1, y - 1, path);​
size--;​ path.remove(path.size() - 1);​
}​ }​
​ }​
private ListNode delete(int idx, ListNode curr) {​ }
if (idx == 0) {​ Write a method named arrangements that accepts a List of names
return curr = curr.next;​ as a parameter and prints out all of the possible arrangements
}​ of the people in a line.
curr.next = delete(idx-1, curr.next);​ public static void arrangements(List<String> people, boolean[]
return curr;​ used, List<String> curr) {​
} if (curr.size() == people.size()) {​
public void makeEvenOrOdd(boolean isEven) {​ System.out.println(curr);​
front = makeEvenOrOdd(isEven, front);​ } else {​
}​ for (int i = 0; i < people.size(); i++) {​
​ if (!used[i]) {​
private ListNode makeEvenOrOdd(boolean isEven, ListNode used[i] = true;​
curr) {​ curr.add(people.get(i));​
if (curr == null) {​ arrangements(people, used, curr);​
return null;​ curr.remove(curr.size() - 1);​
}​ used[i] = false;​
curr.next = makeEvenOrOdd(isEven, curr.next);​ }​
if ((isEven && curr.data % 2 == 0) || (!isEven && }​
curr.data % 2 != 0)) {​ }​
size--;​ }
return curr.next;​ public void reverse() {​
}​ front = reverse(front);​
return curr;​ }​
} ​
public int height() {​ private ListNode reverse(ListNode curr) {​
return height(overallRoot);​ if (curr == null || curr.next == null) {​
}​ return curr;​
​ }​
private int height(IntTreeNode root) {​ ListNode reversed = reverse(curr.next);​
if (root == null) {​ curr.next.next = curr;​
return -1;​ curr.next = null;​
}​ return reversed;​
int leftHeight = height(root.left);​ }
int rightHeight = height(root.right);​ public void writeTree(Scanner input) {​
return Math.max(leftHeight, rightHeight) + 1;​ overallRoot = writeTreeHelper(input);​
} }​
public int countLeaves() {​ ​
return countLeaves(overallRoot);​ private IntTreeNode writeTreeHelper(Scanner input) {​
}​ int type = input.nextInt();​
​ int data = input.nextInt();​
private int countLeaves(IntTreeNode root) {​ IntTreeNode root = new IntTreeNode(data);​
if (root == null) {​ if (type == 1 || type == 3) {​
return 0;​ root.left = writeTreeHelper(input);​
}​ }​
if (root.left == null && root.right == null) {​ if (type == 2 || type == 3) {​
return 1;​ root.right = writeTreeHelper(input);​
}​ }​
return countLeaves(root.left) + return root;​
countLeaves(root.right);​ }
} Write a method tighten that eliminates branch nodes that have
public void removeLeaves() {​ only one child.
overallRoot = removeLeaves(overallRoot);​ public void tighten() {​
}​ overallRoot = tighten(overallRoot);​
​ }​
private IntTreeNode removeLeaves(IntTreeNode root) {​ ​
if (root == null) {​ private IntTreeNode tighten(IntTreeNode root) {​
return null;​ if (root == null) {​
}​ return null;​
if (root.left == null && root.right == null) {​ }​
return null;​ root.left = tighten(root.left);​
}​ root.right = tighten(root.right);​
root.left = removeLeaves(root.left);​ if (root.left != null && root.right == null) {​
root.right = removeLeaves(root.right);​ root = root.left;​
return root;​ } else if (root.left == null && root.right != null) {​
} root = root.right;​
public void removeLeavesInList(List<Integer> toRemove) {​ }​
overallRoot = removeLeavesInList(toRemove, return root;​
overallRoot);​ }
}​ public void completeToLevel(int target) {​
​ if (target < 1) {​
private IntTreeNode removeLeavesInList(List<Integer> throw new IllegalArgumentException();​
toRemove, IntTreeNode root) {​ }​
if (root == null) {​ overallRoot = completeToLevelHelper(target, overallRoot);​
return null;​ }​
} else if (toRemove.contains(root.data)) {​ ​
return null;​ private IntTreeNode completeToLevelHelper(int target,
}​ IntTreeNode root) {​
root.left = removeLeavesInList(toRemove, root.left);​ if (target >= 1) {​
root.right = removeLeavesInList(toRemove, root.right);​ if (root == null) {​
return root;​ root = new IntTreeNode(-1);​
} }​
public void limitPathSum(int max) {​ root.left = completeToLevelHelper(target - 1,
overallRoot = limitPathSum(max ,overallRoot);​ root.left);​
}​ root.right = completeToLevelHelper(target - 1,
private IntTreeNode limitPathSum(int max, IntTreeNode root) {​ root.right);​
if (root == null) {​ }​
return null;​ return root;​
}​ }
if (max-root.data < 0) {​
return null;​
}​
root.left = limitPathSum(max - root.data, root.left);​
root.right = limitPathSum(max - root.data, root.right);​
return root;​
}

You might also like