-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeComparisonUnbalanced.py
178 lines (144 loc) · 5.82 KB
/
TreeComparisonUnbalanced.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from RB_Tree import RBTree
from ABR_Tree import ABRTree
import time
import matplotlib.pyplot as plt # Import della libreria per effettuare i grafici in Python
count_bst = 0
count_rbt = 0
elements = []
elements2 = []
times = []
times2 = []
# Classe per comparare gli alberi in cui il nostro albero ABR ha elementi totalmente sbilanciati
class TreeComparisonUnbalanced:
def __init__(self):
self.arr = []
self.bst = ABRTree() # Albero binario di ricerca
self.rbtree = RBTree() # Albero rosso nero
self.a = None
self.b = None
self.y = None
def compare_unbalanced_insertion(self):
self.arr = list(range(0, 10000))
self.y = len(self.arr)
print("----- UNBALANCED INSERT -----")
start_bst_time = time.process_time_ns()
for num in self.arr:
self.bst.insert(num)
end_bst_time = time.process_time_ns() - start_bst_time
print("BST: %s ns " % end_bst_time)
start_rbt_time = time.process_time_ns()
for num2 in self.arr:
self.rbtree.insert_node(num2)
end_rbt_time = time.process_time_ns() - start_rbt_time
print("RBT: %s ns " % end_rbt_time)
delta = end_bst_time - end_rbt_time
print("Delta: %s ns " % delta)
print("----------------------------\n")
def unbalanced_insertion_with_plot(self):
count = 0
count2 = 0
self.arr = list(range(0, 300))
self.y = len(self.arr)
for num in self.arr:
start_bst_time = time.process_time()
self.bst.insert(num)
count += 1
end_bst_time = time.process_time() - start_bst_time
elements.append(count)
times.append(end_bst_time)
for num2 in self.arr:
start_rbt_time = time.process_time()
self.rbtree.insert_node(num2)
count2 += 1
end_rbt_time = time.process_time() - start_rbt_time
elements2.append(count2)
times2.append(end_rbt_time)
plt.title("INSERIMENTO UNBALANCED")
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.xlabel("Elementi")
plt.ylabel("Tempo Operazioni")
plt.plot(elements, times, marker="o", color='red') # BST
plt.plot(elements2, times2, marker="v", color='blue') # RBT
plt.show()
def compare_unbalanced_find(self, key):
print("----- FIND -----")
start_bst_time = time.process_time_ns()
self.a = self.bst.find(key)
end_bst_time = time.process_time_ns() - start_bst_time
print("BST: %s ns " % end_bst_time)
start_rbt_time = time.process_time_ns()
self.b = self.rbtree.find(key)
end_rbt_time = time.process_time_ns() - start_rbt_time
print("RBT: %s ns " % end_rbt_time)
delta = end_bst_time - end_rbt_time
print("Delta: %s ns " % delta)
print("----------------------------\n")
def unbalanced_find_with_plot(self, key):
global count_bst, count_rbt
start_bst_time = time.process_time()
self.a = self.bst.find(key)
count_bst += 1
end_bst_time = time.process_time() - start_bst_time
elements.append(count_bst)
times.append(end_bst_time)
start_rbt_time = time.process_time()
self.b = self.rbtree.find(key)
count_rbt += 1
end_rbt_time = time.process_time() - start_rbt_time
elements2.append(count_rbt)
times2.append(end_rbt_time)
return self.a, self.b
def compare_unbalanced_successor(self):
print("----- SUCCESSOR -----")
start_bst_time = time.process_time_ns()
self.bst.successor(self.a)
end_bst_time = time.process_time_ns() - start_bst_time
print("BST: %s ns " % end_bst_time)
start_rbt_time = time.process_time_ns()
self.rbtree.successor(self.b)
end_rbt_time = time.process_time_ns() - start_rbt_time
print("RBT: %s ns " % end_rbt_time)
delta = end_bst_time - end_rbt_time
print("Delta: %s ns " % delta)
print("----------------------------\n")
def compare_unbalanced_predecessor(self):
print("----- PREDECESSOR -----")
start_bst_time = time.process_time_ns()
self.bst.predecessor(self.a)
end_bst_time = time.process_time_ns() - start_bst_time
print("BST: %s ns " % end_bst_time)
start_rbt_time = time.process_time_ns()
self.rbtree.predecessor(self.b)
end_rbt_time = time.process_time_ns() - start_rbt_time
print("RBT: %s ns " % end_rbt_time)
delta = end_bst_time - end_rbt_time
print("Delta: %s ns " % delta)
print("----------------------------\n")
def unbalanced_predecessor_with_plot(self, a, b):
global count_bst, count_rbt
start_bst_time = time.process_time()
self.bst.predecessor(a)
count_bst += 1
end_bst_time = time.process_time() - start_bst_time
elements.append(count_bst)
times.append(end_bst_time)
start_rbt_time = time.process_time()
self.rbtree.predecessor(b)
count_rbt += 1
end_rbt_time = time.process_time() - start_rbt_time
elements2.append(count_rbt)
times2.append(end_rbt_time)
def compare_unbalanced_get_root(self):
print("----- GET_ROOT -----")
start_bst_time = time.process_time_ns()
self.bst.get_root()
end_bst_time = time.process_time_ns() - start_bst_time
print("BST: %s ns " % end_bst_time)
start_rbt_time = time.process_time_ns()
self.rbtree.get_root()
end_rbt_time = time.process_time_ns() - start_rbt_time
print("RBT: %s ns " % end_rbt_time)
delta = end_bst_time - end_rbt_time
print("Delta: %s ns " % delta)
print("----------------------------\n")