class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def count_nodes(root):
if root is None:
return 0
left_height = get_left_height(root)
right_height = get_right_height(root)
if left_height == right_height:
return (2 ** left_height) - 1
else:
return (2 ** right_height) - 1
def get_left_height(node):
height = 0
while node is not None:
height += 1
node = node.left
return height
def get_right_height(node):
height = 0
while node is not None:
height += 1
node = node.right
return height
# Example usage:
# Create a complete binary tree
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)
# Calculate the number of nodes
num_nodes = count_nodes(root)
print("Number of nodes in the complete binary tree:", num_nodes)