-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename, add, move DataStructure java code
- Loading branch information
Showing
5 changed files
with
252 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package DataStructure; | ||
|
||
// https://github.com/yennanliu/CS_basics/blob/master/data_structure/java/BST.java | ||
// https://www.coursera.org/learn/algorithms-part1/lecture/7An9B/binary-search-trees | ||
|
||
public class BST<Key extends Comparable<Key>, Value> { | ||
private Node root; | ||
|
||
private class Node { | ||
private Key key; | ||
private Value val; | ||
private Node left, right; | ||
|
||
public Node(Key key, Value val) { | ||
this.key = key; | ||
this.val = val; | ||
} | ||
} | ||
|
||
// put method via recursive way | ||
public void put(Key key, Value val) { | ||
root = put(root, key, val); | ||
} | ||
|
||
private Node put(Node x, Key key, Value val) { | ||
if (x == null) return new Node(key, val); | ||
int cmp = key.compareTo(x.key); | ||
if (cmp < 0) x.left = put(x.left, key, val); | ||
else if (cmp > 0) x.right = put(x.right, key, val); | ||
else if (cmp == 0) x.val = val; | ||
return x; | ||
} | ||
|
||
public Value get(Key key) { | ||
Node x = root; | ||
while (x != null) { | ||
int cmp = key.compareTo(x.key); | ||
if (cmp < 0) x = x.left; | ||
else if (cmp > 0) x = x.right; | ||
else if (cmp == 0) return x.val; | ||
} | ||
return null; | ||
} | ||
|
||
// public void delete(Key key) | ||
// {} | ||
// | ||
// public Iterable<Key> iterator() | ||
// {} | ||
|
||
} |
172 changes: 172 additions & 0 deletions
172
leetcode_java/src/main/java/DataStructure/DirectedEdge.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package DataStructure; | ||
|
||
// https://www.coursera.org/learn/algorithms-part2/lecture/e3UfD/shortest-paths-apis | ||
// https://github.com/yennanliu/CS_basics/blob/master/data_structure/java/DirectedEdge.java | ||
|
||
public class DirectedEdge { | ||
private final int v, w; | ||
private final double weight; | ||
|
||
public DirectedEdge(int v, int w, double weight) { | ||
this.v = v; | ||
this.w = w; | ||
this.weight = weight; | ||
} | ||
|
||
public int from() { | ||
return v; | ||
} | ||
|
||
public int to() { | ||
return w; | ||
} | ||
|
||
public int weight() { | ||
return (int) weight; | ||
} | ||
|
||
// Implementing "Min Heap" | ||
public static class MinHeap { | ||
// Create a complete binary tree using an array | ||
// Then use the binary tree to construct a Heap | ||
int[] minHeap; | ||
// the number of elements is needed when instantiating an array | ||
// heapSize records the size of the array | ||
int heapSize; | ||
// realSize records the number of elements in the Heap | ||
int realSize = 0; | ||
|
||
public MinHeap(int heapSize) { | ||
this.heapSize = heapSize; | ||
minHeap = new int[heapSize + 1]; | ||
// To better track the indices of the binary tree, | ||
// we will not use the 0-th element in the array | ||
// You can fill it with any value | ||
minHeap[0] = 0; | ||
} | ||
|
||
// Function to add an element | ||
public void add(int element) { | ||
realSize++; | ||
// If the number of elements in the Heap exceeds the preset heapSize | ||
// print "Added too many elements" and return | ||
if (realSize > heapSize) { | ||
System.out.println("Added too many elements!"); | ||
realSize--; | ||
return; | ||
} | ||
// Add the element into the array | ||
minHeap[realSize] = element; | ||
// Index of the newly added element | ||
int index = realSize; | ||
// Parent node of the newly added element | ||
// Note if we use an array to represent the complete binary tree | ||
// and store the root node at index 1 | ||
// index of the parent node of any node is [index of the node / 2] | ||
// index of the left child node is [index of the node * 2] | ||
// index of the right child node is [index of the node * 2 + 1] | ||
int parent = index / 2; | ||
// If the newly added element is smaller than its parent node, | ||
// its value will be exchanged with that of the parent node | ||
while ( minHeap[index] < minHeap[parent] && index > 1 ) { | ||
int temp = minHeap[index]; | ||
minHeap[index] = minHeap[parent]; | ||
minHeap[parent] = temp; | ||
index = parent; | ||
parent = index / 2; | ||
} | ||
} | ||
|
||
// Get the top element of the Heap | ||
public int peek() { | ||
return minHeap[1]; | ||
} | ||
|
||
// Delete the top element of the Heap | ||
public int pop() { | ||
// If the number of elements in the current Heap is 0, | ||
// print "Don't have any elements" and return a default value | ||
if (realSize < 1) { | ||
System.out.println("Don't have any element!"); | ||
return Integer.MAX_VALUE; | ||
} else { | ||
// When there are still elements in the Heap | ||
// realSize >= 1 | ||
int removeElement = minHeap[1]; | ||
// Put the last element in the Heap to the top of Heap | ||
minHeap[1] = minHeap[realSize]; | ||
realSize--; | ||
int index = 1; | ||
// When the deleted element is not a leaf node | ||
while (index <= realSize / 2) { | ||
// the left child of the deleted element | ||
int left = index * 2; | ||
// the right child of the deleted element | ||
int right = (index * 2) + 1; | ||
// If the deleted element is larger than the left or right child | ||
// its value needs to be exchanged with the smaller value | ||
// of the left and right child | ||
if (minHeap[index] > minHeap[left] || minHeap[index] > minHeap[right]) { | ||
if (minHeap[left] < minHeap[right]) { | ||
int temp = minHeap[left]; | ||
minHeap[left] = minHeap[index]; | ||
minHeap[index] = temp; | ||
index = left; | ||
} else { | ||
// maxHeap[left] >= maxHeap[right] | ||
int temp = minHeap[right]; | ||
minHeap[right] = minHeap[index]; | ||
minHeap[index] = temp; | ||
index = right; | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
return removeElement; | ||
} | ||
} | ||
|
||
// return the number of elements in the Heap | ||
public int size() { | ||
return realSize; | ||
} | ||
|
||
public String toString() { | ||
if (realSize == 0) { | ||
return "No element!"; | ||
} else { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append('['); | ||
for (int i = 1; i <= realSize; i++) { | ||
sb.append(minHeap[i]); | ||
sb.append(','); | ||
} | ||
sb.deleteCharAt(sb.length() - 1); | ||
sb.append(']'); | ||
return sb.toString(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
// Test case | ||
MinHeap minHeap = new MinHeap(3); | ||
minHeap.add(3); | ||
minHeap.add(1); | ||
minHeap.add(2); | ||
// [1,3,2] | ||
System.out.println(minHeap.toString()); | ||
// 1 | ||
System.out.println(minHeap.peek()); | ||
// 1 | ||
System.out.println(minHeap.pop()); | ||
// [2, 3] | ||
System.out.println(minHeap.toString()); | ||
minHeap.add(4); | ||
// Add too many elements | ||
minHeap.add(5); | ||
// [2,3,4] | ||
System.out.println(minHeap.toString()); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package DataStructure; | ||
|
||
// https://github.com/yennanliu/CS_basics/blob/master/data_structure/java/LinkedinList.java | ||
// https://github.com/youngyangyang04/leetcode-master/blob/master/problems/%E9%93%BE%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.md | ||
|
||
public class ListNode { | ||
// 结点的值 | ||
int val; | ||
|
||
// 下一个结点 | ||
ListNode next; | ||
|
||
// 节点的构造函数(无参) | ||
public ListNode() { | ||
} | ||
|
||
// 节点的构造函数(有一个参数) | ||
public ListNode(int val) { | ||
this.val = val; | ||
} | ||
|
||
// 节点的构造函数(有两个参数) | ||
public ListNode(int val, ListNode next) { | ||
this.val = val; | ||
this.next = next; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../src/main/java/AlgorithmJava/MaxHeap.java → .../src/main/java/DataStructure/MaxHeap.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package AlgorithmJava; | ||
package DataStructure; | ||
|
||
// https://leetcode.com/explore/learn/card/heap/643/heap/4017/ | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
.../src/main/java/AlgorithmJava/MinHeap.java → .../src/main/java/DataStructure/MinHeap.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package AlgorithmJava; | ||
package DataStructure; | ||
|
||
// https://leetcode.com/explore/learn/card/heap/643/heap/4017/ | ||
|
||
|