{"id":27302,"date":"2018-01-20T20:50:31","date_gmt":"2018-01-20T15:20:31","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27302"},"modified":"2018-01-20T20:50:31","modified_gmt":"2018-01-20T15:20:31","slug":"check-whether-given-binary-tree-complete-not-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/check-whether-given-binary-tree-complete-not-2\/","title":{"rendered":"Python Programming-Check whether a given Binary Tree is Complete or not"},"content":{"rendered":"<p>Given a Binary Tree, write a function to check whether the given Binary Tree is Complete Binary Tree or not.<span id=\"more-23449\"><\/span><\/p>\n<p>A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. See following examples.<\/p>\n<pre>The following trees are examples of Complete Binary Trees\r\n    1\r\n  \/   \\\r\n 2     3\r\n  \r\n       1\r\n    \/    \\\r\n   2       3\r\n  \/\r\n 4\r\n\r\n       1\r\n    \/    \\\r\n   2      3\r\n  \/  \\    \/\r\n 4    5  6\r\n<\/pre>\n<pre>The following trees are examples of Non-Complete Binary Trees\r\n    1\r\n      \\\r\n       3\r\n  \r\n       1\r\n    \/    \\\r\n   2       3\r\n    \\     \/  \\   \r\n     4   5    6\r\n\r\n       1\r\n    \/    \\\r\n   2      3\r\n         \/  \\\r\n        4    5\r\n<\/pre>\n<p>The method 2 of level order traversal post can be easily modified to check whether a tree is Complete or not. To understand the approach, let us first define a term \u2018Full Node\u2019. A node is \u2018Full Node\u2019 if both left and right children are not empty (or not NULL).<\/p>\n[ad type=&#8221;banner&#8221;]\nThe approach is to do a level order traversal starting from root. In the traversal, once a node is found which is NOT a Full Node, all the following nodes must be leaf nodes.<br \/>\nAlso, one more thing needs to be checked to handle the below case: If a node has empty left child, then the right child must be empty.<\/p>\n<h4 id=\"python-programming\">Python Programming<\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Check whether binary tree is complete or not<br\/> <br\/># A binary tree node<br\/>class Node:<br\/> <br\/>    # Constructor to create a new node<br\/>    def __init__(self, data):<br\/>        self.data = data <br\/>        self.left = None<br\/>        self.right = None<br\/> <br\/># Given a binary tree, return true if the tree is complete<br\/># else return false<br\/>def isCompleteBT(root):<br\/>     <br\/>    # Base Case: An empty tree is complete Binary tree<br\/>    if root is None:<br\/>        return True<br\/> <br\/>    # Create an empty queue<br\/>    queue = []<br\/> <br\/>    # Create a flag variable which will be set Trye <br\/>    # when a non ful node is seen<br\/>    flag = False<br\/> <br\/>    # Do level order traversal using queue<br\/>    queue.append(root)<br\/>    while(len(queue) &gt; 0):<br\/>        tempNode = queue.pop(0) # Dequeue <br\/> <br\/>        # Check if left child is present<br\/>        if (tempNode.left):<br\/>             <br\/>            # If we have seen a non full node, and we see<br\/>            # a node with non-empty left child, then the<br\/>            # given tree is not a complete binary tree<br\/>            if flag == True :<br\/>                return False<br\/> <br\/>            # Enqueue left child<br\/>            queue.append(tempNode.left)<br\/> <br\/>            # If this a non-full node, set the flag as true<br\/>        else:<br\/>            flag = True<br\/> <br\/>        # Check if right cild is present<br\/>        if(tempNode.right):<br\/>                 <br\/>            # If we have seen a non full node, and we <br\/>            # see a node with non-empty right child, then<br\/>            # the given tree is not a compelete BT<br\/>            if flag == True:<br\/>                return False<br\/> <br\/>            # Enqueue right child<br\/>            queue.append(tempNode.right)<br\/>             <br\/>        # If this is non-full node, set the flag as True<br\/>        else:<br\/>            flag = True<br\/>         <br\/>    # If we reach here, then the tree is compelete BT<br\/>    return True<br\/> <br\/> <br\/># Driver program to test above function<br\/> <br\/>&quot;&quot;&quot; Let us construct the following Binary Tree which<br\/>      is not a complete Binary Tree<br\/>            1<br\/>          \/   \\<br\/>         2     3<br\/>        \/ \\     \\<br\/>       4   5     6<br\/>    &quot;&quot;&quot;<br\/>root = Node(1)<br\/>root.left = Node(2)<br\/>root.right = Node(3)<br\/>root.left.left = Node(4)<br\/>root.left.right = Node(5)<br\/>root.right.right = Node(6)<br\/> <br\/>if (isCompleteBT(root)):<br\/>    print &quot;Complete Binary Tree&quot;<br\/>else:<br\/>    print &quot;NOT Complete Binary Tree&quot;<br\/> <br\/># This code is contributed by Nikhil Kumar Singh(nickzuck_007)<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>NOT Complete Binary Tree<\/pre>\n<p><em>Time Complexity:<\/em> O(n) where n is the number of nodes in given Binary Tree<\/p>\n<p><em>Auxiliary Space: <\/em>O(n) for queue.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Check whether given binary tree complete not Given a Binary Tree, write a function to check whether the given Binary Tree is Complete Binary Tree or not.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,73012,4148,80124],"tags":[81370,81368,81365,81366,81372,81367,81362,81363,81364,81373,81374,81376,81369,81371,81375],"class_list":["post-27302","post","type-post","status-publish","format-standard","hentry","category-coding","category-data-structures","category-python","category-queue","tag-almost-complete-binary-tree","tag-check-if-binary-tree-is-complete-c","tag-check-if-binary-tree-is-perfect","tag-check-if-binary-tree-is-perfectly-balanced","tag-complete-binary-tree-algorithm","tag-complete-binary-tree-code-c","tag-complete-binary-tree-definition-example","tag-complete-binary-tree-java","tag-complete-binary-tree-program-in-c","tag-extended-binary-tree","tag-incomplete-binary-tree","tag-perfect-binary-tree","tag-perfect-binary-tree-c","tag-strictly-binary-tree","tag-types-of-binary-tree"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27302"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27302\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}