Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/main/java/basic/algorithm/AbstractTreeAlogorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package basic.algorithm;

public class AbstractTreeAlogorithm {

public static int[] field = {0,1,2,3,4,5,6,7,8,9,10};

public static void printAsBinaryTree() {
int n = field.length;
int height = (int) Math.ceil(Math.log(n + 1) / Math.log(2)); // 트리 높이
int maxNodeWidth = getMaxWidth(field); // 가장 긴 숫자의 자리수
int maxWidth = (int) Math.pow(2, height) * (maxNodeWidth + 2); // 총 가로 폭 (간격 포함)

int index = 0;

for (int level = 0; level < height; level++) {
int levelNodeCount = (int) Math.pow(2, level);
int spaceBetween = maxWidth / levelNodeCount; // 노드 사이 간격

// 시작 여백
System.out.print(" ".repeat(spaceBetween / 2 - maxNodeWidth / 2));

for (int i = 0; i < levelNodeCount && index < n; i++) {
String formatted = String.format("%" + maxNodeWidth + "d", field[index++]);
System.out.print(formatted);

// 간격
if (i != levelNodeCount - 1) {
System.out.print(" ".repeat(spaceBetween - maxNodeWidth));
}
}

System.out.println();
}
}

// 숫자 중 가장 긴 자리수 계산
private static int getMaxWidth(int[] arr) {
int maxLen = 0;
for (int num : arr) {
int len = String.valueOf(num).length();
if (len > maxLen) maxLen = len;
}
return maxLen;
}
}
46 changes: 3 additions & 43 deletions src/main/java/basic/algorithm/BFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,15 @@
import java.util.Queue;
import java.util.StringTokenizer;

public class BFS {
public static int[] field = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};

public static void printAsBinaryTree() {
int n = field.length;
int height = (int) Math.ceil(Math.log(n + 1) / Math.log(2)); // 트리 높이
int maxNodeWidth = getMaxWidth(field); // 가장 긴 숫자의 자리수
int maxWidth = (int) Math.pow(2, height) * (maxNodeWidth + 2); // 총 가로 폭 (간격 포함)

int index = 0;

for (int level = 0; level < height; level++) {
int levelNodeCount = (int) Math.pow(2, level);
int spaceBetween = maxWidth / levelNodeCount; // 노드 사이 간격

// 시작 여백
System.out.print(" ".repeat(spaceBetween / 2 - maxNodeWidth / 2));

for (int i = 0; i < levelNodeCount && index < n; i++) {
String formatted = String.format("%" + maxNodeWidth + "d", field[index++]);
System.out.print(formatted);

// 간격
if (i != levelNodeCount - 1) {
System.out.print(" ".repeat(spaceBetween - maxNodeWidth));
}
}

System.out.println();
}
}

// 숫자 중 가장 긴 자리수 계산
private static int getMaxWidth(int[] arr) {
int maxLen = 0;
for (int num : arr) {
int len = String.valueOf(num).length();
if (len > maxLen) maxLen = len;
}
return maxLen;
}
public class BFS extends AbstractTreeAlogorithm{

public static void main(String[] args){
printAsBinaryTree();
System.out.print("BFS : ");
bfs(0);
bfs();
}

private static void bfs(int index){
private static void bfs(){
Queue<Integer> queue = new LinkedList<>();
queue.add(0);

Expand Down
42 changes: 1 addition & 41 deletions src/main/java/basic/algorithm/DFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,7 @@

dfs 의 경우 0 1 3 4 5 6
*/
public class DFS {
public static int[] field = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};

public static void printAsBinaryTree() {
int n = field.length;
int height = (int) Math.ceil(Math.log(n + 1) / Math.log(2)); // 트리 높이
int maxNodeWidth = getMaxWidth(field); // 가장 긴 숫자의 자리수
int maxWidth = (int) Math.pow(2, height) * (maxNodeWidth + 2); // 총 가로 폭 (간격 포함)

int index = 0;

for (int level = 0; level < height; level++) {
int levelNodeCount = (int) Math.pow(2, level);
int spaceBetween = maxWidth / levelNodeCount; // 노드 사이 간격

// 시작 여백
System.out.print(" ".repeat(spaceBetween / 2 - maxNodeWidth / 2));

for (int i = 0; i < levelNodeCount && index < n; i++) {
String formatted = String.format("%" + maxNodeWidth + "d", field[index++]);
System.out.print(formatted);

// 간격
if (i != levelNodeCount - 1) {
System.out.print(" ".repeat(spaceBetween - maxNodeWidth));
}
}

System.out.println();
}
}

// 숫자 중 가장 긴 자리수 계산
private static int getMaxWidth(int[] arr) {
int maxLen = 0;
for (int num : arr) {
int len = String.valueOf(num).length();
if (len > maxLen) maxLen = len;
}
return maxLen;
}
public class DFS extends AbstractTreeAlogorithm{

public static void main(String[] args){
printAsBinaryTree();
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/basic/algorithm/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package basic.algorithm;

public class Order extends AbstractTreeAlogorithm {

public static void main(String[] args){
printAsBinaryTree();

System.out.print("preOrder : ");
preOrder(0);
System.out.print("\ninOrder : ");
inOrder(0);
System.out.print("\npostOrder : ");
postOrder(0);
}

private static void preOrder(int index){

int left = index *2+1;
int right = index*2+2;

System.out.print(field[index]+" -> ");

if(left < field.length){
preOrder(left);
}
if(right < field.length){
preOrder(right);
}

}


private static void inOrder(int index){

int left = index *2+1;
int right = index*2+2;

if(left < field.length){
inOrder(left);
}

System.out.print(field[index]+" -> ");
if(right < field.length){
inOrder(right);
}
}



private static void postOrder(int index){

int left = index *2+1;
int right = index*2+2;


if(left < field.length){
postOrder(left);
}

if(right < field.length){
postOrder(right);
}

System.out.print(field[index]+" -> ");
}

}
4 changes: 4 additions & 0 deletions src/main/java/basic/datastructure/list/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public ArrayList(List<T> array){
this.size = array.getSize();
}

public int getCapacity(){
return array.length;
}

@Override
public T get(int index) {
checkOutBoundsIndex(index);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/basic/datastructure/map/AbstractMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public abstract class AbstractMap<K,V> implements Map<K,V>{


protected void checkNullKey(K key){
if(key==null) throw new NullPointerException();
}
Expand Down
Loading