Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 86652cd

Browse files
committed
Initial commit.
1 parent 67b1042 commit 86652cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+10143
-0
lines changed

custom-data-structure/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.yuelchen</groupId>
4+
<artifactId>custom-data-structure</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>custom-data-structure</name>
7+
<description>A project for implementing custom data structure objects using arrays.</description>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>org.apache.maven.plugins</groupId>
13+
<artifactId>maven-compiler-plugin</artifactId>
14+
<configuration>
15+
<source>1.13</source>
16+
<target>1.13</target>
17+
</configuration>
18+
</plugin>
19+
</plugins>
20+
</build>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.12</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
</project>
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package com.yuelchen.ds;
2+
3+
import com.yuelchen.util.HeapIndex;
4+
5+
public class BinaryMaxHeap<T extends Comparable<T>> {
6+
7+
/**
8+
* An array of objects to store heap values in descending order.
9+
*/
10+
private Object[] heap;
11+
12+
//====================================================================================================
13+
14+
/**
15+
* The number of elements in heap array.
16+
*/
17+
private int size;
18+
19+
//====================================================================================================
20+
21+
/**
22+
* Class constructor (default).
23+
*
24+
* The heap array starting size is 3 by default.
25+
*/
26+
public BinaryMaxHeap() {
27+
// insert code below this line
28+
29+
}
30+
31+
//====================================================================================================
32+
33+
/**
34+
* Returns the number of elements in heap array.
35+
*
36+
* @return The number of elements in heap array.
37+
*/
38+
public int getSize() {
39+
// insert code below this line
40+
41+
return -1; // placeholder return
42+
}
43+
44+
//====================================================================================================
45+
46+
/**
47+
* Inserts the given value into the heap array.
48+
*
49+
* @param t The value to be inserted.
50+
*/
51+
public void insert(T t) {
52+
// insert code below this line
53+
54+
}
55+
56+
//====================================================================================================
57+
58+
/**
59+
* Increase the heap array to the next possible size, and returns true if successful.
60+
*
61+
* Will return false if unable to increase heap size due to already being may array size.
62+
*
63+
* @return True if heap array was successfully increased,
64+
* otherwise return false.
65+
*/
66+
private boolean increaseHeap() {
67+
// insert code below this line
68+
69+
return false; // placeholder return
70+
}
71+
72+
//====================================================================================================
73+
74+
/**
75+
* Retrieves and returns the element at the first index of heap array;
76+
* will return null if the heap array is empty.
77+
*
78+
* The returned element is not removed from heap array.
79+
*
80+
* @return The max heap value, or null if heap array is empty.
81+
*/
82+
@SuppressWarnings("unchecked")
83+
public T get() {
84+
// insert code below this line
85+
86+
return null; // placeholder return
87+
}
88+
89+
//====================================================================================================
90+
91+
/**
92+
* Retrieves and returns the element at the first index of heap array;
93+
* will return null if the heap array is empty.
94+
*
95+
* The returned element is removed from heap array.
96+
*
97+
* When the heap array has more than one element,
98+
* move the latest populated element to first index and then heapify down.
99+
*
100+
* @return The max heap value, or null if heap array is empty.
101+
*/
102+
@SuppressWarnings("unchecked")
103+
public T remove() {
104+
// insert code below this line
105+
106+
return null; // placeholder return
107+
}
108+
109+
//====================================================================================================
110+
111+
/**
112+
* Traverses the heap from the given index to the first element
113+
* and swaps elements values to ensure that the parent value is always
114+
* greater than child value.
115+
*
116+
* @param currentIndex The starting index to traverse from.
117+
*/
118+
@SuppressWarnings("unchecked")
119+
private void heapifyUp(int currentIndex) {
120+
// insert code below this line
121+
122+
}
123+
124+
//====================================================================================================
125+
126+
/**
127+
* Traverses the heap from first element to the last populated index
128+
* and swaps element values to ensure that the parent value is always
129+
* greater than child value.
130+
*/
131+
@SuppressWarnings("unchecked")
132+
private void heapifyDown() {
133+
// insert code below this line
134+
135+
}
136+
137+
//====================================================================================================
138+
139+
/**
140+
* Swaps the values at the given indexes.
141+
*
142+
* @param index1 One of the two indexes needed to swap values in heap array.
143+
* @param index2 One of the two indexes needed to swap values in heap array.
144+
*/
145+
@SuppressWarnings("unchecked")
146+
private void heapifyFlip(int index1, int index2) {
147+
// insert code below this line
148+
149+
}
150+
151+
//====================================================================================================
152+
153+
/**
154+
* Clears the heap of all elements.
155+
*/
156+
public void clear() {
157+
// insert code below this line
158+
159+
}
160+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package com.yuelchen.ds;
2+
3+
import com.yuelchen.util.HeapIndex;
4+
5+
public class BinaryMinHeap<T extends Comparable<T>> {
6+
7+
/**
8+
* An array of objects to store heap values in ascending order.
9+
*/
10+
private Object[] heap;
11+
12+
//====================================================================================================
13+
14+
/**
15+
* The number of elements in heap array.
16+
*/
17+
private int size;
18+
19+
//====================================================================================================
20+
21+
/**
22+
* Class constructor (default).
23+
*
24+
* The heap array starting size is 3 by default.
25+
*/
26+
public BinaryMinHeap() {
27+
// insert code below this line
28+
29+
}
30+
31+
//====================================================================================================
32+
33+
/**
34+
* Returns the number of elements in heap array.
35+
*
36+
* @return The number of elements in heap array.
37+
*/
38+
public int getSize() {
39+
// insert code below this line
40+
41+
return -1; // placeholder return
42+
}
43+
44+
//====================================================================================================
45+
46+
/**
47+
* Inserts the given value into the heap array.
48+
*
49+
* @param t The value to be inserted.
50+
*/
51+
public void insert(T t) {
52+
// insert code below this line
53+
54+
}
55+
56+
//====================================================================================================
57+
58+
/**
59+
* Increase the heap array to the next possible size, and returns true if successful.
60+
*
61+
* Will return false if unable to increase heap size due to already being may array size.
62+
*
63+
* @return True if heap array was successfully increased,
64+
* otherwise return false.
65+
*/
66+
private boolean increaseHeap() {
67+
// insert code below this line
68+
69+
return false; // placeholder return
70+
}
71+
72+
//====================================================================================================
73+
74+
/**
75+
* Retrieves and returns the element at the first index of heap array;
76+
* will return null if the heap array is empty.
77+
*
78+
* The returned element is not removed from heap array.
79+
*
80+
* @return The min heap value, or null if heap array is empty.
81+
*/
82+
@SuppressWarnings("unchecked")
83+
public T get() {
84+
// insert code below this line
85+
86+
return null; // placeholder return
87+
}
88+
89+
//====================================================================================================
90+
91+
/**
92+
* Retrieves and returns the element at the first index of heap array;
93+
* will return null if the heap array is empty.
94+
*
95+
* The returned element is removed from heap array.
96+
*
97+
* When the heap array has more than one element,
98+
* move the latest populated element to first index and then heapify down.
99+
*
100+
* @return The min heap value, or null if heap array is empty.
101+
*/
102+
@SuppressWarnings("unchecked")
103+
public T remove() {
104+
// insert code below this line
105+
106+
return null; // placeholder return
107+
}
108+
109+
//====================================================================================================
110+
111+
/**
112+
* Traverses the heap from the given index to the first element
113+
* and swaps elements values to ensure that the parent value is always
114+
* less than child value.
115+
*
116+
* @param currentIndex The starting index to traverse from.
117+
*/
118+
@SuppressWarnings("unchecked")
119+
private void heapifyUp(int currentIndex) {
120+
// insert code below this line
121+
122+
}
123+
124+
//====================================================================================================
125+
126+
/**
127+
* Traverses the heap from first element to the last populated index
128+
* and swaps element values to ensure that the parent value is always
129+
* less than child value.
130+
*/
131+
@SuppressWarnings("unchecked")
132+
private void heapifyDown() {
133+
// insert code below this line
134+
135+
}
136+
137+
//====================================================================================================
138+
139+
/**
140+
* Swaps the values at the given indexes.
141+
*
142+
* @param index1 One of the two indexes needed to swap values in heap array.
143+
* @param index2 One of the two indexes needed to swap values in heap array.
144+
*/
145+
@SuppressWarnings("unchecked")
146+
private void heapifyFlip(int index1, int index2) {
147+
// insert code below this line
148+
149+
}
150+
151+
//====================================================================================================
152+
153+
/**
154+
* Clears the heap of all elements.
155+
*/
156+
public void clear() {
157+
// insert code below this line
158+
159+
}
160+
}

0 commit comments

Comments
 (0)