A Strict Binary Tree is also known as Proper Binary Tree and Full Binary Tree. A Binary Tree is identified as a Strict Binary Tree if each parent node contains either no or two children. All nodes contain two children in a Strict Binary Tree except the leaf nodes which have 0 children.
The above figure is an example of a Strict Binary Tree. The internal nodes (node 1, node 2, node 3, node 5) have 2 children each, whereas the leaf nodes (node 4, node 6, node 7, node 8, node 9) have no children.
To check whether a given Binary Tree is a Strict Binary Tree, we need to find out the number of children a node has. If any node in the tree has 1 child, the tree will not be qualified as a Strict Binary Tree.
Steps:
FUNCTION isStrictTree(node)
IF right and left child are null
RETURN true
IF right and left child both are not null
CALL isStrictTree(left child) && isStrictTree(right child)
ELSE
RETURN false
// define the nodes of the binary tree
class Node {
int val;
Node left;
Node right;
// constructor for Node class
Node(int num) {
val = num;
left = right = null;
}
}
// Binary tree construction and operations
class BinaryTree {
Node root;
// function to check if a tree is a strict binary tree
boolean isStrictTree(Node node) {
// in case the tree is empty, it is a strict binary tree
if (node == null)
return true;
// in case the node is a leaf node and has no children
if (node.left == null && node.right == null)
return true;
// if both left and right subtrees are not null and the node has two children
if ((node.left != null) && (node.right != null))
return isStrictTree(node.left) && isStrictTree(node.right);
// the last case where the node has one child
return false;
}
// function to print whether a tree is strict binary or not
void printResult(Node node) {
if (isStrictTree(node))
System.out.println("The binary tree is a Strict Binary Tree");
else
System.out.println("The binary tree is not a Strict Binary Tree");
}
public static void main(String args[]) {
// construct the binary tree for case A
BinaryTree casea = new BinaryTree();
casea.root = new Node(1);
casea.root.left = new Node(2);
casea.root.right = new Node(3);
casea.root.left.left = new Node(4);
casea.root.left.right = new Node(5);
casea.root.right.left = new Node(6);
casea.root.right.right = new Node(7);
casea.root.left.left.left = new Node(8);
casea.root.left.left.right = new Node(9);
// print result for case A
casea.printResult(casea.root);
// construct the binary tree for case B
BinaryTree caseb = new BinaryTree();
caseb.root = new Node(1);
caseb.root.left = new Node(2);
caseb.root.right = new Node(3);
caseb.root.left.left = new Node(4);
caseb.root.left.right = new Node(5);
caseb.root.right.right = new Node(6);
// print result for case B
caseb.printResult(caseb.root);
}
}
Output
The binary tree is a Strict Binary Tree
The binary tree is not a Strict Binary Tree
Time complexity: O(N) where n is number of nodes in given binary tree.
Auxiliary Space: O(N) for call stack since using recursion
Types of Binary Tree
1. Complete Binary Tree
A Binary tree is identified as a Complete Binary tree if all the nodes are added from the left, so nodes are not added to a new level until the previous level is completely filled. In the last level, all the nodes must be as left as possible.
2. Perfect Binary Tree
A Binary tree is a Perfect Binary Tree if all the internal nodes have 2 children and the levels are completely filled, including the last level. The leaf nodes are present at the same level which forms the last level of the tree. All Perfect Binary trees are Strict Binary Tree.
3. Degenerate Binary Tree
A degenerate or pathological tree is a binary tree having a single child. It could be a left or right child. A degenerate tree where the nodes only have the right child is called a right skewed binary tree whereas a tree where the nodes have left child only is called a left skewed binary tree.
119 docs|30 tests
|
|
Explore Courses for Computer Science Engineering (CSE) exam
|