{"id":8592,"date":"2022-06-21T12:37:56","date_gmt":"2022-06-21T12:37:56","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=8592"},"modified":"2023-07-21T07:00:27","modified_gmt":"2023-07-21T07:00:27","slug":"height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/","title":{"rendered":"Height of a complete Binary tree or Binary heap with N Nodes"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg\" alt=\"\" \/><\/p>\n<p>Every node in a binary tree has either 0 children, 1 children, or 2 children. The node is a leaf node if it has no offspring. A node is an internal node if it contains one or two children. The binary tree&#8217;s nodes each have three elements: the node&#8217;s data value, Pointer to the left child and Pointer to the right child.<br \/>\nThe height of a complete binary tree or binary heap with N nodes is a crucial factor that impacts the efficiency and performance of various operations performed on these structures. Understanding the height allows us to analyze the time complexity of essential operations such as insertion, deletion, and search.<\/p>\n<h2>What is the Height of Complete Binary tree with n Nodes?<\/h2>\n<p>In a complete binary tree, the height is defined as the number of edges on the longest path from the root node to any leaf node. In simpler terms, the height represents the number of levels in the tree, counting from the root level as level 0. Each level of the complete binary tree, except possibly the last level, is completely filled with nodes, and any unfilled positions on the last level are filled from left to right.<br \/>\nThe height of a complete binary tree with n nodes can be calculated using the formula:<\/p>\n<p><strong>h = floor(log2(n + 1))<\/strong><\/p>\n<p>In this formula:<\/p>\n<ul>\n<li>&quot;h&quot; represents the height of the complete binary tree.<\/li>\n<li>&quot;n&quot; represents the number of nodes in the complete binary tree.<\/li>\n<li>log2 denotes the logarithm with base 2.<\/li>\n<li>The &quot;+ 1&quot; in the formula accounts for the fact that the height of the tree starts from 0.<\/li>\n<li>Before moving ahead, let\u2019s understand the complete binary tree quickly.<\/li>\n<\/ul>\n<h3>Complete Binary Tree:<\/h3>\n<p>A complete binary tree is a binary tree in which all the levels are completely filled except the last level and the last level must be filled from the left.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1656415993128-Image-03.png\" alt=\"\" \/><\/p>\n<h3>Properties of Complete binary tree:<\/h3>\n<p>In a complete binary tree all the leaves are at the same level.<br \/>\nThe height of the complete binary tree with n nodes is log(n+1).<\/p>\n<p>The above example is the complete binary tree in which all the levels are completely filled.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814973682-Image-01%20%281%29.png\" alt=\"\" \/><\/p>\n<h3>Complete Binary tree:<\/h3>\n<p>A Binary tree is said to be a complete binary tree if all the levels of the tree are completely filled except the last level where all the nodes are as left as possible.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814762081-Image-02.png\" alt=\"\" \/><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_8591 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_8591 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_8591 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_8591 .wpsm_nav-tabs > li.active > a, #tab_container_8591 .wpsm_nav-tabs > li.active > a:hover, #tab_container_8591 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_8591 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_8591 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_8591 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_8591 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_8591 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_8591 .wpsm_nav-tabs > li > a:hover , #tab_container_8591 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_8591 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_8591 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_8591 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_8591 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_8591 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_8591 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_8591 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_8591 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_8591 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_8591 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_8591 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_8591\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_8591\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_8591_1\" aria-controls=\"tabs_desc_8591_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_8591_2\" aria-controls=\"tabs_desc_8591_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_8591_3\" aria-controls=\"tabs_desc_8591_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_8591\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_8591_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n \r\nint height(int N)\r\n{\r\n    return floor(log2(N));\r\n}\r\n \r\n\/\/ driver node\r\nint main()\r\n{\r\n    int N = 6;\r\n    cout &lt;&lt; height(N);\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_8591_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\n\r\nimport java.lang.*;\r\n \r\nclass prepbytes {\r\n     \r\n    static int height(int N)\r\n    {\r\n        return (int)Math.ceil(Math.log(N +\r\n                    1) \/ Math.log(2)) - 1;\r\n    }\r\n \r\n    public static void main(String[] args)\r\n    {\r\n        int N = 6;\r\n        System.out.println(height(N));\r\n    }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_8591_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\n \r\nimport math\r\ndef height(N):\r\n    return math.ceil(math.log2(N + 1)) - 1\r\n \r\n# driver node\r\nN = 6\r\nprint(height(N))\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_8591 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_8591 a\"),jQuery(\"#tab-content_8591\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Time Complexity:<\/strong> The time complexity for finding the height of complete binary tree with n nodes is O(1) as we are using logarithmic operation to find the complete binary tree height.<\/p>\n<p><strong>Space Complexity:<\/strong> The space complexity for finding the height of complete binary tree with n nodes is O(1) as we are using logarithmic operation to find the complete binary tree height.<\/p>\n<h2>Properties of height of complete binary tree with n nodes<\/h2>\n<ul>\n<li>\n<p><strong>Logarithmic Relationship with the Number of Nodes:<\/strong><br \/>\nAs mentioned earlier, the height of a complete binary tree with n nodes can be calculated using the formula: h = floor(log2(n + 1)). This logarithmic relationship means that as the number of nodes increases, the height grows much slower. For example, doubling the number of nodes will increase the height by only 1.<\/p>\n<\/li>\n<li>\n<p><strong>Efficient Operations:<\/strong><br \/>\nThe logarithmic height property of complete binary trees is one of the key factors contributing to their efficiency in various operations. The height determines the time complexity of essential operations like searching, insertion, and deletion. With a logarithmic height, these operations have O(log n) time complexity, making them efficient for large data sets.<\/p>\n<\/li>\n<li>\n<p><strong>Balancing Property:<\/strong><br \/>\nComplete binary trees are automatically balanced due to their definition. Since every level, except possibly the last one, is filled with nodes, the difference in the heights of the left and right subtrees of any node is at most one. This balanced structure ensures that operations are uniformly distributed, providing a consistent level of performance.<\/p>\n<\/li>\n<li>\n<p><strong>Applications in Heaps and Priority Queues:<\/strong><br \/>\nComplete binary trees find extensive applications in heaps, specifically binary min-heaps and max-heaps. In a binary min-heap, the root node holds the minimum value among all nodes, while in a binary max-heap, the root node holds the maximum value. The logarithmic height ensures that operations like extracting the minimum (or maximum) element, inserting elements, and updating heap properties are efficiently performed.<\/p>\n<\/li>\n<li>\n<p><strong>Representation and Space Efficiency:<\/strong><br \/>\nComplete binary trees can be efficiently represented using arrays, where the left and right children of a node can be found at predictable positions. This property allows for compact storage, making them ideal for implementation in memory-constrained environments.<\/p>\n<\/li>\n<\/ul>\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, the height of a complete binary tree with N nodes is a critical aspect of understanding the efficiency and performance of this powerful data structure. The height determines the number of levels in the tree, and its logarithmic relationship with the number of nodes allows for fast access, insertion, and deletion operations. As the number of nodes in the tree increases, the height grows much slower, making complete binary trees suitable for managing large datasets and solving complex computational challenges.<\/p>\n<p>The balanced nature of complete binary trees, along with their efficient representation using arrays, ensures consistent performance in a variety of applications, including heaps, priority queues, and graph algorithms. Their space efficiency and logarithmic height make them valuable in memory-constrained environments and real-time systems.<\/p>\n<h2>Frequently Asked Questions (FAQ) on Complete Binary Tree Height:<\/h2>\n<p>Here are some FAQs on height of complete binary tree with n nodes<br \/>\n<strong>Q1. How does the height of a complete binary tree impact its time complexity?<\/strong><br \/>\nThe height of a complete binary tree directly influences the time complexity of essential operations such as search, insertion, and deletion. With a logarithmic height, these operations have a time complexity of O(log N), ensuring efficient performance even for large datasets.<\/p>\n<p><strong>Q2. Can a complete binary tree be unbalanced?<\/strong><br \/>\nNo, a complete binary tree is always balanced. The property of being complete ensures that every level, except possibly the last one, is filled with nodes. This balanced structure guarantees that the height difference between the left and right subtrees of any node is at most one.<\/p>\n<p><strong>Q3. What are some practical applications of complete binary trees?<\/strong><br \/>\nComplete binary trees find extensive applications in priority queues, heaps, and graph algorithms. They are commonly used to efficiently extract the minimum or maximum element from a dataset, manage dynamic priority systems, and traverse graphs in a breadth-first manner.<\/p>\n<p><strong>Q4. How is a complete binary tree represented in memory?<\/strong><br \/>\nComplete binary trees can be efficiently represented using arrays. By sequentially storing the nodes in an array, we can easily access the left and right children of a node at predictable positions. This representation ensures space efficiency and allows for fast index-based computations.<\/p>\n<p><strong>Q5. Can the height of a complete binary tree exceed log N?<\/strong><br \/>\nNo, the height of a complete binary tree with N nodes will never exceed log2(N + 1). As N grows larger, the height increases logarithmically, making complete binary trees efficient data structures for managing vast amounts of data with relatively low tree heights.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every node in a binary tree has either 0 children, 1 children, or 2 children. The node is a leaf node if it has no offspring. A node is an internal node if it contains one or two children. The binary tree&#8217;s nodes each have three elements: the node&#8217;s data value, Pointer to the left [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[131],"tags":[176,175],"class_list":["post-8592","post","type-post","status-publish","format-standard","hentry","category-heap","tag-binary-tree","tag-heap"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Height of a complete Binary tree or Binary heap with N Nodes | Heap | Prepbytes<\/title>\n<meta name=\"description\" content=\"Given a binary tree or heap with n nodes, we have to find the height of the tree.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Height of a complete Binary tree or Binary heap with N Nodes | Heap | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Given a binary tree or heap with n nodes, we have to find the height of the tree.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-21T12:37:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-21T07:00:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Height of a complete Binary tree or Binary heap with N Nodes\",\"datePublished\":\"2022-06-21T12:37:56+00:00\",\"dateModified\":\"2023-07-21T07:00:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/\"},\"wordCount\":1181,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg\",\"keywords\":[\"binary tree\",\"heap\"],\"articleSection\":[\"Heap\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/\",\"name\":\"Height of a complete Binary tree or Binary heap with N Nodes | Heap | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg\",\"datePublished\":\"2022-06-21T12:37:56+00:00\",\"dateModified\":\"2023-07-21T07:00:27+00:00\",\"description\":\"Given a binary tree or heap with n nodes, we have to find the height of the tree.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Heap\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/heap\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Height of a complete Binary tree or Binary heap with N Nodes\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Height of a complete Binary tree or Binary heap with N Nodes | Heap | Prepbytes","description":"Given a binary tree or heap with n nodes, we have to find the height of the tree.","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:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/","og_locale":"en_US","og_type":"article","og_title":"Height of a complete Binary tree or Binary heap with N Nodes | Heap | Prepbytes","og_description":"Given a binary tree or heap with n nodes, we have to find the height of the tree.","og_url":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-06-21T12:37:56+00:00","article_modified_time":"2023-07-21T07:00:27+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Height of a complete Binary tree or Binary heap with N Nodes","datePublished":"2022-06-21T12:37:56+00:00","dateModified":"2023-07-21T07:00:27+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/"},"wordCount":1181,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg","keywords":["binary tree","heap"],"articleSection":["Heap"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/","url":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/","name":"Height of a complete Binary tree or Binary heap with N Nodes | Heap | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg","datePublished":"2022-06-21T12:37:56+00:00","dateModified":"2023-07-21T07:00:27+00:00","description":"Given a binary tree or heap with n nodes, we have to find the height of the tree.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655814660559-Height%20of%20a%20complete%20Binary%20tree%20or%20Binary%20heap%20with%20N%20Nodes.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/height-of-a-complete-binary-tree-or-binary-heap-with-n-nodes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Heap","item":"https:\/\/prepbytes.com\/blog\/category\/heap\/"},{"@type":"ListItem","position":3,"name":"Height of a complete Binary tree or Binary heap with N Nodes"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/8592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=8592"}],"version-history":[{"count":7,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/8592\/revisions"}],"predecessor-version":[{"id":17304,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/8592\/revisions\/17304"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=8592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=8592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=8592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}