-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary search tree
More file actions
73 lines (60 loc) · 1.73 KB
/
Copy pathbinary search tree
File metadata and controls
73 lines (60 loc) · 1.73 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 3 14:10:34 2019
@author: thausmann
binary tree
"""
import time
import matplotlib.pyplot as plt
import random
class Node():
def __init__(self,value):
self.value = value
self.left = False
self.right = False
def left_right_applic(self,value,func):
if value > self.value and self.right:
return func(self.right,value)
if value <= self.value and self.left:
return func(self.left,value)
return False
def lookup(self,value):
if self.value == value:
return True
return self.left_right_applic(value,Node.lookup)
def insert(self,value):
if self.left_right_applic(value,Node.insert):
return True
if value <= self.value:
self.left = Node(value)
self.right = Node(value)
return True
def print_from_left(self):
if self.left:
self.left.print_from_left()
print(self.value)
if self.right:
self.right.print_from_left()
def median(array):
half_length = len(array)/2
if len(array)%2==0:
return array[int(half_length)]
else:
return (array[int(half_length)]+array[int(half_length)+1])/2
def test(n):
new = 0
for i in range(10000):
size = n
data = [i for i in range(size)]
top = Node(median(data))
random.shuffle(data)
for point in data:
top.insert(point)
start = time.time()
for i in range(2*size):
top.lookup(i)
stop=time.time()
new += (stop-start)/2/n
return new / 1000
plt.plot([2.714**test(n) for n in range(6,100)])