# Checking if a binary tree is a perfect binary tree in Python class newNode: def __init__(self, k): self.key = k self.right = self.left = None # calculate the depth of the tree def calculateDepth(node): if node is None: return 0 left_depth = calculateDepth(node.left) right...
Python Java C C++ # Binary Tree in Python class Node: def __init__(self, key): self.left = None self.right = None self.val = key # Traverse preorder def traversePreOrder(self): print(self.val, end=' ') if self.left: self.left.traversePreOrder() if self.right: self.right.tra...
Python, Java and C/C++ Examples Python Java C C++ # Checking 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 number of nodes def count_nodes(root): if root is None: ...
# Binary Search Tree operations in Python# Create a nodeclassNode:def__init__(self, key):self.key = key self.left =Noneself.right =None# Inorder traversaldefinorder(root):ifrootisnotNone:# Traverse leftinorder(root.left)# Traverse rootprint(str(root.key) +"->", end=' ')# Traverse...
Python Java C C++ # Checking if a binary tree is height balanced in Python class Node: def __init__(self, data): self.data = data self.left = self.right = None class Height: def __init__(self): self.height = 0 def isHeightBalanced(root, height): left_height = Height() right...