Last active
October 10, 2023 04:01
-
-
Save PatheticMustan/02d8b1eaca74e7307356d0108c668fd7 to your computer and use it in GitHub Desktop.
vague spec and no clarification from TAs... what do they even want from us
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
// properties | |
private Node left; | |
private Node right; | |
private int value; | |
// getters | |
public Node getLeft() { return left; } | |
public Node getRight() { return right; } | |
public int getValue() { return value; } | |
// setters | |
public void setLeft(Node n) { left = n; } | |
public void setRight(Node n) { right = n; } | |
public void setValue(int v) { value = v; } | |
// constructors | |
public Node() { this(0, null, null); } | |
public Node(int v) { this(v, null, null); } | |
private Node(int v, Node l, Node r) { // only available to internals | |
value = v; | |
left = l; | |
right = r; | |
} | |
} | |
public class BinarySearchTree { | |
public static void insert(Node root, int key) { | |
// exclude duplicates | |
if (root.getValue() == key) return; | |
if (key < root.getValue()) { | |
// if it's less than the center, it should be on the left subtree | |
if (root.getLeft() == null) { | |
root.setLeft(new Node(key)); | |
} else { | |
insert(root.getLeft(), key); | |
} | |
} else { | |
// right subtree | |
if (root.getRight() == null) { | |
root.setRight(new Node(key)); | |
} else { | |
insert(root.getRight(), key); | |
} | |
} | |
} | |
public static void preorderRec(Node root) { | |
// C, L, R | |
System.out.print(root.getValue() + " "); | |
if (root.getLeft() != null) preorderRec(root.getLeft()); | |
if (root.getRight() != null) preorderRec(root.getRight()); | |
} | |
public static int sum(Node root) { | |
return ( | |
(root.getValue()) + | |
((root.getLeft() != null)? sum(root.getLeft()) : 0) + | |
((root.getRight() != null)? sum(root.getRight()) : 0) | |
); | |
} | |
public static Node kthBiggest(Node root, int k) { | |
// count nodes on the right, which are all bigger | |
int r = ((root.getRight() != null)? countNodes(root.getRight()) : 0); | |
if (k - r == 1) return root; | |
if (k <= r) return kthBiggest(root.getRight(), k); | |
return kthBiggest(root.getLeft(), k-r-1); | |
} | |
// helper function | |
public static int countNodes(Node root) { | |
return ( | |
1 + | |
((root.getLeft() != null)? countNodes(root.getLeft()) : 0) + | |
((root.getRight() != null)? countNodes(root.getRight()) : 0) | |
); | |
} | |
public static void main(String args[]) { | |
Node root = new Node(23); | |
insert(root, 5); | |
insert(root, 11); | |
insert(root, 4); | |
insert(root, 1); | |
insert(root, 3); | |
insert(root, 40); | |
insert(root, 18); | |
preorderRec(root); | |
System.out.println(); | |
System.out.println(sum(root)); | |
System.out.println(countNodes(root)); | |
// 1 3 4 5 11 18 23 40 | |
// should return 40, the biggest | |
System.out.println(kthBiggest(root, 1).getValue()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.NoSuchElementException; | |
public class DNode { | |
// properties | |
private DNode prev; | |
private DNode next; | |
private int value; | |
// getters and setters | |
public DNode getPrev() { return prev; } | |
public void setPrev(DNode t) { prev = t; } | |
public DNode getNext() { return next; } | |
public void setNext(DNode t) { next = t; } | |
public int getValue() { return value; } | |
public void setValue(int n) { value = n; } | |
// util | |
public void addNode(DNode t) { | |
// list <-> t | |
DNode tail = this; | |
while (tail.getNext() != null) tail = tail.getNext(); | |
tail.setNext(t); | |
t.setPrev(tail); | |
} | |
public void printList() { | |
DNode p = this; | |
while (p != null) { | |
System.out.print(p.getValue() + " "); | |
p = p.getNext(); | |
} | |
System.out.println(); | |
} | |
// default value of 0 if no parameter is provided | |
public DNode() { this(0); } | |
public DNode(int v) { | |
prev = null; | |
next = null; | |
value = v; | |
} | |
public static void main(String[] args) { | |
// make new head | |
DNode L = new DNode(1); | |
DNode X = new DNode(5); | |
L.addNode(new DNode(2)); | |
L.addNode(new DNode(3)); | |
L.addNode(new DNode(4)); | |
L.addNode(X); | |
L.addNode(new DNode(6)); | |
L.addNode(new DNode(7)); | |
L.printList(); | |
// let's find X, and swap it with it's successor ! | |
// make temp pointer so we can iterate through the list | |
DNode p = L; | |
while (p != X) { | |
// if we've reached the end of the list without finding X, X is not in the list | |
if (p.getNext() == null) throw new NoSuchElementException(X.getValue() + " is not found in the list"); | |
p = p.getNext(); | |
} | |
// now that we've found X, we can swap | |
DNode nodeToBeSwapped = p.getNext(); | |
if (nodeToBeSwapped == null) { | |
// nothing to be swapped, so don't swap | |
} else { | |
// a <-> p <-> nodeToBeSwapped <-> b | |
// a <-> nodeToBeSwapped <-> p <-> b | |
// p: prev=nodeToBeSwapped, next=nodeToBeSwapped.next | |
// nodeToBeSwapped: prev=p.prev, next=p | |
// a: next=nodeToBeSwapped | |
// b: prev=p | |
p.setNext(nodeToBeSwapped.getNext()); | |
nodeToBeSwapped.setPrev(p.getPrev()); | |
p.setPrev(nodeToBeSwapped); | |
nodeToBeSwapped.setNext(p); | |
DNode a = nodeToBeSwapped.getPrev(); | |
if (a != null) { | |
a.setNext(nodeToBeSwapped); | |
} | |
DNode b = p.getNext(); | |
if (b != null) { | |
b.setPrev(p); | |
} | |
} | |
// print our results | |
L.printList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment