Repository navigation
Expand file tree
/
Copy pathComplete_Binary_Tree.py
More file actions
88 lines (79 loc) · 3.09 KB
/
Copy pathComplete_Binary_Tree.py
File metadata and controls
88 lines (79 loc) · 3.09 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def add (self, data):
if self.data == data:
return
if self.data > data:
if self.left:
self.left.add(data)
else:
self.left = Node(data)
else:
if self.right:
self.right.add(data)
else:
self.right = Node(data)
class Binary_Tree:
def __init__(self):
self.root = None
def is_complete_tree(self, root):
if root is None:
return True
queue = []
queue.append(root)
# This flag is used to indicate if we have encountered a node with missing children
flag = False
while queue:
temp = queue.pop(0)
# Check the left child
if temp.left:
# If we have already encountered a node with missing children, the tree is incomplete
if flag:
return False
queue.append(temp.left) # Add the left child to the queue
else:
# If there is no left child, set the flag to True
flag = True
# Check the right child
if temp.right:
# If the flag is already set, and we find a right child, it's incomplete
if flag:
return False
queue.append(temp.right) # Add the right child to the queue
else:
# If there is no right child, set the flag to True
flag = True
return True
"""
- We use level-order traversal (using a queue) to check if the tree is complete.
- The `flag` is used to ensure that once a node has missing children, all nodes afterward must be leaf nodes.
- If we encounter a child after the `flag` is set, the tree is incomplete and we return False.
- If no invalid structure is found during traversal, the tree is complete, and we return True.
"""
if __name__ == '__main__':
tree = Binary_Tree()
tree.root = Node(1)
tree.root.left = Node(2)
tree.root.right = Node(3)
tree.root.left.left = Node(4)
tree.root.left.right = Node(5)
tree.root.right.left = Node(6)
tree.root.right.right = Node(7)
print(tree.is_complete_tree(tree.root))
tree.root.right.right.right = Node(8)
print(tree.is_complete_tree(tree.root))
tree.root.right.right.right.left = Node(9)
print(tree.is_complete_tree(tree.root))
tree.root.right.right.right.right = Node(10)
print(tree.is_complete_tree(tree.root))
tree.root.right.right.right.right.right = Node(11)
print(tree.is_complete_tree(tree.root))
tree.root.right.right.right.right.right.left = Node(12)
print(tree.is_complete_tree(tree.root))
tree.root.right.right.right.right.right.right = Node(13)
print(tree.is_complete_tree(tree.root))
tree.root.right.right.right.right.right.right.left = Node(14)
print(tree.is_complete_tree(tree.root))