From dd0f3702b1e92840a1a14a89e2b8086163f7f48e Mon Sep 17 00:00:00 2001 From: gyuseon Date: Tue, 28 Jan 2025 16:49:24 +0900 Subject: [PATCH] =?UTF-8?q?Section7-7.=20=EC=9D=B4=EC=A7=84=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=EC=88=9C=ED=9A=8C(BFS)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\246\254\354\210\234\355\232\214_BFS.java" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "Section07/\354\235\264\354\247\204\355\212\270\353\246\254\354\210\234\355\232\214_BFS.java" diff --git "a/Section07/\354\235\264\354\247\204\355\212\270\353\246\254\354\210\234\355\232\214_BFS.java" "b/Section07/\354\235\264\354\247\204\355\212\270\353\246\254\354\210\234\355\232\214_BFS.java" new file mode 100644 index 0000000..1566f09 --- /dev/null +++ "b/Section07/\354\235\264\354\247\204\355\212\270\353\246\254\354\210\234\355\232\214_BFS.java" @@ -0,0 +1,50 @@ +package 인프런.Section07; + +import java.util.LinkedList; +import java.util.Queue; + +class NodeBFS { + int data; + NodeBFS lt, rt; + public NodeBFS(int val) { + data = val; + lt=rt=null; + } +} + +public class 이진트리순회_BFS { + NodeBFS root; + + public void BFS(NodeBFS root) { + Queue q = new LinkedList<>(); + q.offer(root); + int L = 0; //레벨 + while(!q.isEmpty()) { + int len = q.size(); + System.out.print(L + " : "); + for(int i = 0; i < len; i++) { + NodeBFS cur = q.poll(); + System.out.print(cur.data + " "); + if(cur.lt != null) q.offer(cur.lt); + if(cur.rt != null) q.offer(cur.rt); + } + L++; + System.out.println(); + } + } + + public static void main(String[] args) { + 이진트리순회_BFS tree = new 이진트리순회_BFS(); + + tree.root = new NodeBFS(1); + tree.root.lt = new NodeBFS(2); + tree.root.rt = new NodeBFS(3); + tree.root.lt.lt = new NodeBFS(4); + tree.root.lt.rt = new NodeBFS(5); + tree.root.rt.lt = new NodeBFS(6); + tree.root.rt.rt = new NodeBFS(7); + + tree.BFS(tree.root); + + } +}