티스토리 뷰

알고리즘

priortyQueue (BST using array)

AKAC 2017. 11. 14. 21:34

http://www.edwith.org/datastructure-2017f/lecture/13960/


마찬가지


package priortyQueue;


public class BinarySearchTree {

public static Node[] bst;


public static void main(String[] args) {

bst = new Node[mypow(5)];

enqueue("김대중", 10);

enqueue("박근혜", 3);

enqueue("노무현", 9);

enqueue("이명박", 9);

enqueue("문재인", 2);

inorderPrintTree(1);

}


private static int mypow(int i) {

int returnInt = 1;

for (int j = 0; j < i; j++) {

returnInt *= 2;

}

return returnInt;

}


private static void enqueue(String name, int priority) {

tree(name, priority, 1);

}


private static void tree(String name, int priority, int i) {

while (bst[i] != null) {

if (bst[i].Priority < priority) {

i = 2 * i;

} else {

i = 2 * i + 1;

}

}


bst[i] = new Node(name, priority);


}


public static void inorderPrintTree(int i) {

if (bst[i] == null) {

return;

}


inorderPrintTree(i * 2);

System.out.print(bst[i]);

inorderPrintTree(i * 2 + 1);

}


public static class Node {

String Name;

int Priority;


public Node(String name, int priority) {

Name = name;

Priority = priority;

}


public String toString() {

return "[" + Name + ", " + Priority + "]";

}


}


}



'알고리즘' 카테고리의 다른 글

priortyQueue (linked list)  (0) 2017.11.14
java bufferedreader - 실행속도가 더 빠름  (0) 2017.03.19
1065 한수  (0) 2017.03.17
4673 셀프넘버  (0) 2017.03.17
4344 평균은 넘겠지  (0) 2017.03.17
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함