Check whether a given Binary Tree is Complete or not | GeeksforGeeks
Given a Binary Tree, write a function to check whether the given Binary Tree is Complete Binary Tree or not.
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.
Time Complexity: O(n) where n is the number of nodes in given Binary Tree
Auxiliary Space: O(n) for queue.
The 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.
Also, 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.
http://www.ideserve.co.in/learn/check-whether-binary-tree-is-complete-tree-or-not
Time Complexity is O(n)
Space Complexity is O(n)
http://www.geeksforgeeks.org/check-whether-binary-tree-complete-not-set-2-recursive-solution/
Read full article from Check whether a given Binary Tree is Complete or not | GeeksforGeeks
Given a Binary Tree, write a function to check whether the given Binary Tree is Complete Binary Tree or not.
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.
Time Complexity: O(n) where n is the number of nodes in given Binary Tree
Auxiliary Space: O(n) for queue.
The 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.
Also, 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.
bool
isCompleteBT(
struct
node* root)
{
// Base Case: An empty tree is complete Binary Tree
if
(root == NULL)
return
true
;
// Create an empty queue
int
rear, front;
struct
node **queue = createQueue(&front, &rear);
// Create a flag variable which will be set true
// when a non full node is seen
bool
flag =
false
;
// Do level order traversal using queue.
enQueue(queue, &rear, root);
while
(!isQueueEmpty(&front, &rear))
{
struct
node *temp_node = deQueue(queue, &front);
/* Ceck if left child is present*/
if
(temp_node->left)
{
// If we have seen a non full node, and we see a node
// with non-empty left child, then the given tree is not
// a complete Binary Tree
if
(flag ==
true
)
return
false
;
enQueue(queue, &rear, temp_node->left);
// Enqueue Left Child
}
else
// If this a non-full node, set the flag as true
flag =
true
;
/* Ceck if right child is present*/
if
(temp_node->right)
{
// If we have seen a non full node, and we see a node
// with non-empty left child, then the given tree is not
// a complete Binary Tree
if
(flag ==
true
)
return
false
;
enQueue(queue, &rear, temp_node->right);
// Enqueue Right Child
}
else
// If this a non-full node, set the flag as true
flag =
true
;
}
// If we reach here, then the tree is complete Bianry Tree
return
true
;
}
Time Complexity is O(n)
Space Complexity is O(n)
public boolean checkIfComplete()
{
if
(root ==
null
)
return
true
;
LinkedList<TreeNode> queue =
new
LinkedList();
boolean nonFullNodeSeen =
false
;
queue.add(root);
while
(!queue.isEmpty())
{
TreeNode currentNode = queue.remove();
if
((currentNode.left !=
null
) && (currentNode.right !=
null
))
{
// there should not be any non-leaf node after first non full-node is seen
if
(nonFullNodeSeen)
{
return
false
;
}
queue.add(currentNode.left);
queue.add(currentNode.right);
}
if
((currentNode.left !=
null
) && (currentNode.right ==
null
))
{
// there should not be any non-leaf node after first non full-node is seen
if
(nonFullNodeSeen)
{
return
false
;
}
// this is the first non-full node seen
nonFullNodeSeen =
true
;
queue.add(currentNode.left);
}
// this should never be the case for a complete binary tree
if
((currentNode.left ==
null
) && (currentNode.right !=
null
))
{
return
false
;
}
}
return
true
;
}
http://www.geeksforgeeks.org/check-whether-binary-tree-complete-not-set-2-recursive-solution/
n the array representation of a binary tree, if the parent node is assigned an index of ‘i’ and left child gets assigned an index of ‘2*i + 1’ while the right child is assigned an index of ‘2*i + 2’. If we represent the binary tree below as an array with the respective indices assigned to the different nodes of the tree below are shown below:-
As can be seen from the above figure, the assigned indices in case of a complete binary tree will strictly less be than the number of nodes in the complete binary tree. Below is the example of non-complete binary tree with the assigned array indices. As can be seen the assigned indices are equal to the number of nodes in the binary tree. Hence this tree is not a complete binary tree.
Hence we proceed in the following manner in order to check if the binary tree is complete binary tree.
- Calculate the number of nodes (count) in the binary tree.
- Start recursion of the binary tree from the root node of the binary tree with index (i) being set as 0 and the number of nodes in the binary (count).
- If the current node under examination is NULL, then the tree is a complete binary tree. Return true.
- If index (i) of the current node is greater than or equal to the number of nodes in the binary tree (count) i.e. (i>= count), then the tree is not a complete binary. Return false.
- Recursively check the left and right sub-trees of the binary tree for same condition. For the left sub-tree use the index as (2*i + 1) while for the right sub-tree use the index as (2*i + 2).
The time complexity of the above algorithm is O(n).
unsigned
int
countNodes(
struct
Node* root)
{
if
(root == NULL)
return
(0);
return
(1 + countNodes(root->left) + countNodes(root->right));
}
/* This function checks if the binary tree is complete or not */
bool
isComplete (
struct
Node* root, unsigned
int
index,
unsigned
int
number_nodes)
{
// An empty tree is complete
if
(root == NULL)
return
(
true
);
// If index assigned to current node is more than
// number of nodes in tree, then tree is not complete
if
(index >= number_nodes)
return
(
false
);
// Recur for left and right subtrees
return
(isComplete(root->left, 2*index + 1, number_nodes) &&
isComplete(root->right, 2*index + 2, number_nodes));
}
Read full article from Check whether a given Binary Tree is Complete or not | GeeksforGeeks