Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[자료구조] Array & ArrayList & LinkedList #115

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
69 changes: 69 additions & 0 deletions 자료구조/Array&ArrayList&LinkedList.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
## Array

![image.png](./images/Array&ArrayList&LinkedList/1.png)

- 데이터타입 배열이름[배열길이]
- 배열을 선언함과 동시에 데이터 타입과 길이가 결정됨
- 저장할 데이터의 최대 수를 예상하기 어렵거나 길이를 가변적으로 변경해야할 필요가 있으면 적합하지 않음
- 인덱스를 통해 원하는 값을 빠르게 검색할 수 있음

## ArrayList

![image.png](./images/Array&ArrayList&LinkedList/2.png)

- ArrayList<데이터타입> 이름 = new ArrayList<데이터타입>(초기용량);
- ArrayList는 내부적으로 데이터를 배열에서 관리
- 배열에 더 이상 저장할 공간이 없으면 자체적으로 배열의 크기를 늘려 처리
- 인덱스를 통해 데이터 접근이 가능

## LinkedList

![image.png](./images/Array&ArrayList&LinkedList/3.png)

```java
class Node {
Node* next;
int data;
}
```

- 불연속적으로 저장된 데이터를 서로 연결한 형태로 구성되어있는 자료구조
- 각 node들은 자신과 연결된 다음 요소에 대한 주소값과 데이터로 구성되어있음
- 크기가 가변적
- 데이터가 불연속적으로 저장 → 원하는 데이터에 접근할 때 처음부터 순차적으로 검색해야 함

## 자료구조의 삽입과 삭제

| | Array | ArrayList | LinkedList |
| --- | --- | --- | --- |
| 순차적인 데이터 추가 | 빠름 | 빠름 | 빠름 |
| 순차적인(끝에서부터) 데이터 삭제 | 빠름 | 빠름 | 빠름 |
| 비순차적인(배열의 중간)
데이터 추가 및 삭제 | 느림 | 느림 | 빠름 |

### Array

- 중간 데이터 추가/삭제는 기존의 데이터들을 복사해서 새로운 배열에 저장하는 과정이 필요하기 때문에 속도가 느림
- 크기를 변경하려면 새로운 배열을 생성해서 데이터를 복사해야함
- 새로운 배열 생성을 막기 위해 충분히 큰 크기의 배열을 생성하면 메모리 낭비

### ArrayList

- 순서와 관계없이 데이터를 추가하고 삭제할 수 있는 기능을 제공
- 중간 순서 데이터 추가/삭제시 다른 데이터들이 자리를 이동해야하는 것은 Array와 동일
- 크기가 가변적이라는 점 외에는 Array와 유사

### LinkedList

- 데이터 추가 및 삭제는 이전 순서와 다음 순서의 노드만 조정해주면 되어서 빠름
- 인덱스를 통한 데이터 접근이 불가능해 추가하거나 삭제할 순서까지 접근하는데 많은 시간이 소요됨

## 질문

1. Array의 중간 삽입 속도가 느린 이유는?
2. ArrayList는 더이상 저장할 공간이 없으면 어떻게 되는가?
3. LinkedList의 단점은 무엇인가?

## 참고

[https://velog.io/@letskuku/자료구조-Array-vs-ArrayList-vs-LinkedList](https://velog.io/@letskuku/%EC%9E%90%EB%A3%8C%EA%B5%AC%EC%A1%B0-Array-vs-ArrayList-vs-LinkedList)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 자료구조/images/trie/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 자료구조/images/trie/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 자료구조/images/trie/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 자료구조/images/trie/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 자료구조/images/trie/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 101 additions & 0 deletions 자료구조/트라이.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
## 트라이(Trie)란?

### 트리(Tree)

![image.png](./images/trie/1.png)

- 노드와 에지로 구성되는 계층 관계를 나타내는 자료구조
- 구성 : 노드, 엣지, 루트(가장 상위 노드), 리프(가장 마지막 노드)
- 종류 : 바이너리 트리, ***트라이*** 등

### 트라이(Trie)

![image.png](./images/trie/2.png)

- retrieval(검색) tree에서 나옴
- Prefix(접두사) Tree, digital search tree, retrieval tree라고도 부름
- 문자열을 Key로 사용하는 동적인 Set 또는 연관 배열을 저장하는 트리의 확장 구조
- 문자열을 저장하고 효율적으로 탐색하기 위해 사용
- 검색할 때 볼 수 있는 자동완성 기능, 사전 검색 등에 특화되어있음

### 트라이 장단점

- 문자열 검색을 빠르게 함
- 문자열 탐색시 하나하나씩 전부 비교해서 탐색하는 것보다 시간복잡도가 낮음
- 각 노드에서 자식들에 대한 포인터들을 배열로 모두 저장하고 있기때문에 저장 공간을 많이 차지함

### 트라이 구조 : ‘abc’, ‘ab’, ‘car’를 트라이에 저장하기

- 기본적으로 data는 None으로 구성
- 단어가 끝날 때 data에 해당 단어를 저장

![image.png](./images/trie/3.png)

![image.png](./images/trie/4.png)

## 트라이 구현 - python

```python
class Node(object):
def __init__(self, key, data=None):
self.key = key
self.data = data
self.children = {}
```

![image.png](./images/trie/5.png)

```python
class Trie(object):
def __init__(self):
self.head = Node(None)

# 문자열 삽입
def insert(self, string):
curr_node = self.head

# 삽입할 String 각각의 문자에 대해 자식Node를 만들며 내려간다.
for char in string:
# 자식Node들 중 같은 문자가 없으면 Node 새로 생성
if char not in curr_node.children:
curr_node.children[char] = Node(char)

# 같음 문자가 있으면 노드를 따로 생성하지 않고, 해당 노드로 이동
curr_node = curr_node.children[char]

# 문자열이 끝난 지점의 노드의 data값에 해당 문자열을 표시
curr_node.data = string

# 문자열이 존재하는지 탐색!
def search(self, string):
# 가장 아래에 있는 노드에서부터 탐색 시작한다.
curr_node = self.head

for char in string:
if char in curr_node.children:
curr_node = curr_node.children[char]
else:
return False

# 탐색이 끝난 후에 해당 노드의 data값이 존재한다면
# 문자가 포함되어있다는 뜻이다!
if curr_node.data is not None:
return True
```

- 생성 시간 복잡도 : O(ML)
- 탐색 시간 복잡도 : O(L)
- M : 총 문자열들의 수 / L : 제일 긴 문자열의 길이

---
## 질문

1. 트라이는 어디에 사용되는가?
2. 트라이의 장단점은?
3. 트라이의 탐색 시간 복잡도는 왜 O(L)일까?

## 참고

https://brunch.co.kr/@springboot/75

[https://velog.io/@kimdukbae/자료구조-트라이-Trie](https://velog.io/@kimdukbae/%EC%9E%90%EB%A3%8C%EA%B5%AC%EC%A1%B0-%ED%8A%B8%EB%9D%BC%EC%9D%B4-Trie)