I'm trying to get the size of tree of this code.
I know Size of a tree = Size of left subtree + 1 + Size of right subtree, but I do not know how to implement with this code.
I want to create a function called size after the program end I call this function to print the size of tree.
# Initial values of Alpha and Beta
MAX, MIN = 1000, -1000
# Returns optimal value for current player
#(Initially called for root and maximizer)
def minimax(depth, nodeIndex, maximizingPlayer,
values, alpha, beta):
# Terminating condition. i.e
# leaf node is reached
if depth == 3:
return values[nodeIndex]
if maximizingPlayer:
best = MIN
# Recur for left and right children
for i in range(0, 2):
val = minimax(depth + 1, nodeIndex * 2 + i,
False, values, alpha, beta)
best = max(best, val)
alpha = max(alpha, best)
# Alpha Beta Pruning
if beta <= alpha:
break
return best
else:
best = MAX
# Recur for left and
# right children
for i in range(0, 2):
val = minimax(depth + 1, nodeIndex * 2 + i,
True, values, alpha, beta)
best = min(best, val)
beta = min(beta, best)
# Alpha Beta Pruning
if beta <= alpha:
break
return best
# Driver Code
if __name__ == "__main__":
values = [3, 5, 6, 9, 1, 2, 0, -1]
print("The optimal value is :", minimax(0, 0, True, values, MIN, MAX))
# This code is contributed by Rituraj Jain
```
`
There is an example how to get the size of tree but I'm not sure how to implement with above code.
class Node:
Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
Computes the number of nodes in tree
def size(node):
if node is None:
return 0
else:
return (size(node.left)+ 1 + size(node.right))
Driver program to test above function
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Size of the tree is %d" %(size(root)))