-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_join.py
78 lines (56 loc) · 1.49 KB
/
table_join.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
Author: James Uejio
class BinTree:
empty = ()
def __init__(self, label, left=empty, right=empty):
self.label = label
self.left = left
self.right = right
def insert(bst, n):
if bst == BinTree.empty:
return BinTree(n)
elif bst.label >= n:
return BinTree(bst.label, insert(bst.left, n), bst.right)
else:
return BinTree(bst.label, bst.left, insert(bst.right, n))
def contains(bst, elem):
if not bst:
return False
if bst.label == elem:
return True
if bst.label > elem:
return contains(bst.left, elem)
return contains(bst.right, elem)
table1 = [1, 2, 3]
table2 = [-1, 0, 3]
# Assuming no duplicates
def naive_join(table1, table2):
table3 = []
for x in table1:
for y in table2:
if x == y:
table3.append(x)
return table3
# What is the runtime in terms of N, the length of T1 and T2?
def bst_join(table1, table2):
table3 = []
# Create BST from table2
bst = BinTree.empty
for y in table2:
bst = insert(bst, y)
# Assume runtime for creating BST is n log n
for x in table1:
if contains(bst, x):
table3.append(x)
return table3
# What is the runtime?
def set_join(table1, table2):
table3 = []
# Create set from table2
t2_set = set()
for y in table2:
t2_set.add(y)
for x in table1:
if x in t2_set:
table3.append(x)
return table3
# What is the runtime?