Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Tree
  • Practice Tree
  • MCQs on Tree
  • Tutorial on Tree
  • Types of Trees
  • Basic operations
  • Tree Traversal
  • Binary Tree
  • Complete Binary Tree
  • Ternary Tree
  • Binary Search Tree
  • Red-Black Tree
  • AVL Tree
  • Full Binary Tree
  • B-Tree
  • Advantages & Disadvantages
Open In App
Next Article:
Count number of nodes in a complete Binary Tree
Next article icon

Number of edges in mirror image of Complete binary tree

Last Updated : 08 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a complete binary tree of depth H. If the mirror image from the left and the right side of this tree is taken then: 
 

Right Mirrored Image: Rightmost node of the every level is connected to mirrored corresponding node. 
Left Mirrored Image: Left most node of the every level is connected to mirrored corresponding node. 
 


The task is to find the number of edges after taking both the mirror images in the final tree.
 


Examples: 
 

Input: H = 1 
Output: 10 
2 edges in the original tree will get mirrored in the mirror images (left and right) i.e. 6 edges in total. 
And the edges connecting the mirror images with the original tree as shown in the image above.
Input: H = 2 
Output: 24 
(6 * 3) + 3 + 3 = 24 


Approach: Maintain the leftmost, rightmost nodes after each mirror image. Number of edges will change after each operation of mirror image. Initially, 
[Tex]$$ No.\hspace{1mm} of\hspace{1mm} nodes = 2^{(H+1)}-1 $$ $$ No.\hspace{1mm} Of\hspace{1mm} edges = 2\times(2^{H}-1}) $$ $$ No.\hspace{1mm} of\hspace{1mm} Left\hspace{1mm} side\hspace{1mm} nodes = H+1 $$ $$ No.\hspace{1mm} of\hspace{1mm} Right\hspace{1mm} side\hspace{1mm} nodes = H+1 $$    [/Tex]
After right mirrored image: 
[Tex]$$ No.\hspace{1mm} Of\hspace{1mm} edges = (Initial\hspace{1mm}edges\times 2+rightmost \hspace{1mm}nodes) $$    [/Tex]
After left mirrored image: 
[Tex]$$ No.\hspace{1mm} Of\hspace{1mm} edges = (Initial\hspace{1mm}edges\times 2+leftmost \hspace{1mm}nodes) $$    [/Tex]
In complete modified tree: 
[Tex]$$ No.\hspace{1mm} Of\hspace{1mm} edges = (Initial\hspace{1mm}edges\times 3+leftmost \hspace{1mm}nodes+rightmost \hspace{1mm}nodes) $$  [/Tex]

Algorithm:

Step 1: Start
Step 2: Create a function named “countEdges” of int return type and takes an integer value as a parameter.
Step 3: Now, in the “countEdges” function let’s declare three variables to store integer values
         named “edges”, “right”, and “left”.
Step 4: Using the formula 2*(pow()2, H)-1), determine the total number of edges in the entire binary tree of height ‘H’.
Step 5: Use formula 2 to determine how many nodes are in the final level of the entire binary tree (H).
Step 6: Set ‘left’ and ‘right’ variables to H+1.
Step 7: Now, let’s calculate the total number of edges in the modified tree by applying the below formula:
              cnt = (edges * 3) + left + right;
Step 8: Return the value
Step 9: End.


Below is the implementation of the above approach: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the total number
// of edges in the modified tree
int countEdges(int H)
{
 
    int edges, right, left;
    edges = 2 * (pow(2, H) - 1);
    left = right = H + 1;
 
    // Total edges in the modified tree
    int cnt = (edges * 3) + left + right;
    return cnt;
}
 
// Driver code
int main()
{
    int H = 1;
 
    cout << countEdges(H);
 
    return 0;
}
                      
                       

Java

// Java implementation of the approach
import java.io.*;
 
class GFG {
 
    // Function to return the total number
    // of edges in the modified tree
    static int countEdges(int H)
    {
 
        int edges, right, left;
        edges = 2 * (int)(Math.pow(2, H) - 1);
        left = right = H + 1;
 
        // Total edges in the modified tree
        int cnt = (edges * 3) + left + right;
        return cnt;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int H = 1;
        System.out.println(countEdges(H));
    }
}
 
// This code has been contributed by anuj_67..
                      
                       

Python 3

# Python 3 implementation of the approach
 
# Function to return the total number
# of edges in the modified tree
def countEdges( H):
 
    edges = 2 * (pow(2, H) - 1)
    left = right = H + 1
 
    # Total edges in the modified tree
    cnt = (edges * 3) + left + right
    return cnt
 
# Driver code
if __name__ == "__main__":
    H = 1;
 
    print(countEdges(H))
 
