-
Notifications
You must be signed in to change notification settings - Fork 2
/
he-ds-trees-LittleMonkandSwaps.py
82 lines (57 loc) · 1.69 KB
/
he-ds-trees-LittleMonkandSwaps.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
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
import sys
binary_inorder = []
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
class Tree:
def __init__(self):
self.root = None
def getRoot(self):
return self.root
def add_linearly(self, arr, node, index, size):
if (index < size):
node = Node(arr[index])
# insert left child
node.left = tree.add_linearly(arr, node.left, 2 * index + 1, size)
# insert right child
node.right = tree.add_linearly(arr, node.right, 2 * index + 2, size)
return node
def get_in_order(self, node):
# getting inorder array in var
if (node != None):
self.get_in_order(node.left)
binary_inorder.append(node.data)
self.get_in_order(node.right)
return binary_inorder
# main
if __name__ == '__main__':
try:
n = int(input().strip())
except(Exception):
n = int(sys.stdin.readline().strip())
a = (list(map(int, sys.stdin.readline().strip().split(' '))))
tree = Tree()
tree.root = tree.add_linearly(a, tree.getRoot(), 0, len(a))
binary_inorder = tree.get_in_order(tree.getRoot())
A = list(binary_inorder)
A.sort()
M = {}
for i in range(len(A)):
M[A[i]] = i
check = {}
def cycleSize(index):
size = 1
i = M[binary_inorder[index]]
check[i] = 1
while i != index:
i = M[binary_inorder[i]]
check[i] = 1
size += 1
return size
total = 0
for i in range(len(binary_inorder)):
if i not in check:
total += cycleSize(i) - 1
print(total)