Complete Binary Tree

Complete Binary Tree

Complete binary tree is defined as a trees in which all the nodes are completely filled except the last level. Complete binarytree is the full binarytree in which all leaves are the same path.

 

Types of Binary Tree

 

Properties of Complete BinaryTree

  • The maximum no. of nodes in CBT is 2h+1 – 1.
  • The minimum no. of nodes in CBT is 2h.
  • The minimum height of a CBT is log2(n+1) – 1.

Program of CBT in C

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

struct Node {
  int key;
  struct Node *left, *right;
};

// Node creation
struct Node *newNode(char k) {
  struct Node *node = (struct Node *)malloc(sizeof(struct Node));
  node->key = k;
  node->right = node->left = NULL;
  return node;
}

// Count the no. of nodes
int countNumNodes(struct Node *root) {
  if (root == NULL)
    return (0);
  return (1 + countNumNodes(root->left) + countNumNodes(root->right));
}

bool checkComp(struct Node *root, int index, int numberNodes) {
  // Check whether the tree is complete
  if (root == NULL)
    return true;

  if (index >= numberNodes)
    return false;

  return (checkComp(root->left, 2 * index + 1, numberNodes) && checkComp(root->right, 2 * index + 2, numberNodes));
}

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->right->left = newNode(6);

  int node_count = countNumNodes(root);
  int index = 0;

  if (checkComp(root, index, node_count))
    printf("The tree is complete binary tree\n");
  else
    printf("The tree is not complete binary tree\n");
}

Output

The tree is a complete binary tree

 

Program of CBT in C++

#include <iostream>

using namespace std;

struct Node {
  int key;
  struct Node *left, *right;
};

// Create node
struct Node *newNode(char k) {
  struct Node *node = (struct Node *)malloc(sizeof(struct Node));
  node->key = k;
  node->right = node->left = NULL;
  return node;
}

// Count the no. of nodes
int countNumNodes(struct Node *root) {
  if (root == NULL)
    return (0);
  return (1 + countNumNodes(root->left) + countNumNodes(root->right));
}

bool checkComp(struct Node *root, int index, int numberNodes) {
  
  if (root == NULL)
    return true;

  if (index >= numberNodes)
    return false;

  return (checkCompl(root->left, 2 * index + 1, numberNodes) && checkComp(root->right, 2 * index + 2, numberNodes));
}

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->right->left = newNode(6);

  int node_count = countNumNodes(root);
  int index = 0;

  if (checkComp(root, index, node_count))
    cout << "The tree is complete binary tree\n";
  else
    cout << "The tree is not complete binary tree\n";
}

Output

The tree is a complete binary tree

 

Program of CBT in Python

# To Check if a binary tree is a complete binary tree in Python


class Node:

    def __init__(self, item):
        self.item = item
        self.left = None
        self.right = None


# Count the no. of nodes
def count_nodes(root):
    if root is None:
        return 0
    return (1 + count_nodes(root.left) + count_nodes(root.right))


def is_complete(root, index, numberNodes):

    # Check whether the tree is empty
    if root is None:
        return True

    if index >= numberNodes:
        return False

    return (is_complete(root.left, 2 * index + 1, numberNodes)
            and is_complete(root.right, 2 * index + 2, numberNodes))


root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)

node_count = count_nodes(root)
index = 0

if is_complete(root, index, node_count):
    print("The tree is complete binary tree")
else:
    print("The tree is not complete binary tree")

Output

The tree is a complete binary tree

Program of CBT in Java

// TO Check whether a binary tree is a complete binary tree in Java

// Node creation
class Node {
  int data;
  Node left, right;

  Node(int item) {
    data = item;
    left = right = null;
  }
}

class BinaryTree {
  Node root;

  // Count the no. of nodes
  int countNumNodes(Node root) {
    if (root == null)
      return (0);
    return (1 + countNumNodes(root.left) + countNumNodes(root.right));
  }

  boolean checkComp(Node root, int index, int numberNodes) {

    if (root == null)
      return true;

    if (index >= numberNodes)
      return false;

    return (checkComp(root.left, 2 * index + 1, numberNodes)
        && checkComp(root.right, 2 * index + 2, numberNodes));
  }

  public static void main(String args[]) {
    BinaryTree tree = new BinaryTree();

    tree.root = new Node(1);
    tree.root.left = new Node(2);
    tree.root.right = new Node(3);
    tree.root.left.right = new Node(5);
    tree.root.left.left = new Node(4);
    tree.root.right.left = new Node(6);

    int node_count = tree.countNumNodes(tree.root);
    int index = 0;

    if (tree.checkComp(tree.root, index, node_count))
      System.out.println("The tree is complete binary tree");
    else
      System.out.println("The tree is not complete binary tree");
  }
}

Output

The tree is a complete binary tree

 

Read more, Perfect Binary Tree

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *