Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[4주차] DS_트리 & 그래프 #15

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

Merged
EunjiShin merged 11 commits into TecheerB:master from Gnu-Kenny:master
Nov 29, 2021
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
50 changes: 50 additions & 0 deletions GnuPark/DS/Trees & Graphs/Binary_Heaps.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Min Heap

```
1 5
/ \ / \
2 3 4 3
/ \ / \
4 5 1 2
Min Heap Max Heap
```

- Heap

- 최대 혹은 최소 값을 빠르게 찾기 위해 구현된 완전 이진트리를 기본으로한 자료구조

- Min Heap

- Root Node가 가장 작은 값이 되도록한 트리

- Max Heap

- 가장 큰 값이 맨 위에 오도록 자기 부모 노드에 자기 값보다 큰값을 두게 한 트리

## 최소 힙에 노드 삽입하기

1. 완전 트리에 맨끝에 노드를 추가

2. 자신의 부모 노드와 비교해 자기 값이 작을 경우 부모 노드와 값 변경

3. 2번 반복

### 시간 복잡도

- O(logn)

- 한 레벨에 절반씩 떨어짐

## 최소 힙에 노드 꺼내오기

1. 최소 힙에서 노드를 요청할땐 가장 작은 값인 루트를 꺼내온다.

2. 루트 자리에 가장 마지막 노드를 넣는다.

3. 자식 노드와 비교했을 때 더 작은 자식 노드의 값과 자리 교체

4. 3번 반복

### 시간 복잡도

- O(logn)
55 changes: 55 additions & 0 deletions GnuPark/DS/Trees & Graphs/DFS_BFS.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# DFS, BFS

## DFS

- Depth-First Search (DFS)

- inorder

- preorder

- postorder

- 마지막 노드와 만날때까지 Child Node 순회

- Stack으로 구현

### 구현 순서

1. 스택을 하나 만든다.

2. 처음에는 시작할 노드를 넣는다.

3. 스택에서 노드를 하나 꺼내서 해당 노드의 자식노드를 스택에 추가

4. 꺼낸 노드는 출력

5. 한번 스택에 담았던 노드는 다시 담지 않는다.

### DFS: Recursion

1. 시작 노드로 함수를 호출한다.

2. 함수를 호출하면 자식 노드를 호출하기 전에 자기 자신을 출력한다.

3.

## BFS

- Breath-First Search (BFS)

- Level 단위로 순회

- Queue로 구현

### 구현 순서

1. 큐를 하나 만든다.

2. 처음에는 시작할 노드를 넣는다.

3. 큐에서 노드를 하나 꺼내서 해당 노드의 자식노드를 큐에 추가

4. 꺼낸 노드는 출력

5. 한번 큐에 담았던 노드는 다시 담지 않는다.
186 changes: 186 additions & 0 deletions GnuPark/DS/Trees & Graphs/bfs_dfs.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Stack;
import java.util.NoSuchElementException;

class Queue<T> {

class Node<T> {
private T data;
private Node<T> next;

public Node(T data) {
this.data = data;
}
}

private Node<T> first;
private Node<T> last;

public void enqueue(T item) {
Node<T> t = new Node<T>(item);

if (first == null) {
first = last;
}
last = t;
if (first == null) {
first = last;
}
}

public T dequeue() {
if (first == null) {
throw new NoSuchElementException();
}
T data = first.data;

first = first.next;

if (first == null) {
last = null;
}
return data;
}

public T peek() {
if (first == null) {
throw new NoSuchElementException();
}
return first.data;
}

public boolean isEmpty() {
return first == null;
}
}

