Reputation: 11
I want to empirically show that a Red-Black tree has an average height of logn.
Why am I getting an "TypeError: height_rb() takes 2 positional arguments but 3 were given"?
import numpy as np
import pandas as pd
from random import randint
from time import perf_counter
from matplotlib import pyplot as plt
RED, BLACK = 'R', 'B'
# Tnil necessary since code has reference assignments like y.right.p
class Tn:
def __init__(self):
self.p = None
self.color = BLACK
Tnil = Tn()
# All references are assigned to Tnil
class RBNode:
def __init__(self, value):
self.value = value
self.left = Tnil
self.right = Tnil
self.p = None
self.color = None
self.height = None
def height_rb(self, node):
if node is None:
return -1
return max(self.height_rb(node.left), self.height_rb(node.right)) + 1
# For measuring insertion cost of Red-Black tree
def new_rb_val(_n): # So there will be collisions
return randint(0, _n / 10), randint(0, _n)
# Empirical Time Complexity
def measure_cost(n_runs, height_f, node_f):
ds = None
t = []
for n in n_runs:
runs = []
for j in range(10): # reduce the variation of the measurement
ds = None # starting from an empty data structure
st = perf_counter()
for i in range(n):
ds = height_f(ds, *node_f(n))
runs += [perf_counter() - st]
t += [np.mean(runs)]
print('clock: ', ' '.join(['{:g}'.format(v) for v in t]))
# ds dataset can be used for search
return t, ds
N_RUNS = [10, 100, 500, 700, 1000, 2000, 3000, 5000, 7000, 9000, 10000]
t, ds = measure_cost(N_RUNS, height_rb, new_rb_val)
Error:
TypeError: height_rb() takes 2 positional arguments but 3 were given
Upvotes: 0
Views: 202
Reputation: 4378
The full stacktrace (that you did not provided) :
Traceback (most recent call last):
File "C:/PycharmProjects/stack_overflow/68002339.py", line 64, in <module>
t, ds = measure_cost(N_RUNS, height_rb, new_rb_val)
File "C:/PycharmProjects/stack_overflow/68002339.py", line 52, in measure_cost
ds = height_f(ds, *node_f(n))
TypeError: height_rb() takes 2 positional arguments but 3 were given
You call measure_cost(N_RUNS, height_rb, new_rb_val)
.
On the line ds = height_f(ds, *node_f(n))
, height_f
is height_rb
and you call it with (None, 0, 10)
because node_f(n)
evaluates to (0, 10)
.
So indeed you call height_rb()
with three parameters.
This is your error, now you have to find how to fix it.
Upvotes: 0