2022년 10월 16일 코딩 스터디를 진행하며 이진트리의 방식을 직접 구현한 것입니다.
public class BinaryTree {
public static void main(String[] args) {
int[] num = {3,5,7,8,9,2,4,1,6};
Tree t = new Tree();
t.makeTree(num);
}
}
class Tree{
class Node{
int data;
Node left;
Node right;
Node(int data){
data = this.data;
}
}
Node root;
public void makeTree(int[] num){
root = makeTreeR(num, 0, num.length-1);
}
public Node makeTreeR(int[]num, int start, int end){
if(start > end) return null;
int mid = (start + end) / 2;
Node node = new Node(num[mid]);
node.left = makeTreeR(num, 0, mid-1);
node.right = makeTreeR(num, mid+1, end);
return node;
}
}
'알고리즘 > 구현' 카테고리의 다른 글
알고리즘_MinHeap(최소힙) (1) | 2022.11.10 |
---|---|
알고리즘_ArrayList (0) | 2022.11.10 |
알고리즘_LinkedList(리스트) (0) | 2022.11.10 |
알고리즘_queue(선형) (1) | 2022.10.04 |