
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find the largest Complete Subtree in a given Binary Tree in Python
Suppose we have a Binary Tree; we have to find the size of maximum complete sub-tree in this Binary Tree. As we know a complete binary tree is a Binary Tree if all levels are completely filled without possibly the final level and the final level has all keys as left as possible.
So, if the input is like
then the output will be 4 as size and inorder traversal will be 10, 45, 60, 70,
To solve this, we will follow these steps −
- Define return type with few parameters like isComplete, isPerfect, these are initially false, then size and rootTree, size is initially 0 and rootTree is null.
- ret_type := returnType
- if root is null, then
- ret_type.isPerfect := True
- ret_type.isComplete := True
- ret_type.size := 0
- ret_type.rootTree := None
- return ret_type
- left_tree := checkCompleteness(root.left)
- right_tree := checkCompleteness(root.right)
- if (left_tree.isPerfect is True and right_tree.isComplete is True and height of left and right tree are same, then
- ret_type.isComplete := True
- ret_type.isPerfect := right_tree.isPerfect
- ret_type.size := left_tree.size + right_tree.size + 1
- ret_type.rootTree := root
- return ret_type
- if (left_tree.isComplete is True and right_tree.isPerfect is True and height of left and right tree are same, then
- ret_type.isComplete := True
- ret_type.isPerfect := False
- ret_type.size := left_tree.size + right_tree.size + 1
- ret_type.rootTree := root
- return ret_type
- ret_type.isPerfect := False
- ret_type.isComplete := False
- ret_type.size := maximum of left_tree.size, right_tree.size
- if left_tree.size > right_tree.size, then
- ret_type.rootTree := left_tree.rootTree
- otherwise,
- ret_type.rootTree := right_tree.rootTree
- return ret_type
Python
Let us see the following implementation to get better understanding −
import math class TreeNode: def __init__(self, data, left = None, right = None): self.data = data self.left = left self.right = right class returnType : def __init__(self): self.isPerfect = None self.isComplete = None self.size = 0 self.rootTree = None def getHeight(size): return int(math.ceil(math.log(size + 1)/math.log(2))) def checkCompleteness(root) : ret_type = returnType() if (root == None): ret_type.isPerfect = True ret_type.isComplete = True ret_type.size = 0 ret_type.rootTree = None return ret_type left_tree = checkCompleteness(root.left) right_tree = checkCompleteness(root.right) if (left_tree.isPerfect == True and right_tree.isComplete == True and getHeight(left_tree.size) == getHeight(right_tree.size)) : ret_type.isComplete = True ret_type.isPerfect = right_tree.isPerfect ret_type.size = left_tree.size + right_tree.size + 1 ret_type.rootTree = root return ret_type if (left_tree.isComplete == True and right_tree.isPerfect == True and getHeight(left_tree.size) == getHeight(right_tree.size) + 1): ret_type.isComplete = True ret_type.isPerfect = False ret_type.size = left_tree.size + right_tree.size + 1 ret_type.rootTree = root return ret_type ret_type.isPerfect = False ret_type.isComplete = False ret_type.size =max(left_tree.size, right_tree.size) if(left_tree.size > right_tree.size ): ret_type.rootTree = left_tree.rootTree else: ret_type.rootTree = right_tree.rootTree return ret_type def print_tree(root): if root is not None: print_tree(root.left) print(root.data, end = ', ') print_tree(root.right) root = TreeNode(50) root.left = TreeNode(30) root.right = TreeNode(60) root.left.left = TreeNode(5) root.left.right = TreeNode(20) root.right.left = TreeNode(45) root.right.right = TreeNode(70) root.right.left.left = TreeNode(10) ans = checkCompleteness(root) print( "Size:" , ans.size ) print("Inorder Traversal: ", end = '') print_tree(ans.rootTree)
Input
root = TreeNode(50) root.left = TreeNode(30) root.right = TreeNode(60) root.left.left = TreeNode(5) root.left.right = TreeNode(20) root.right.left = TreeNode(45) root.right.right = TreeNode(70) root.right.left.left = TreeNode(10)
Output:
Size: 4 Inorder Traversal: 10, 45, 60, 70,
- Related Articles
- Find the largest Complete Subtree in a given Binary Tree in C++
- Find the largest Perfect Subtree in a given Binary Tree in Python
- Program to find largest binary search subtree from a given tree in Python
- Find the largest BST subtree in a given Binary Tree - Set 1 in C++
- Find largest subtree sum in a tree in C++
- Program to find most frequent subtree sum of a binary tree in Python
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Check if a binary tree is subtree of another binary tree in C++
- C++ Program to Find Size of the Largest Independent Set(LIS) in a Given a Binary Tree
- Python Program To Find the Smallest and Largest Elements in the Binary Search Tree
- Program to find largest sum of any path of a binary tree in Python
- Complete Binary Tree Inserter in C++
- Largest BST in a Binary Tree in C++
- Find the Kth node in the DFS traversal of a given subtree in a Tree in C++
- Construct a complete binary tree from given array in level order fashion in C++

Advertisements