-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
181 lines (134 loc) · 4.89 KB
/
node.py
File metadata and controls
181 lines (134 loc) · 4.89 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
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
from bin import Bin
class Node:
def __init__(self,size,ID):
self.si = size
self.ID = ID
self.l=[]
self.left = None
self.right = None
self.height = 1
self.M = AVL()
def add_object(self,object_id,size):
self.si-=size
self.l.append(object_id)
class NodeOb:
def __init__(self,ID):
self.ID = ID
self.left = None
self.right = None
self.height = 1
def comp_2(node_1,node_2):
return node_1.ID < node_2.ID
class AVL:
def __init__(self):
compare_function = comp_2
self.root = None
self.ID = 0
self.comparator = compare_function
def height(self,node):
if not node:
return 0
else:
return node.height
def balance(self,node):
if not node:
return 0
else:
return self.height(node.left) - self.height(node.right)
def insert(self,root,node):
if root is None:
node.left = None
node.right = None
node.height = 1
return node
else:
if self.comparator(node,root) :
root.left = self.insert(root.left,node)
elif self.comparator(root,node):
root.right = self.insert(root.right,node)
root.height = 1 + max(self.height(root.left), self.height(root.right))
balance = self.balance(root)
#print(f"Balance factor at node {root.si}: {balance}")
if balance > 1 and self.comparator(node,root.left):
#print(f"Right rotation at node {root.si}")
root = self.rightrotation(root)
if balance < -1 and self.comparator(root.right,node):
#print(f"Left rotation at node {root.si}")
root = self.leftrotation(root)
if balance > 1 and self.comparator(root.left,node):
root.left = self.leftrotation(root.left)
root= self.rightrotation(root)
if balance < -1 and self.comparator(node,root.right):
root.right = self.rightrotation(root.right)
root = self.leftrotation(root)
return root
def delete(self, root, node):
if not root:
return root
if self.comparator(node,root):
root.left = self.delete(root.left, node)
elif self.comparator(root,node):
root.right = self.delete(root.right, node)
else:
if not root.left:
temp = root.right
root = None
return temp
elif not root.right:
temp = root.left
root = None
return temp
temp = self.min_value_node(root.right)
root.right = self.delete(root.right, temp)
root.ID = temp.ID
temp.eft = root.left
temp.right = root.right
temp.height = root.height
if root==None:
return root
root.height = 1 + max(self.height(root.left), self.height(root.right))
#print(root.height)
balance = self.balance(root)
#print(balance)
# Left rotation
if balance > 1 and self.balance(root.left) >= 0:
root = self.rightrotation(root)
# Right rotation
if balance < -1 and self.balance(root.right) <= 0:
root = self.leftrotation(root)
# Left-Right rotation
if balance > 1 and self.balance(root.left) < 0:
root.left = self.leftrotation(root.left)
root = self.rightrotation(root)
# Right-Left rotation
if balance < -1 and self.balance(root.right) > 0:
root.right = self.rightrotation(root.right)
root = self.leftrotation(root)
return root
def leftrotation(self, z):
y = z.right
T2 = y.left
y.left = z
z.right = T2
z.height = 1 + max(self.height(z.left), self.height(z.right))
y.height = 1 + max(self.height(y.left), self.height(y.right))
return y
def rightrotation(self, z):
y = z.left
T3 = y.right
y.right = z
z.left = T3
z.height = 1 + max(self.height(z.left), self.height(z.right))
y.height = 1 + max(self.height(y.left), self.height(y.right))
return y
def min_value_node(self, root):
current = root
while current.left:
current = current.left
return current
def search(self, root, value,a):
if not root or root.ID == value:
return root
if root.ID < value:
return self.search(root.right, value,0)
return self.search(root.left, value,0)