class Graph {
class Node {
int data;
LinkedList<Node> adjacent;
boolean marked;

Node(int data) {
this.data = data;
this.marked = false;
adjacent = new LinkedList<Node>();
}
}

Node[] nodes;

Graph(int size) {
nodes = new Node[size];
for (int i = 0; i < size; i++) {
nodes[i] = new Node(i);
}
}

// 노드의 관계를 저장
void addEdge(int i1, int i2) {
Node n1 = nodes[i1];
Node n2 = nodes[i2];
if (!n1.adjacent.contains(n2)) {
n1.adjacent.add(n2);
}
if (!n2.adjacent.contains(n1)) {
n2.adjacent.add(n1);
}
}

// bfs를 인자 없이 시작하면 0번부터 시작
void dfs() {
dfs(0);
}

void dfs(int index) {
Node root = nodes[index];
Stack<Node> stack = new Stack<Node>();
stack.push(root);
root.marked = true;
while (!stack.isEmpty()) {
Node r = stack.pop();
for (Node n : r.adjacent) {
// 가져온 노드의 자식들 중 스택에 들어가지 않은 노드들만 스택에 추가
if (n.marked == false) {
n.marked = true;
stack.push(n);
}
}
visit(r);
}
}

void bfs() {
bfs(0);
}

void bfs(int index) {
Node root = nodes[index];
Queue<Node> queue = new Queue<Node>();
queue.enqueue(root);
root.marked = true;
// 큐가 비어있을때까지 반복 작업
while (!queue.isEmpty()) {
Node r = queue.dequeue();
// 큐에서 꺼낸 노드의 자식 노드들을 큐에 추가
for (Node n : r.adjacent) {
if (n.marked == false) {
n.marked = true;
queue.enqueue(n);
}
}
// 가지고 나온 노드는 출력
visit(r);
}
}

// 재귀 호출시 링크드리스트가 노드의 주소를 가지고 있기 때문에 재귀함수는 노드를 받는 형태가 되어야함.
void dfsR(Node r) {
if (r == null)
return;
r.marked = true;
visit(r);
for (Node n : r.adjacent) {
if (n.marked == false) {
dfsR(n);
}
}
}

// 시작 노드를 다양하게 받기 위해 배열방의 인덱스를 인자로 받는 형태 구현
void dfsR(int index) {
Node r = nodes[index];
dfsR(r);
}

void dfsR() {
dfsR(0);
}

// 방문할때 출력하는 함수
void visit(Node n) {
System.out.print(n.data + " ");
}
}

/*
* ------------------------------- 0 / 1 -- 3 7 | / | \ / | / | 5 2 -- 4 \ 6 - 8
*/
public class dfs_bfs {
public static void main(String[] args) {
Graph g = new Graph(9);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(2, 4);
g.addEdge(3, 4);
g.addEdge(3, 5);
g.addEdge(5, 6);
g.addEdge(5, 7);
g.addEdge(6, 8);
g.dfsR();

}
}
67 changes: 67 additions & 0 deletions GnuPark/DS/Trees & Graphs/binary_tree.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//Inorder (Left, Root, Right): 4 2 5 1 3
//Preorder (Root, Left, Right): 1 2 4 5 3
//Postorder (Left, Right, Root): 4 5 2 3 1

class Node {
int data;
Node left;
Node right;
}

class Tree {
public Node root;

public void setRoot(Node node) {
this.root = node;
}

public Node getroot() {
return root;
}

public Node makeNode(Node left, int data, Node right) {
Node node = new Node();
node.data = data;
node.left = left;
node.right = right;
return node;
}

public void inorder(Node node) {
if (node != null) {
inorder(node.left);
System.out.println(node.data);
inorder(node.right);
}
}

public void preorder(Node node) {
if (node != null) {
System.out.println(node.data);
preorder(node.left);
preorder(node.right);
}
}

public void postorder(Node node) {
if (node != null) {
postorder(node.left);
postorder(node.right);
System.out.println(node.data);
}
}
}

public class binary_tree {
public static void main(String[] args) {
Tree t = new Tree();
Node n4 = t.makeNode(null, 4, null);
Node n5 = t.makeNode(null, 5, null);
Node n2 = t.makeNode(n4, 2, n5);
Node n3 = t.makeNode(null, 3, null);
Node n1 = t.makeNode(n2, 1, n3);
t.setRoot(n1);
t.inorder(t.getroot());

}
}
File renamed without changes.
File renamed without changes.
Loading

AltStyle によって変換されたページ (->オリジナル) /