{"id":4865,"date":"2023-09-06T17:48:04","date_gmt":"2023-09-06T12:18:04","guid":{"rendered":"https:\/\/www.interviewbit.com\/blog\/?p=4865"},"modified":"2023-09-06T17:48:05","modified_gmt":"2023-09-06T12:18:05","slug":"n-ary-tree","status":"publish","type":"post","link":"https:\/\/www.interviewbit.com\/blog\/n-ary-tree\/","title":{"rendered":"N-ary Tree &#8211; Tree Data Structures"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<div class=\"gutentoc tocactive nostyle\"><div class=\"gutentoc-toc-wrap\"><div class=\"gutentoc-toc-title-wrap\"><div class=\"gutentoc-toc-title\">Table Of Contents<\/div><div id=\"open\" class=\"text_open\">show<\/div><\/div><div id=\"toclist\"><div class=\"gutentoc-toc__list-wrap\"><ul class=\"gutentoc-toc__list\"><li><a href=\"#introduction\">Introduction<\/a><\/li><li><a href=\"#1-naive-approach\">1. Naive Approach<\/a><\/li><li><a href=\"#2-improved-approach\">2. Improved Approach<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#sample-implementation\">Sample Implementation<\/a><\/li><\/ul><li><a href=\"#3-optimal-approach\">3. Optimal Approach<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#advantages\">Advantages<\/a><\/li><li><a href=\"#sample-implementation\">Sample Implementation<\/a><\/li><\/ul><li><a href=\"#additional-resources\">Additional Resources<\/a><\/li><\/ul><\/div><\/div><\/div><\/div>\n\n\n\n<h2 id=\"introduction\">Introduction<\/h2>\n\n\n\n<p><strong>N-ary trees<\/strong> are tree data structures that allow us to have up to <strong>n<\/strong> children nodes for each of the nodes, differing from the standard binary trees which allow only up to <strong>2<\/strong> children nodes for each node.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img  loading=\"lazy\"  width=\"1024\"  height=\"569\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"n-ary tree\"  class=\"wp-image-5042 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/12\/Image1-4-1024x569.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/12\/Image1-4-1024x569.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/12\/Image1-4-300x167.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/12\/Image1-4-768x427.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/12\/Image1-4-380x211.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/12\/Image1-4-550x306.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/12\/Image1-4-800x445.png 800w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/12\/Image1-4-1160x645.png 1160w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/12\/Image1-4.png 1447w\" ><\/figure>\n\n\n\n<p>The above picture shows an example of an n-ary tree. Observe that in the example, the tree has a total of n = 11 nodes, where some nodes have 3 children, some have only 1 child, and some don\u2019t have any children at all. This tree is an N-ary tree where N &gt;= 3 because the number of children of the tree ranges from 0 to 3.<\/p>\n\n\n\n<h2 id=\"1-naive-approach\">1. Naive Approach<\/h2>\n\n\n\n<p>We basically need to store 2 pieces of information to completely represent an N-ary tree.<\/p>\n\n\n\n<ul><li>The value of the node<\/li><li>The addresses of all its children nodes.<\/li><\/ul>\n\n\n\n<p>We can build a class or struct to store this information effectively. For storing the latter information, we can use an <strong>Array or LinkedList<\/strong>. Both these data structures will however have some shortcomings which we will discuss below:<\/p>\n\n\n\n<ul><li>Since the number of children of a node is <strong>not known<\/strong> beforehand, we can store only a fixed number of children\u2019s addresses in an <strong>array<\/strong>.<\/li><li><strong>LinkedList<\/strong> won\u2019t allow us to randomly access any child\u2019s address, so it will be expensive in terms of complexity.<\/li><\/ul>\n\n\n\n<h2 id=\"2-improved-approach\">2. Improved Approach<\/h2>\n\n\n\n<p>To improve the shortcomings of the naive approach, we can use <strong>Dynamic Arrays<\/strong> to store the addresses of the children of the nodes. Using this approach, we can randomly access any child\u2019s address in O(1) as well as don\u2019t have to know the number of children of each node beforehand.<\/p>\n\n\n\n<h3 id=\"sample-implementation\"><span id=\"sample-implementation-2\">Sample Implementation<\/span><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class TreeNode {\n    int value;\n    vector&lt;TreeNode*> children;\n};<\/pre>\n\n\n\n<h2 id=\"3-optimal-approach\">3. Optimal Approach<\/h2>\n\n\n\n<p>The optimal way to implement a Node for an N-ary Tree will be to use the First Child\/Next Sibling representation. The steps to implement this are as follows:<\/p>\n\n\n\n<ul><li>For each node, we link the children of the common parent(siblings) from left to right order.<\/li><li>Then we will remove the links from the parent to all the children, except for the first child node.<\/li><\/ul>\n\n\n\n<p>The focus of this representation is that since we have a link between children, we do not need extra links from parents to all the children.<\/p>\n\n\n\n<h3 id=\"advantages\">Advantages<\/h3>\n\n\n\n<ul><li>Since no extra links are required, we can consider this to be a memory-efficient implementation.<\/li><li>All the nodes are of constant size, and no dynamic arrays or linked lists are required.<\/li><li>With the above representation, we can convert all n-ary trees into a binary tree representation, which is a topic we are more familiar with.<\/li><li>Many algorithms can be expressed and implemented more easily because we can consider the N-ary tree to be a binary tree.<\/li><\/ul>\n\n\n\n<h3 id=\"sample-implementation\">Sample Implementation<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct TreeNode {\n    int value;\n    TreeNode* firstChild;\n    TreeNode* nextSibling;\n};<\/pre>\n\n\n\n<h2 id=\"additional-resources\">Additional Resources<\/h2>\n\n\n\n<ul><li><a href=\"https:\/\/www.interviewbit.com\/courses\/programming\/tree-data-structure\/\" target=\"_blank\" rel=\"noreferrer noopener\">Tree Data Structure<\/a><\/li><li><a href=\"https:\/\/www.interviewbit.com\/coding-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Coding Interview Questions<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"Introduction N-ary trees are tree data structures that allow us to have up to n children nodes for&hellip;\n","protected":false},"author":5,"featured_media":5043,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_daextam_enable_autolinks":"1","csco_singular_sidebar":"","csco_page_header_type":"","csco_appearance_grid":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0},"categories":[145],"tags":[681],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/4865"}],"collection":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/comments?post=4865"}],"version-history":[{"count":6,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/4865\/revisions"}],"predecessor-version":[{"id":23625,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/4865\/revisions\/23625"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media\/5043"}],"wp:attachment":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media?parent=4865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/categories?post=4865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/tags?post=4865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}