-
Notifications
You must be signed in to change notification settings - Fork 8
/
complete-binary-tree.py
40 lines (32 loc) · 977 Bytes
/
complete-binary-tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Python implementation for checking if complete binary tree
class Node:
def __init__(self, item):
self.left = None
self.right = None
self.item = item
# Count number of nodes
def numNodes(root):
if root is None:
return 0
return (1 + numNodes(root.left) + numNodes(root.right))
# Check whether it is complete binary tree
def isCompleteTree(root, index, numnodes):
if root is None:
return True
if index >= numnodes:
return False
return (isCompleteTree(root.left, 2 * index + 1, numnodes)
and isCompleteTree(root.right, 2 * index + 2, numnodes))
# Driver code
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)
node_count = numNodes(root)
index = 0
if isCompleteTree(root, index, node_count):
print("The tree is a complete binary tree")
else:
print("The tree is not a complete binary tree")