๐ป Language/Java : ์๋ฐ
[Java] ์๋ฃ๊ตฌ์กฐ - ๋ฐฐ์ด, ์ฐ๊ฒฐ๋ฆฌ์คํธ, ์คํ, ํ, ํ, ํธ๋ฆฌ, ๊ทธ๋ํ, ํด์
mxnxeonx
2024. 11. 12. 20:25
728x90
728x90
1. ์๋ฃ๊ตฌ์กฐ
1) ์๋ฃ๊ตฌ์กฐ์ ์ ์
- ์๋ฃ๊ตฌ์กฐ(Data Structure)๋ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ์ ์ฅํ๊ณ ๊ด๋ฆฌํ์ฌ ํ์ํ ์์ ์ ๋น ๋ฅด๊ฒ ์ํํ ์ ์๊ฒ ํ๋ ๊ตฌ์กฐ
- ๋ฐ์ดํฐ์ ์กฐ์, ์ ๊ทผ ๋ฐฉ์์ ์ค๊ณํ๋ ์ค์ํ ๊ฐ๋
์ผ๋ก ๋ฐ์ดํฐ์ ์ฑ๊ฒฉ๊ณผ ์ฌ์ฉ ๋ชฉ์ ์ ๋ฐ๋ผ ๋ค์ํ ํํ๋ก ๊ตฌํ
- 'ํจ์จ์ ์ด๋ค' : ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐ ํ์ํ ์๊ฐ๊ณผ ๊ณต๊ฐ์ ์ต์ํํ๋ ๊ฒ
๋๋ณด๊ธฐ
์๋ฃ์ ํํ์ ๋ฐ๋ผ ๋ค์๊ณผ ๊ฐ์ด ๋ถ๋ฅํ ์ ์๋ค.
- ๋จ์ ๊ตฌ์กฐ : ๊ธฐ๋ณธ ์๋ฃํ ์ ์, ์ค์, ๋ฌธ์, ๋ฌธ์์ด ๋ฑ
- ์ ํ ๊ตฌ์กฐ : ์๋ฃ๋ค ๊ฐ์ ์๋ค ๊ด๊ณ๊ฐ 1:1์ ์ ํ ๊ด๊ณ๋ฅผ ๊ฐ์ง๋ ์๋ฃํ ๋ฆฌ์คํธ, ์ฐ๊ฒฐ๋ฆฌ์คํธ, ์คํ, ํ, ๋ฐํฌ ๋ฑ
- ๋น์ ํ ๊ตฌ์กฐ : ์๋ฃ๋ค ๊ฐ์ ์๋ค ๊ด๊ณ๊ฐ 1:๋ค, ๋ค:๋ค์ ๊ด๊ณ๋ฅผ ๊ฐ์ง๋ ์๋ฃํ ํธ๋ฆฌ, ๊ทธ๋ํ ๋ฑ
- ํ์ผ ๊ตฌ์กฐ : ๋ ์ฝ๋์ ์งํฉ์ธ ํ์ผ์ ๋ํ ๊ตฌ์กฐ ์์ฐจํ์ผ, ์์ธํ์ผ, ์ง์ ํ์ผ ๋ฑ
2) ์๋ฃ๊ตฌ์กฐ์ ์ข ๋ฅ
(1) ๋ฐฐ์ด (Array)
- ๊ณ ์ ๋ ํฌ๊ธฐ์ ์ฐ์์ ์ธ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๊ฐ์ ์ ํ์ ๋ฐ์ดํฐ๋ฅผ ์์ฐจ์ ์ผ๋ก ์ ์ฅํ๋ ์๋ฃ๊ตฌ์กฐ
- ๊ฐ ์์๋ ์ธ๋ฑ์ค๋ฅผ ํตํด ์ ๊ทผํ ์ ์์ผ๋ฉฐ, ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ ์๋๊ฐ ๋น ๋ฆ
- ํฌ๊ธฐ๊ฐ ๊ณ ์ ๋์ด ์์ด ํฌ๊ธฐ๋ฅผ ๋์ ์ผ๋ก ์กฐ์ ํ๊ธฐ ์ด๋ ค์
- ๋ฌผ๋ฆฌ์ ์ฐ์์ฑ, ์ ์ ๊ตฌ์กฐ, ์ธ๋ถ๋จํธํO, ๊ฒ์ ์ฐ์ฐโฒ, ๊ธฐ๋ก ๋ฐ๋โผ, ์์ ์ฐ์ฐโฒ, ์ฝ์ /์ญ์ ์ฐ์ฐโผ
public class ArrayExample {
public static void main(String[] args) {
int[] array = new int[5]; // ๋ฐฐ์ด ํฌ๊ธฐ 5
array[0] = 10;
array[1] = 20;
array[2] = 30;
// ๋ฐฐ์ด ์์ ์ถ๋ ฅ
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
(2) ์ฐ๊ฒฐ๋ฆฌ์คํธ (Linked List)
- ์์๋ค์ด ๊ฐ๊ฐ ๋ ธ๋(node)๋ก ๊ตฌ์ฑ๋๋ฉฐ, ๊ฐ ๋ ธ๋๋ ๋ค์ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ๋ฅผ ํตํด ์๋ก ์ฐ๊ฒฐ๋ ์๋ฃ๊ตฌ์กฐ
- ์ฝ์ ๊ณผ ์ญ์ ๊ฐ ์ฉ์ดํ๋ฉฐ, ๋ฐฐ์ด๊ณผ ๋ฌ๋ฆฌ ๊ฐ ์์๊ฐ ๋ฉ๋ชจ๋ฆฌ ์์ ์์์ ์์น์ ์ ์ฅ๋๊ณ , ํฌ๊ธฐ๋ฅผ ์ ๋์ ์ผ๋ก ์กฐ์ ํ ์ ์์
- ํน์ ์์์ ์ ๊ทผํ๊ธฐ ์ํด ์์ฐจ์ ์ผ๋ก ํ์ํด์ผ ํ๋ฏ๋ก ๊ฒ์ ์๋๊ฐ ์ ํ๋ ์ ์์
- ๋ ผ๋ฆฌ์ ์ฐ์์ฑ, ๋์ ๊ตฌ์กฐ, ์ธ๋ถ๋จํธํX, ๊ฒ์ ์ฐ์ฐโผ, ๊ธฐ๋ก ๋ฐ๋โผ, ์์ ์ฐ์ฐโฒ, ์ฝ์ /์ญ์ ์ฐ์ฐโผ
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
public void printList() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
public class LinkedListExample {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(10);
list.add(20);
list.add(30);
list.printList();
}
}
(3) ์คํ (Stack)
- ํ์ ์ ์ถ(LIFO; Last In, First Out) ๋ฐฉ์์ ์๋ฃ๊ตฌ์กฐ
- ๊ฐ์ฅ ๋ง์ง๋ง์ ์ถ๊ฐ๋ ์์๊ฐ ๊ฐ์ฅ ๋จผ์ ์ ๊ฑฐ๋จ, ์์๋ฅผ ์ถ๊ฐ(push)ํ๊ณ ์ ๊ฑฐ(pop)ํ๋ ์ฐ์ฐ๋ง ๊ฐ๋ฅ
- ํจ์ ํธ์ถ์ ๊ด๋ฆฌ, ์คํ ์ทจ์(undo), ๊ดํธ ๊ฒ์ฌ, ํ์ ํ๊ธฐ๋ฒ ๋ฑ์ ์ด์ฉ
import java.util.EmptyStackException;
class Stack {
private int[] stack;
private int top;
public Stack(int size) {
stack = new int[size];
top = -1;
}
public void push(int value) {
if (top == stack.length - 1) {
System.out.println("Stack is full");
return;
}
stack[++top] = value;
}
public int pop() {
if (top == -1) {
throw new EmptyStackException();
}
return stack[top--];
}
public int peek() {
if (top == -1) {
throw new EmptyStackException();
}
return stack[top];
}
}
public class StackExample {
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(10);
stack.push(20);
System.out.println(stack.pop()); // 20
System.out.println(stack.peek()); // 10
}
}
(4) ํ (Queue)
- ์ ์ ์ ์ถ(FIFO; First In First Out) ๋ฐฉ์์ ์๋ฃ๊ตฌ์กฐ
- ๊ฐ์ฅ ๋จผ์ ๋ค์ด์จ ์์๊ฐ ๊ฐ์ฅ ๋จผ์ ์ ๊ฑฐ๋จ. ๋ฐ์ดํฐ ๋ฃ๊ธฐ(enqueue), ๊บผ๋ด๊ธฐ(dequeue)
- ๋๋น ์ฐ์ ํ์, ๋๊ธฐ์ด ๊ด๋ฆฌ, ์์ (CPU) ์ค์ผ์ค๋ง, ๋ฐ์ดํฐ ์คํธ๋ฆฌ๋ฐ ๋ฑ์ ์ด์ฉ
class Queue {
private int[] queue;
private int front;
private int rear;
private int size;
public Queue(int size) {
queue = new int[size];
front = -1;
rear = -1;
this.size = size;
}
public void enqueue(int value) {
if (rear == size - 1) {
System.out.println("Queue is full");
return;
}
queue[++rear] = value;
if (front == -1) front = 0;
}
public int dequeue() {
if (front == -1 || front > rear) {
System.out.println("Queue is empty");
return -1;
}
return queue[front++];
}
}
public class QueueExample {
public static void main(String[] args) {
Queue queue = new Queue(5);
queue.enqueue(10);
queue.enqueue(20);
System.out.println(queue.dequeue()); // 10
System.out.println(queue.dequeue()); // 20
}
}
(5) ํ (Queue)
- ์์ ์ด์ง ํธ๋ฆฌ ํํ์ ์๋ฃ ๊ตฌ์กฐ๋ก, ์ฐ์ ์์ ํ(Priority Queue)๋ฅผ ๊ตฌํํ๊ธฐ ์ํด ์ฌ์ฉ๋จ
// ์ต์ ํ ์์
import java.util.PriorityQueue;
public class HeapExample {
public static void main(String[] args) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
minHeap.add(20);
minHeap.add(10);
minHeap.add(30);
System.out.println(minHeap.poll()); // 10
System.out.println(minHeap.poll()); // 20
}
}
(6) ๊ทธ๋ํ (Graph)
- ๊ฐ์ฒด(๋ ธ๋ ๋๋ ์ ์ )์ ๊ฐ์ฒด ๊ฐ์ ๊ด๊ณ(์ฃ์ง ๋๋ ๊ฐ์ )๋ฅผ ํํํ๋ ๋น์ ํ ์๋ฃ๊ตฌ์กฐ
- ์ํ์ ์ธ ๊ตฌ์กฐ(์ฌ์ดํด ํ์ฉ)๋ฅผ ๊ฐ์ง ์ ์๊ณ , ๋ฐฉํฅ์ ๊ฐ์ง ์๋ ์์
- ์ฌํ/๋๋ก ๋คํธ์ํฌ, ์น ํ์ด์ง์ ๋งํฌ ๊ตฌ์กฐ, ์์ ๋ฏธ๋์ด, ์ง๋ ํ์ ๋ฑ ๋ณต์กํ ๊ด๊ณ๋ฅผ ํํํ ๋ ์ ์ฉ
// ์ธ์ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํ ๊ทธ๋ํ ๊ตฌํ
import java.util.ArrayList;
import java.util.List;
class Graph {
private List<List<Integer>> adjList;
public Graph(int vertices) {
adjList = new ArrayList<>(vertices);
for (int i = 0; i < vertices; i++) {
adjList.add(new ArrayList<>());
}
}
public void addEdge(int src, int dest) {
adjList.get(src).add(dest);
adjList.get(dest).add(src); // ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ
}
public void printGraph() {
for (int i = 0; i < adjList.size(); i++) {
System.out.print("Vertex " + i + ":");
for (int edge : adjList.get(i)) {
System.out.print(" -> " + edge);
}
System.out.println();
}
}
}
public class GraphExample {
public static void main(String[] args) {
Graph graph = new Graph(3);
graph.addEdge(0, 1);
graph.addEdge(1, 2);
graph.addEdge(2, 0);
graph.printGraph();
}
}
(7) ํธ๋ฆฌ (Tree)
- ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ํํํ๋ ๋น์ ํ ์๋ฃ๊ตฌ์กฐ๋ก, ๊ฐ ๋ ธ๋๊ฐ ๋ถ๋ชจ-์์ ๊ด๊ณ๋ก ์ฐ๊ฒฐ๋์ด ์์
- ๋ฃจํธ(root) ๋ ธ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ฐ ๋ ธ๋๊ฐ ์์์ ๊ฐ์ง ์ ์์ผ๋ฉฐ, ํ ๊ฐ์ ๋ถ๋ชจ ๋ ธ๋๋ฅผ ๊ฐ์ง(๋ฃจํธ ๋ ธ๋ ์ ์ธ)
- ๋ฐ์ดํฐ์ ๊ณ์ธต์ ๊ตฌ์กฐ๋ฅผ ํํํ๊ณ ๊ฒ์/์ฝ์ /์ญ์ ์ฐ์ฐ์ ํจ์จ์ ์ผ๋ก ์ํํ ์ ์์
- ์ด์ง ๊ฒ์ ํธ๋ฆฌ, AVL ํธ๋ฆฌ, B-ํธ๋ฆฌ, ๋ ๋-๋ธ๋ ํธ๋ฆฌ ๋ฑ ๋ค์ํ ๋ณํ์ด ์กด์ฌ
- ๊ทธ๋ํ์ ํน์ํ ํํ๋ก ์ฌ์ดํด์ ํ์ฉํ์ง ์๋๋ค.
- ํ์ผ ์์คํ , HTML ๋ฌธ์์ DOM, ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ด๋ฆฌ ๋ฑ์ ์ฌ์ฉ
// ์ด์ง ํธ๋ฆฌ
class TreeNode {
int data;
TreeNode left, right;
public TreeNode(int data) {
this.data = data;
left = right = null;
}
}
class BinaryTree {
TreeNode root;
public void preorder(TreeNode node) {
if (node != null) {
System.out.print(node.data + " ");
preorder(node.left);
preorder(node.right);
}
}
}
public class TreeExample {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.root = new TreeNode(10);
tree.root.left = new TreeNode(20);
tree.root.right = new TreeNode(30);
tree.preorder(tree.root); // 10 20 30
}
}
(8) ํด์ (Hash)
- ํค(Key)๋ฅผ ์ ๋ ฅ ๋ฐ์ ํด์ ํจ์๋ฅผ ํตํด ๊ฐ์ ๋น ๋ฅด๊ฒ ๊ฒ์ํ ์ ์๋ ์๋ฃ๊ตฌ์กฐ
- ํค๋ฅผ ํด์ ํ ์ด๋ธ์ ์ธ๋ฑ์ค๋ก ๋ณํํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ฑฐ๋ ์ฐพ์
- ์ถฉ๋(collision) ํด๊ฒฐ ๊ธฐ๋ฒ : ํด์ ํจ์์ ์ถ๋ ฅ์ด ๊ฐ์(๊ฐ์ ํด์ ๊ฐ) ์ถฉ๋์ด ๋ฐ์ํ๋ ๊ฒฝ์ฐ
- ๊ฐ๋ณ ์ฒด์ด๋, ๊ฐ๋ฐฉ ์ฃผ์๋ฒ, ์ด์ฐจ ํด์, ํด์ฑ ํ์ฒด์ด๋ ๋ฑ
- ํด์ ๋งต(hash map) ๋๋ ๋์
๋๋ฆฌ(dictionary)๋ก ๊ตฌํ
- ํด์ ๋งต : ํค-๊ฐ ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ์๋ฃ๊ตฌ์กฐ. ๊ฐ ํค๋ ํด์ ํจ์๋ฅผ ํตํด ๊ณ ์ ์ ์ธ๋ฑ์ค๋ก ๋งคํ๋์ด ๋น ๋ฅด๊ฒ ๋ฐ์ดํฐ์ ์ ๊ทผํ ์ ์์ (Java์ HashMap)
- ๋์ ๋๋ฆฌ : ํค-๊ฐ ์์ผ๋ก ์ด๋ฃจ์ด์ ธ ์๊ณ ๋ด๋ถ์ ์ผ๋ก ํด์ ํ ์ด๋ธ๋ก ๊ตฌํ๋จ (Python์ dict)
- ๋ฐ์ดํฐ์ ๋น ๋ฅธ ๊ฒ์๊ณผ ์ฝ์ ์ ๋งค์ฐ ํจ์จ์
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("Alice", 30);
map.put("Bob", 25);
System.out.println(map.get("Alice")); // 30
System.out.println(map.containsKey("Bob")); // true
System.out.println(map.remove("Alice")); // 30
}
}
728x90
320x100