-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.py
190 lines (151 loc) · 3.69 KB
/
binary_tree.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
179
180
181
182
183
184
185
186
187
188
189
190
"""
Marta Dzięgielewska
Binary tree - generating, printing, inserting, deleting
26.02.2020
"""
from timeit import default_timer as timer
from numpy.random import randint
class Node:
def __init__(self, data=None):
self.data = data
self.left = None
self.right = None
def generate(node, N):
if N == 0:
node = None
else:
node = Node()
x = randint(0, 100000)
node.data = int(x)
generate(node.left, N//2)
generate(node.right, N - N//2 - 1)
COUNT = [1]
def printtree(root, h):
if root == None:
return
h += COUNT[0]
printtree(root.right, h)
for i in range(COUNT[0], h):
print(end="....")
print(root.data)
printtree(root.left, h)
def printbin(root):
printtree(root, 0)
def insert(node, data):
if node is None:
return Node(data)
else:
if data < node.data:
node.left = insert(node.left, data)
return node
else:
if data > node.data:
node.right = insert(node.right, data)
return node
else:
pass
def minimumnode(node):
current = node
while current.left is not None:
current = current.left
return current
def delete(root, data):
if root is None:
return root
if data < root.data:
root.left = delete(root.left, data)
elif data > root.data:
root.right = delete(root.right, data)
else:
if root.left is None:
temp = root.right
root = None
return temp
elif root.right is None:
temp = root.left
root = None
return temp
temp = minimumnode(root.right)
root.data = temp.data
root.right = delete(root.right, temp.data)
return root
root = None
for i in range(20):
x = randint(0, 1000000)
root = insert(root, x)
printbin(root)
# 100 ELEMENTÓW
start = timer()
root = None
for i in range(100):
x = randint(0, 1000000)
root = insert(root, x)
end = timer()
czas = end - start
print("czas wstawiania 100 losowych elementów wyniósł: ", czas)
start = timer()
root = None
for i in range(100):
x = randint(0, 1000000)
root = delete(root, x)
printbin(root)
end = timer()
czas = end - start
print("czas usuwania 100 losowych elementów wyniósł: ", czas)
print()
# 1000 ELEMENTÓW
start = timer()
root = None
for i in range(1000):
x = randint(0, 1000000)
root = insert(root, x)
end = timer()
czas = end - start
print("czas wstawiania 1000 losowych elementów wyniósł: ", czas)
start = timer()
root = None
for i in range(1000):
x = randint(0, 1000000)
root = delete(root, x)
printbin(root)
end = timer()
czas = end - start
print("czas usuwania 1000 losowych elementów wyniósł: ", czas)
print()
# 10000 ELEMENTÓW
start = timer()
root = None
for i in range(10000):
x = randint(0, 1000000)
root = insert(root, x)
end = timer()
czas = end - start
print("czas wstawiania 10000 losowych elementów wyniósł: ", czas)
start = timer()
root = None
for i in range(10000):
x = randint(0, 1000000)
root = delete(root, x)
printbin(root)
end = timer()
czas = end - start
print("czas usuwania 10000 losowych elementów wyniósł: ", czas)
print()
#100000 ELEMENTÓW
start = timer()
root = None
for i in range(100000):
x = randint(0, 1000000)
root = insert(root, x)
end = timer()
czas = end - start
print("czas wstawiania 100000 losowych elementów wyniósł: ", czas)
start = timer()
root = None
for i in range(100000):
x = randint(0, 1000000)
root = delete(root, x)
printbin(root)
end = timer()
czas = end - start
print("czas usuwania 100000 losowych elementów wyniósł: ", czas)