Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Perfect Binary Tree

Perfect Binary Tree | DSA in C++ - Software Development PDF Download

Introduction


A binary tree is a widely used data structure in computer science. It consists of nodes connected through edges, where each node can have at most two children. In this article, we will explore a special type of binary tree known as a "Perfect Binary Tree." We will discuss what makes a binary tree perfect, how to identify one, and provide examples and code explanations to reinforce the concepts.

What is a Perfect Binary Tree?


A perfect binary tree is a type of binary tree where all internal nodes have exactly two children, and all leaf nodes are at the same level. This means that every level of the tree, except the last, is completely filled with nodes.

Characteristics of a Perfect Binary Tree


  • All internal nodes have two children.
  • All leaf nodes (also known as external nodes) are at the same level.
  • Each level, except the last, is completely filled with nodes.
  • The total number of nodes in a perfect binary tree can be calculated using the formula: 2(height+1) - 1, where height is the height of the tree.

Identifying a Perfect Binary Tree


To determine if a binary tree is perfect, we need to check its structural properties. Here's a simple algorithm to identify a perfect binary tree:

  • Start from the root node and calculate the height of the left and right subtrees.
  • If the height of the left and right subtrees is the same and each subtree is also perfect, then the binary tree is perfect.

Creating a Perfect Binary Tree


Let's see an example of how to create a perfect binary tree using a C++ code snippet:

#include <iostream>

// Node structure

struct Node {

    int data;

    Node* left;

    Node* right;

};

// Function to create a new node

Node* createNode(int value) {

    Node* newNode = new Node();

    newNode->data = value;

    newNode->left = nullptr;

    newNode->right = nullptr;

    return newNode;

}

// Function to create a perfect binary tree

Node* createPerfectBinaryTree(int depth) {

    if (depth == 0)

        return nullptr;

    Node* root = createNode(1);

    root->left = createPerfectBinaryTree(depth - 1);

    root->right = createPerfectBinaryTree(depth - 1);

    return root;

}

int main() {

    int depth = 3;

    Node* root = createPerfectBinaryTree(depth);

    // Print the tree

    std::cout << "Perfect Binary Tree:\n";

    std::cout << "       1\n";

    std::cout << "    /     \\\n";

    std::cout << "   2       2\n";

    std::cout << "  / \\     / \\\n";

    std::cout << " 3   3   3   3\n";

    return 0;

}

Output:

Perfect Binary Tree:

       1

    /     \

   2       2

  / \     / \

 3   3   3   3

Explanation:

  • In the 'createPerfectBinaryTree' function, we recursively create nodes for the left and right subtrees.
  • The 'depth' parameter represents the height of the tree. In this example, we set it to 3, resulting in a perfect binary tree with four levels.
  • The output illustrates the structure of the perfect binary tree created.

Sample Problems and Solutions


Here are some sample problems related to perfect binary trees:

Problem 1: Count the total number of nodes in a perfect binary tree of height 4.

Using the formula, 2^(4+1) - 1 = 31. Hence, the total number of nodes is 31.

Problem 2: Given a binary tree, determine if it is a perfect binary tree.

Calculate the height of the left and right subtrees. If they are the same and each subtree is perfect, then the binary tree is perfect.

Conclusion

Perfect binary trees are special binary trees with specific structural characteristics. They have various applications in computer science and are useful for understanding tree-based algorithms. By identifying and creating perfect binary trees, you can enhance your understanding of binary tree concepts and problem-solving skills in C++.

The document Perfect Binary Tree | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Important questions

,

Free

,

Exam

,

video lectures

,

Sample Paper

,

past year papers

,

Semester Notes

,

practice quizzes

,

MCQs

,

study material

,

pdf

,

Perfect Binary Tree | DSA in C++ - Software Development

,

Extra Questions

,

Perfect Binary Tree | DSA in C++ - Software Development

,

ppt

,

shortcuts and tricks

,

Viva Questions

,

Perfect Binary Tree | DSA in C++ - Software Development

,

Objective type Questions

,

mock tests for examination

,

Previous Year Questions with Solutions

,

Summary

;