{"id":2451,"date":"2016-10-02T09:02:15","date_gmt":"2016-10-02T09:02:15","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=2451"},"modified":"2017-01-27T09:41:31","modified_gmt":"2017-01-27T00:41:31","slug":"golang-heap-%ec%9e%90%eb%a3%8c%ea%b5%ac%ec%a1%b0","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=2451","title":{"rendered":"[Golang] Heap \uc790\ub8cc\uad6c\uc870"},"content":{"rendered":"<p>go\uc5d0\uc11c \ud328\ud0a4\uc9c0 \ub77c\uc774\ube0c\ub7ec\ub9ac\ub85c \uc81c\uacf5\ud558\ub294 \ud799 \uc790\ub8cc\uad6c\uc870\uac00 \uc788\uc2b5\ub2c8\ub2e4. \uc81c\uacf5\ub418\ub294 \ud799 \uc790\ub8cc\uad6c\uc870\ub294 \ubd80\ubaa8 \ub178\ub4dc\uc758 \uac12\uc740 \uc790\uc2dd \ub178\ub4dc\uc758 \uac12\ubcf4\ub2e4 \ubc18\ub4dc\uc2dc \uc791\uac70\ub098 \uac19\ub2e4\ub77c\ub294 \uc870\uac74\uc744 \ucda9\uc871\ud574\uc57c \ud569\ub2c8\ub2e4. \uc774\ub7ec\ud55c \uc870\uac74 \ucda9\uc871\uc744 \ube44\uad50 \ub9e4\uc11c\ub4dc\uc640 \ud799 \uc790\ub8cc\uad6c\uc870\uc5d0 \uc694\uc18c\ub97c \ucd94\uac00(Push)\ud558\uace0 \uac00\uc838\uc624\ub294(Pop) \ub9e4\uc11c\ub4dc \ub4f1\uc744 \uc81c\uacf5\ud558\ub294 heap.Interface \uc778\ud130\ud398\uc774\uc2a4\ub97c \uc9c1\uc811 \uad6c\ud604\ud55c Custom Type \uc18c\uc2a4 \ucf54\ub4dc\ub97c \uc791\uc131\ud574\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<p>\ub9cc\uc57d \uc815\uc218\ud615 \uac12\uc744 \uc694\uc18c\ub85c \ud558\ub294 \ud799 \uc790\ub8cc\uad6c\uc870\ub97c \ub9cc\ub4e0\ub2e4\uace0 \ud560\ub54c heap.Interface \uc778\ud130\ud398\uc774\uc2a4\ub97c \uad6c\ud604\ud558\ub294 Custom Type\uc740 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"\">type IntHeap []int\r\n\r\nfunc (h IntHeap) Len() int {\r\n    return len(h)\r\n}\r\n\r\nfunc (h IntHeap) Less(i, j int) bool {\r\n    return h[i] < h[j]\r\n}\r\n\r\nfunc (h IntHeap) Swap(i, j int) {\r\n    h[i], h[j] = h[j], h[i]\r\n}\r\n\r\nfunc (h *IntHeap) Push(elem interface{}) {\r\n    *h = append(*h, elem.(int))\r\n}\r\n\r\nfunc (h *IntHeap) Pop() interface{} {\r\n    old := *h\r\n    n := len(old)\r\n    elem := old[n-1]\r\n    *h = old[0 : n-1]\r\n\r\n    return elem\r\n}\r\n<\/pre>\n<p>heap.Interface\uc5d0\uc11c \uad6c\ud604\ud574\uc57c \ud558\ub294 \ub9e4\uc11c\ub4dc\ub294 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<table style=\"border-collapse: collapse; border: 1px gray solid;\" border=\"1\" cellspacing=\"0\" cellpadding=\"2\" align=\"center\">\n<tbody>\n<tr>\n<th style=\"text-align: center;\">\ub9e4\uc11c\ub4dc<\/th>\n<th style=\"text-align: center;\">\uc124\uba85<\/th>\n<\/tr>\n<tr>\n<td>func (h IntHeap) Less(i, j int) bool<\/td>\n<td>i\uc640 j \ubc88\uc9f8 \uc694\uc18c\uc758 \uac12\uc744 \ube44\uad50\ud558\uc5ec i\ubc88\uc9f8 \uc694\uc18c\uac00 j\ubc88\uc9f8 \uc694\uc18c\ubcf4\ub2e4 \uc791\uc73c\uba74 true\uc744 \ubc18\ud658<\/td>\n<\/tr>\n<tr>\n<td>func (h IntHeap) Len() int<\/td>\n<td>\uc694\uc18c\uc758 \uac1c\uc218\ub97c \ubc18\ud658<\/td>\n<\/tr>\n<tr>\n<td>func (h IntHeap) Swap(i, j int)<\/td>\n<td>i\ubc88\uc9f8\uc640 j\ubc88\uc9f8 \uc694\uc18c\uc758 \uac12\uc744 \uc11c\ub85c \ubc14\uafc8<\/td>\n<\/tr>\n<tr>\n<td>func (h *IntHeap) Push(elem interface{})<\/td>\n<td>\uc694\uc18c\uc758 \uac12\uc744 \ucd94\uac00<\/td>\n<\/tr>\n<tr>\n<td>func (h *IntHeap) Pop() interface{}<\/td>\n<td>\uc694\uc18c\uc758 \uac12\uc744 \uac00\uc838\uc634(\uc791\uc740 \uac12 \uc21c\uc11c\ub85c \uac12\uc774 \uc5bb\uc5b4\uc9d0)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uc774\uc81c \uc704\uc758 IntHeap\uc744 \uc0ac\uc6a9\ud558\ub294 \uc18c\uc2a4 \ucf54\ub4dc\ub97c \uc0b4\ud3b4 \ubcf4\uba74 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre>\/\/ main\r\npackage main\r\n\r\nimport (\r\n    \"container\/heap\"\r\n    \"fmt\"\r\n)\r\n\r\ntype IntHeap []int\r\n\r\nfunc (h IntHeap) Len() int {\r\n    return len(h)\r\n}\r\n\r\nfunc (h IntHeap) Less(i, j int) bool {\r\n    return h[i] < h[j] \r\n} \r\n\r\nfunc (h IntHeap) Swap(i, j int) { \r\n    h[i], h[j] = h[j], h[i] \r\n} \r\n\r\nfunc (h *IntHeap) Push(elem interface{}) { \r\n    *h = append(*h, elem.(int)) \r\n} \r\n\r\nfunc (h *IntHeap) Pop() interface{} { \r\n    old := *h \r\n    n := len(old) \r\n    elem := old[n-1] \r\n    *h = old[0 : n-1] \r\n\r\n    return elem \r\n} \r\n\r\nfunc main() { \r\n    h := &#038;IntHeap{9, 6, 4, 1} \r\n    heap.Init(h) \r\n\r\n    fmt.Println(*h) \r\n    fmt.Println(\"---------------\") \r\n\r\n    heap.Push(h, 7) \r\n    heap.Push(h, 9) \r\n    heap.Push(h, 11) \r\n\r\n    for h.Len() > 0 {\r\n        m := heap.Pop(h)\r\n        fmt.Print(m, \" \")\r\n    }\r\n}\r\n<\/pre>\n<p>\uc55e\uc11c \uc124\uba85\ud55c \ucf54\ub4dc\uc5d0 \ub300\ud55c \uc124\uba85\uc740 \ud558\uc9c0 \uc54a\uace0 main \ud568\uc218\ub97c \uc0b4\ud3b4\ubcf4\uba74.. \uba3c\uc800 37\ubc88 \ucf54\ub4dc\uc5d0\uc11c \ud799 \ub370\uc774\ud130\ub85c \uad6c\uc131\ud560 \uc694\uc18c\ub4e4\uc5d0 \ub300\ud55c \uc2ac\ub77c\uc774\uc2a4 \uac1d\uccb4\ub97c \uc0dd\uc131\ud569\ub2c8\ub2e4. \uadf8\ub9ac\uace0 38\ubc88\uc5d0\uc11c \uc774 \uc2ac\ub77c\uc774\uc2a4 \uac1d\uccb4\ub97c \ud1b5\ud574 heap \uac1d\uccb4\ub85c \ucd08\uae30\ud654\ud569\ub2c8\ub2e4. \uadf8\ub9ac\uace0 43\ubc88~45\ubc88 \ucf54\ub4dc\uc5d0\uc11c \ub2e4\uc2dc \uba87\uac1c\uc758 \ub370\uc774\ud130\ub97c \ub354 \ucd94\uac00\ud558\uace0 47\ubc88\uc758 for \ubb38\uc744 \ud1b5\ud574 \ud799\uc758 \uc694\uc18c\ub97c \uc21c\uc11c\ub300\ub85c \ucd9c\ub825\ud574\ubcf4\uba74 \uc791\uc740 \uac12\uc5d0\uc11c \uc810\uc810 \ud070 \uac12\uc774 \uc5bb\uc5b4\uc9c0\ub294 \uac83\uc744 \ubcfc \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc704\uc758 \ucf54\ub4dc\uc5d0 \ub300\ud55c \uc2e4\ud589 \uacb0\uacfc\ub294 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre>[1 6 4 9]\r\n---------------\r\n1 4 6 7 9 9 11\r\n<\/pre>\n<p>\uc774 \uae00\uc740 <a href=\"http:\/\/golang.site\/\">\uc608\uc81c\ub85c \ubc30\uc6b0\ub294 GO \ud504\ub85c\uadf8\ub798\ubc0d<\/a>\uc5d0\uc11c \ud559\uc2b5\ud55c \ucf54\ub4dc\ub97c \uc81c\uac00 \uc774\ud574\ud55c \ub0b4\uc6a9\uc744 \ub367\ubd99\uc5ec \uc815\ub9ac\ud55c \uae00\uc785\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>go\uc5d0\uc11c \ud328\ud0a4\uc9c0 \ub77c\uc774\ube0c\ub7ec\ub9ac\ub85c \uc81c\uacf5\ud558\ub294 \ud799 \uc790\ub8cc\uad6c\uc870\uac00 \uc788\uc2b5\ub2c8\ub2e4. \uc81c\uacf5\ub418\ub294 \ud799 \uc790\ub8cc\uad6c\uc870\ub294 \ubd80\ubaa8 \ub178\ub4dc\uc758 \uac12\uc740 \uc790\uc2dd \ub178\ub4dc\uc758 \uac12\ubcf4\ub2e4 \ubc18\ub4dc\uc2dc \uc791\uac70\ub098 \uac19\ub2e4\ub77c\ub294 \uc870\uac74\uc744 \ucda9\uc871\ud574\uc57c \ud569\ub2c8\ub2e4. \uc774\ub7ec\ud55c \uc870\uac74 \ucda9\uc871\uc744 \ube44\uad50 \ub9e4\uc11c\ub4dc\uc640 \ud799 \uc790\ub8cc\uad6c\uc870\uc5d0 \uc694\uc18c\ub97c \ucd94\uac00(Push)\ud558\uace0 \uac00\uc838\uc624\ub294(Pop) \ub9e4\uc11c\ub4dc \ub4f1\uc744 \uc81c\uacf5\ud558\ub294 heap.Interface \uc778\ud130\ud398\uc774\uc2a4\ub97c \uc9c1\uc811 \uad6c\ud604\ud55c Custom Type \uc18c\uc2a4 \ucf54\ub4dc\ub97c \uc791\uc131\ud574\uc57c \ud569\ub2c8\ub2e4. \ub9cc\uc57d \uc815\uc218\ud615 \uac12\uc744 \uc694\uc18c\ub85c \ud558\ub294 \ud799 \uc790\ub8cc\uad6c\uc870\ub97c \ub9cc\ub4e0\ub2e4\uace0 \ud560\ub54c heap.Interface &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=2451\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;[Golang] Heap \uc790\ub8cc\uad6c\uc870&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[113],"tags":[114,111],"class_list":["post-2451","post","type-post","status-publish","format-standard","hentry","category-golang","tag-go","tag-golang"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/2451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2451"}],"version-history":[{"count":3,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/2451\/revisions"}],"predecessor-version":[{"id":2777,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/2451\/revisions\/2777"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2451"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}