Skip to content

Commit 7aef6bf

Browse files
committed
rename, add, move DataStructure java code
1 parent 30ecf62 commit 7aef6bf

File tree

5 files changed

+252
-2
lines changed

5 files changed

+252
-2
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package DataStructure;
2+
3+
// https://github.com/yennanliu/CS_basics/blob/master/data_structure/java/BST.java
4+
// https://www.coursera.org/learn/algorithms-part1/lecture/7An9B/binary-search-trees
5+
6+
public class BST<Key extends Comparable<Key>, Value> {
7+
private Node root;
8+
9+
private class Node {
10+
private Key key;
11+
private Value val;
12+
private Node left, right;
13+
14+
public Node(Key key, Value val) {
15+
this.key = key;
16+
this.val = val;
17+
}
18+
}
19+
20+
// put method via recursive way
21+
public void put(Key key, Value val) {
22+
root = put(root, key, val);
23+
}
24+
25+
private Node put(Node x, Key key, Value val) {
26+
if (x == null) return new Node(key, val);
27+
int cmp = key.compareTo(x.key);
28+
if (cmp < 0) x.left = put(x.left, key, val);
29+
else if (cmp > 0) x.right = put(x.right, key, val);
30+
else if (cmp == 0) x.val = val;
31+
return x;
32+
}
33+
34+
public Value get(Key key) {
35+
Node x = root;
36+
while (x != null) {
37+
int cmp = key.compareTo(x.key);
38+
if (cmp < 0) x = x.left;
39+
else if (cmp > 0) x = x.right;
40+
else if (cmp == 0) return x.val;
41+
}
42+
return null;
43+
}
44+
45+
// public void delete(Key key)
46+
// {}
47+
//
48+
// public Iterable<Key> iterator()
49+
// {}
50+
51+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package DataStructure;
2+
3+
// https://www.coursera.org/learn/algorithms-part2/lecture/e3UfD/shortest-paths-apis
4+
// https://github.com/yennanliu/CS_basics/blob/master/data_structure/java/DirectedEdge.java
5+
6+
public class DirectedEdge {
7+
private final int v, w;
8+
private final double weight;
9+
10+
public DirectedEdge(int v, int w, double weight) {
11+
this.v = v;
12+
this.w = w;
13+
this.weight = weight;
14+
}
15+
16+
public int from() {
17+
return v;
18+
}
19+
20+
public int to() {
21+
return w;
22+
}
23+
24+
public int weight() {
25+
return (int) weight;
26+
}
27+
28+
// Implementing "Min Heap"
29+
public static class MinHeap {
30+
// Create a complete binary tree using an array
31+
// Then use the binary tree to construct a Heap
32+
int[] minHeap;
33+
// the number of elements is needed when instantiating an array
34+
// heapSize records the size of the array
35+
int heapSize;
36+
// realSize records the number of elements in the Heap
37+
int realSize = 0;
38+
39+
public MinHeap(int heapSize) {
40+
this.heapSize = heapSize;
41+
minHeap = new int[heapSize + 1];
42+
// To better track the indices of the binary tree,
43+
// we will not use the 0-th element in the array
44+
// You can fill it with any value
45+
minHeap[0] = 0;
46+
}
47+
48+
// Function to add an element
49+
public void add(int element) {
50+
realSize++;
51+
// If the number of elements in the Heap exceeds the preset heapSize
52+
// print "Added too many elements" and return
53+
if (realSize > heapSize) {
54+
System.out.println("Added too many elements!");
55+
realSize--;
56+
return;
57+
}
58+
// Add the element into the array
59+
minHeap[realSize] = element;
60+
// Index of the newly added element
61+
int index = realSize;
62+
// Parent node of the newly added element
63+
// Note if we use an array to represent the complete binary tree
64+
// and store the root node at index 1
65+
// index of the parent node of any node is [index of the node / 2]
66+
// index of the left child node is [index of the node * 2]
67+
// index of the right child node is [index of the node * 2 + 1]
68+
int parent = index / 2;
69+
// If the newly added element is smaller than its parent node,
70+
// its value will be exchanged with that of the parent node
71+
while ( minHeap[index] < minHeap[parent] && index > 1 ) {
72+
int temp = minHeap[index];
73+
minHeap[index] = minHeap[parent];
74+
minHeap[parent] = temp;
75+
index = parent;
76+
parent = index / 2;
77+
}
78+
}
79+
80+
// Get the top element of the Heap
81+
public int peek() {
82+
return minHeap[1];
83+
}
84+
85+
// Delete the top element of the Heap
86+
public int pop() {
87+
// If the number of elements in the current Heap is 0,
88+
// print "Don't have any elements" and return a default value
89+
if (realSize < 1) {
90+
System.out.println("Don't have any element!");
91+
return Integer.MAX_VALUE;
92+
} else {
93+
// When there are still elements in the Heap
94+
// realSize >= 1
95+
int removeElement = minHeap[1];
96+
// Put the last element in the Heap to the top of Heap
97+
minHeap[1] = minHeap[realSize];
98+
realSize--;
99+
int index = 1;
100+
// When the deleted element is not a leaf node
101+
while (index <= realSize / 2) {
102+
// the left child of the deleted element
103+
int left = index * 2;
104+
// the right child of the deleted element
105+
int right = (index * 2) + 1;
106+
// If the deleted element is larger than the left or right child
107+
// its value needs to be exchanged with the smaller value
108+
// of the left and right child
109+
if (minHeap[index] > minHeap[left] || minHeap[index] > minHeap[right]) {
110+
if (minHeap[left] < minHeap[right]) {
111+
int temp = minHeap[left];
112+
minHeap[left] = minHeap[index];
113+
minHeap[index] = temp;
114+
index = left;
115+
} else {
116+
// maxHeap[left] >= maxHeap[right]
117+
int temp = minHeap[right];
118+
minHeap[right] = minHeap[index];
119+
minHeap[index] = temp;
120+
index = right;
121+
}
122+
} else {
123+
break;
124+
}
125+
}
126+
return removeElement;
127+
}
128+
}
129+
130+
// return the number of elements in the Heap
131+
public int size() {
132+
return realSize;
133+
}
134+
135+
public String toString() {
136+
if (realSize == 0) {
137+
return "No element!";
138+
} else {
139+
StringBuilder sb = new StringBuilder();
140+
sb.append('[');
141+
for (int i = 1; i <= realSize; i++) {
142+
sb.append(minHeap[i]);
143+
sb.append(',');
144+
}
145+
sb.deleteCharAt(sb.length() - 1);
146+
sb.append(']');
147+
return sb.toString();
148+
}
149+
}
150+
151+
public static void main(String[] args) {
152+
// Test case
153+
MinHeap minHeap = new MinHeap(3);
154+
minHeap.add(3);
155+
minHeap.add(1);
156+
minHeap.add(2);
157+
// [1,3,2]
158+
System.out.println(minHeap.toString());
159+
// 1
160+
System.out.println(minHeap.peek());
161+
// 1
162+
System.out.println(minHeap.pop());
163+
// [2, 3]
164+
System.out.println(minHeap.toString());
165+
minHeap.add(4);
166+
// Add too many elements
167+
minHeap.add(5);
168+
// [2,3,4]
169+
System.out.println(minHeap.toString());
170+
}
171+
}
172+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package DataStructure;
2+
3+
// https://github.com/yennanliu/CS_basics/blob/master/data_structure/java/LinkedinList.java
4+
// 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
5+
6+
public class ListNode {
7+
// 结点的值
8+
int val;
9+
10+
// 下一个结点
11+
ListNode next;
12+
13+
// 节点的构造函数(无参)
14+
public ListNode() {
15+
}
16+
17+
// 节点的构造函数(有一个参数)
18+
public ListNode(int val) {
19+
this.val = val;
20+
}
21+
22+
// 节点的构造函数(有两个参数)
23+
public ListNode(int val, ListNode next) {
24+
this.val = val;
25+
this.next = next;
26+
}
27+
}

leetcode_java/src/main/java/AlgorithmJava/MaxHeap.java renamed to leetcode_java/src/main/java/DataStructure/MaxHeap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package AlgorithmJava;
1+
package DataStructure;
22

33
// https://leetcode.com/explore/learn/card/heap/643/heap/4017/
44

leetcode_java/src/main/java/AlgorithmJava/MinHeap.java renamed to leetcode_java/src/main/java/DataStructure/MinHeap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package AlgorithmJava;
1+
package DataStructure;
22

33
// https://leetcode.com/explore/learn/card/heap/643/heap/4017/
44

0 commit comments

Comments
 (0)