Full Binary Tree
Full Binary Tree
Full binary tree is also called as a strict binary tree.Each node either is a leaf or is an internal node with exactly two non empty children.
The FST is defined as the tree in which each node must contain 2 children except the leaf nodes.
Full Binary Tree Theorems
Let, i = number of internal nodes n = total number of nodes l = number of leaves λ = number of levels
- The no. of leaves is
i + 1
. - The total no. of nodes is
2i + 1
. - The no. of internal nodes is (n – 1) / 2.
- The no. of leaves is
(n + 1) / 2
. - The total no. of nodes is
2i – 1
. - The no. of internal nodes is
i – 1
.
Properties of FBT
- The no. of leaf nodes is equal to the no. of internal nodes + 1.
- The maximum no. of nodes is the same as the number of nodes in the binary tree that is, 2h+1 -1.
- The minimum no. of nodes in the FSD is 2*h-1.
- The minimum height of the FBT is log2(n+1) – 1.
- The maximum height of the FBT can be calculated as:
n= 2*h – 1
n+1 = 2*h
Program of FBT in C
// to checking if a binary tree is a full binary tree in C #include <stdbool.h> #include <stdio.h> #include <stdlib.h> struct Node { int item; struct Node *left, *right; }; // Create a new Node struct Node *createNewNode(char k) { struct Node *node = (struct Node *)malloc(sizeof(struct Node)); node->item = k; node->right = node->left = NULL; return node; } bool isFullBinaryTree(struct Node *root) { if (root == NULL) return true; if (root->left == NULL && root->right == NULL) return true; if ((root->left) && (root->right)) return (isFullBinaryTree(root->left) && isFullBinaryTree(root->right)); return false; } int main() { struct Node *root = NULL; root = createNewNode(1); root->left = createNewNode(2); root->right = createNewNode(3); root->left->left = createNewNode(4); root->left->right = createNewNode(5); root->left->right->left = createNewNode(6); root->left->right->right = createNewNode(7); if (isFullBinaryTree(root)) printf("The tree is full binary tree\n"); else printf("The tree is not full binary tree\n"); }
Output
The tree is full binary tree
Program of FBT in C++
#include <iostream> using namespace std; struct Node { int key; struct Node *left, *right; }; // creation a new nodw struct Node *newNode(char k) { struct Node *node = (struct Node *)malloc(sizeof(struct Node)); node->key = k; node->right = node->left = NULL; return node; } bool isFullBinaryTree(struct Node *root) { if (root == NULL) return true; if (root->left == NULL && root->right == NULL) return true; if ((root->left) && (root->right)) return (isFullBinaryTree(root->left) && isFullBinaryTree(root->right)); return false; } int main() { struct Node *root = NULL; root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->left->right->left = newNode(6); root->left->right->right = newNode(7); if (isFullBinaryTree(root)) cout << "The tree is full binary tree\n"; else cout << "The tree is not full binary tree\n"; }
Output
The tree is full binary tree
Program of FBT in Python
# Creating a node class Node: def __init__(self, item): self.item = item self.leftChild = None self.rightChild = None # Checking full binary tree def isFullTree(root): # Tree empty case if root is None: return True # Checking if child is present if root.leftChild is None and root.rightChild is None: return True if root.leftChild is not None and root.rightChild is not None: return (isFullTree(root.leftChild) and isFullTree(root.rightChild)) return False root = Node(1) root.rightChild = Node(3) root.leftChild = Node(2) root.leftChild.leftChild = Node(4) root.leftChild.rightChild = Node(5) root.leftChild.rightChild.leftChild = Node(6) root.leftChild.rightChild.rightChild = Node(7) if isFullTree(root): print("The tree is full binary tree") else: print("The tree is not full binary tree")
Output
The tree is full binary tree
Program of FBT in Java
class Node { int data; Node leftChild, rightChild; Node(int item) { data = item; leftChild = rightChild = null; } } class BinaryTree { Node root; // Check for Full Binary Tree boolean isFullBinaryTree(Node node) { // Checking tree emptiness if (node == null) return true; // Checking the children if (node.leftChild == null && node.rightChild == null) return true; if ((node.leftChild != null) && (node.rightChild != null)) return (isFullBinaryTree(node.leftChild) && isFullBinaryTree(node.rightChild)); return false; } public static void main(String args[]) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.leftChild = new Node(2); tree.root.rightChild = new Node(3); tree.root.leftChild.leftChild = new Node(4); tree.root.leftChild.rightChild = new Node(5); tree.root.rightChild.leftChild = new Node(6); tree.root.rightChild.rightChild = new Node(7); if (tree.isFullBinaryTree(tree.root)) System.out.print("The tree is full binary tree"); else System.out.print("The tree is not full binary tree"); } }
Output
Thr tree is full binary tree
Read more, Binary Tree