{"id":35176,"date":"2024-11-01T09:36:26","date_gmt":"2024-11-01T09:36:26","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35176"},"modified":"2024-11-01T11:44:46","modified_gmt":"2024-11-01T11:44:46","slug":"kotlin-coding-test-course-finding-the-diameter-of-a-tree","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35176\/","title":{"rendered":"kotlin coding test course, finding the diameter of a tree"},"content":{"rendered":"<p><body><\/p>\n<p>\n        Hello. Today we will learn how to find the diameter of a tree using Kotlin.<br \/>\n        In this tutorial, we will explain the basic concepts of trees, the definition of diameter, and the algorithmic approach in detail,<br \/>\n        and we will practice through example code.\n    <\/p>\n<h2>Tree and Tree Diameter<\/h2>\n<p>\n        A tree is a non-linear data structure in which each node can have zero or more children.<br \/>\n        A tree has the following characteristics:\n    <\/p>\n<ul>\n<li>A tree has a root node, and the root is connected to other nodes.<\/li>\n<li>A child node must have exactly one parent node.<\/li>\n<li>The path from the root to a leaf node defines the height of the tree.<\/li>\n<\/ul>\n<p>\n<strong>The diameter of a tree<\/strong> refers to the length of the longest path within the tree.<br \/>\n        The diameter can always be defined as the distance between two leaf nodes.<br \/>\n        For example, consider the tree below:\n    <\/p>\n<pre>\n        1\n       \/ \\\n      2   3\n     \/ \\\n    4   5\n    <\/pre>\n<p>\n        The diameter of this tree is the longest path from node 4 to node 5, which is 4-2-5.<br \/>\n        Therefore, the length of the diameter is 2.\n    <\/p>\n<h2>Algorithm for Finding the Diameter of a Tree<\/h2>\n<p>\n        To find the diameter, you can use DFS (Depth-First Search) or BFS (Breadth-First Search).<br \/>\n        Generally, DFS is the more commonly used method.<br \/>\n        The basic idea of the algorithm is as follows:\n    <\/p>\n<ol>\n<li>Select an arbitrary node (root node) and perform the first DFS. During this, find the farthest node.<\/li>\n<li>Starting from the node found in the first DFS, perform a second DFS.<\/li>\n<li>Return the maximum distance (diameter) calculated in the second DFS.<\/li>\n<\/ol>\n<h3>Kotlin Code Implementation<\/h3>\n<p>\n        Now, we will implement this algorithm in Kotlin.<br \/>\n        We will use data classes to represent nodes and edges, and<br \/>\n        we will create a DFS method to calculate the diameter of the tree.\n    <\/p>\n<h4>Data Class and Graph Initialization<\/h4>\n<pre><code>Kotlin\ndata class Edge(val to: Int, val weight: Int)\n\nclass Tree(val size: Int) {\n    private val graph = Array(size) { mutableListOf<Edge>() }\n\n    fun addEdge(from: Int, to: Int, weight: Int) {\n        graph[from].add(Edge(to, weight))\n        graph[to].add(Edge(from, weight))\n    }\n\n    fun diameter(start: Int): Int {\n        \/\/ Perform the first DFS\n        val (farthestNode, _) = dfs(start, BooleanArray(size), 0)\n        \/\/ Perform the second DFS\n        val (_, diameter) = dfs(farthestNode, BooleanArray(size), 0)\n        return diameter\n    }\n\n    private fun dfs(node: Int, visited: BooleanArray, distance: Int): Pair<Int, Int> {\n        visited[node] = true\n        var maxDistance = distance\n        var farthestNode = node\n\n        for (edge in graph[node]) {\n            if (!visited[edge.to]) {\n                val (newFarthestNode, newDistance) = dfs(edge.to, visited, distance + edge.weight)\n                if (newDistance > maxDistance) {\n                    maxDistance = newDistance\n                    farthestNode = newFarthestNode\n                }\n            }\n        }\n\n        return Pair(farthestNode, maxDistance)\n    }\n}\n<\/Int,><\/Edge><\/code><\/pre>\n<h4>Code Explanation<\/h4>\n<p>\n        &#8211; <code>Edge<\/code> data class: A class that defines an edge.<br \/>\n        It includes the node and weight.\n    <\/p>\n<p>\n        &#8211; <code>Tree<\/code>: A class that represents a tree. It initializes the size of the tree and the graph data structure.<br \/>\n        Edges can be added using the <code>addEdge<\/code> method.\n    <\/p>\n<p>\n        &#8211; <code>diameter<\/code> method: A method that calculates the diameter.<br \/>\n        It performs the first and second DFS.\n    <\/p>\n<p>\n        &#8211; <code>dfs<\/code> method: A recursive method that performs depth-first search.<br \/>\n        It checks the visited nodes and calculates the maximum distance.<br \/>\n        It returns the farthest node and the distance based on the maximum distance.\n    <\/p>\n<h3>Test Cases<\/h3>\n<p>\n        To test the diameter of the tree, we will add edges as shown below and print the results.\n    <\/p>\n<pre><code>Kotlin\nfun main() {\n    val tree = Tree(5)\n    tree.addEdge(0, 1, 1)\n    tree.addEdge(0, 2, 2)\n    tree.addEdge(1, 3, 1)\n    tree.addEdge(1, 4, 1)\n\n    val diameter = tree.diameter(0)\n    println(\"The diameter of the tree is: $diameter\")\n}\n<\/code><\/pre>\n<h4>Execution Result<\/h4>\n<pre>\n    The diameter of the tree is: 4\n    <\/pre>\n<h2>Conclusion<\/h2>\n<p>\n        In this tutorial, we explored how to find the diameter of a tree using Kotlin.<br \/>\n        This approach using DFS is useful for dealing with tree data structures and can be applied in various applications.<br \/>\n        I hope this tutorial helps in solving tree-related problems.\n    <\/p>\n<p>\n        We plan to cover various algorithms and coding test problems in the future, so please look forward to it!<br \/>\n        Thank you.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello. Today we will learn how to find the diameter of a tree using Kotlin. In this tutorial, we will explain the basic concepts of trees, the definition of diameter, and the algorithmic approach in detail, and we will practice through example code. Tree and Tree Diameter A tree is a non-linear data structure in &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35176\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;kotlin coding test course, finding the diameter of a tree&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[106],"tags":[],"class_list":["post-35176","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>kotlin coding test course, finding the diameter of a tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/35176\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"kotlin coding test course, finding the diameter of a tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello. Today we will learn how to find the diameter of a tree using Kotlin. In this tutorial, we will explain the basic concepts of trees, the definition of diameter, and the algorithmic approach in detail, and we will practice through example code. Tree and Tree Diameter A tree is a non-linear data structure in &hellip; \ub354 \ubcf4\uae30 &quot;kotlin coding test course, finding the diameter of a tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35176\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:36:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:44:46+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/35176\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35176\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"kotlin coding test course, finding the diameter of a tree\",\"datePublished\":\"2024-11-01T09:36:26+00:00\",\"dateModified\":\"2024-11-01T11:44:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35176\/\"},\"wordCount\":473,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35176\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35176\/\",\"name\":\"kotlin coding test course, finding the diameter of a tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:36:26+00:00\",\"dateModified\":\"2024-11-01T11:44:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35176\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35176\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35176\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"kotlin coding test course, finding the diameter of a tree\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"kotlin coding test course, finding the diameter of a tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/35176\/","og_locale":"ko_KR","og_type":"article","og_title":"kotlin coding test course, finding the diameter of a tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello. Today we will learn how to find the diameter of a tree using Kotlin. In this tutorial, we will explain the basic concepts of trees, the definition of diameter, and the algorithmic approach in detail, and we will practice through example code. Tree and Tree Diameter A tree is a non-linear data structure in &hellip; \ub354 \ubcf4\uae30 \"kotlin coding test course, finding the diameter of a tree\"","og_url":"https:\/\/atmokpo.com\/w\/35176\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:36:26+00:00","article_modified_time":"2024-11-01T11:44:46+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/35176\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35176\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"kotlin coding test course, finding the diameter of a tree","datePublished":"2024-11-01T09:36:26+00:00","dateModified":"2024-11-01T11:44:46+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35176\/"},"wordCount":473,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35176\/","url":"https:\/\/atmokpo.com\/w\/35176\/","name":"kotlin coding test course, finding the diameter of a tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:36:26+00:00","dateModified":"2024-11-01T11:44:46+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35176\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35176\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35176\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"kotlin coding test course, finding the diameter of a tree"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=35176"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35176\/revisions"}],"predecessor-version":[{"id":35177,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35176\/revisions\/35177"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}