# This code is contributed by ChitraNayal
                      
                       

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the total number
    // of edges in the modified tree
    static int countEdges(int H)
    {
 
        int edges, right, left;
         
        edges = 2 * (int)(Math.Pow(2, H) - 1);
        left = right = H + 1;
 
        // Total edges in the modified tree
        int cnt = (edges * 3) + left + right;
        return cnt;
    }
 
    // Driver code
    public static void Main()
    {
        int H = 1;
        Console.WriteLine(countEdges(H));
    }
 
}
 
// This code is contributed by AnkitRai01
                      
                       

Javascript

<script>
    // Javascript implementation of the approach
     
    // Function to return the total number
    // of edges in the modified tree
    function countEdges(H)
    {
   
        let edges, right, left;
           
        edges = 2 * (Math.pow(2, H) - 1);
        left = right = H + 1;
   
        // Total edges in the modified tree
        let cnt = (edges * 3) + left + right;
        return cnt;
    }
     
    let H = 1;
      document.write(countEdges(H));
 
</script>
                      
                       

Output: 
10

 

Time Complexity : O(1)

Auxiliary Space: O(1)



Next Article
Count number of nodes in a complete Binary Tree

S

SURENDRA_GANGWAR
Improve
Article Tags :
  • Binary Search Tree
  • DSA
  • Tree
  • Complete Binary Tree
Practice Tags :
  • Binary Search Tree
  • Tree

Similar Reads

  • Count number of nodes in a complete Binary Tree
    Given the root of a Complete Binary Tree consisting of N nodes, the task is to find the total number of nodes in the given Binary Tree. Examples: Input: Output: 7 Input: Output: 5 Native Approach: The simple approach to solving the given tree is to perform the DFS Traversal on the given tree and cou
    15+ min read
  • Find mirror of a given node in Binary tree
    Given a Binary tree, the problem is to find the mirror of a given node. The mirror of a node is a node which exists at the mirror position of the node in opposite subtree at the root. Examples: In above tree- Node 2 and 3 are mirror nodes Node 4 and 6 are mirror nodes. Recommended PracticeMirror of
    15+ min read
  • Minimum nodes to be removed to make a Binary tree complete
    Given a binary tree with positive values of nodes, find the minimum number of nodes that need to be removed to transform it into a complete binary tree. Return 0 if the given tree is already complete. Note: A complete binary tree is a special type of binary tree where all the levels of the tree are
    15+ min read
  • Maximize number of edges added to convert given Tree into a Bipartite Graph
    Given a tree of N nodes, the task is to find the maximum number of edges that can be added to the tree such that it becomes a bipartite graph. Note: Self loop or multiple edges are not allowed but cycles are permitted. Examples: Input: N = 4, Edges = {{1, 2}, {2, 3}, {1, 4}} 1 / \ 2 4 / 3Output: 1Ex
    13 min read
  • Find the largest Complete Subtree in a given Binary Tree
    Given a Binary Tree, the task is to find the size and also the inorder traversal of the largest Complete sub-tree in the given Binary Tree. Complete Binary Tree - A Binary tree is a Complete Binary Tree if all levels are filled except possibly the last level and the last level has all keys as left a
    13 min read
  • Number of edges in a perfect binary tree with N levels
    Given a positive integer N, the task is to find the count of edges of a perfect binary tree with N levels.Examples: Input: N = 2 Output: 2 1 / \ 2 3 Input: N = 3 Output: 6 1 / \ 2 3 / \ / \ 4 5 6 7 Approach: It can be observed that for the values of N = 1, 2, 3, ..., a series will be formed as 0, 2,
    2 min read
  • Number of ways to divide a Binary tree into two halves
    Given a binary tree, the task is to count the number of ways to remove a single edge from the tree such that the tree gets divided into two halves with equal sum.Examples: Input: 1 / \ -1 -1 \ 1 Output: 1 Only way to do this will be to remove the edge from the right of the root. After that we will g
    10 min read
  • Print path from root to all nodes in a Complete Binary Tree
    Given a number N which is the total number of nodes in a complete binary tree where nodes are number from 1 to N sequentially level-wise. The task is to write a program to print paths from root to all of the nodes in the Complete Binary Tree.For N = 3, the tree will be: 1 / \ 2 3 For N = 7, the tree
    9 min read
  • Count the number of visible nodes in Binary Tree
    Given a Binary tree, the task is to find the number of visible nodes in the given binary tree. A node is a visible node if, in the path from the root to the node N, there is no node with greater value than N’s, Examples: Input: 5 / \ 3 10 / \ / 20 21 1 Output: 4 Explanation: There are 4 visible node
    7 min read
  • Count frequency of K in given Binary Tree
    Given a binary tree of N nodes. Count the frequency of an integer K in the binary tree. Examples: Input: N = 7, K = 2 1 / \ 2 3 / \ / \4 2 2 5Output: 3Explanation: 2 occurs 3 times in the tree. Input: N = 6, K = 5 1 / \ 4 5 / \ / \5 6 2 4Output: 2Explanation: 5 occurs 2 times in the tree. Approach:
    14 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences