Skip to content

Commit da4ce6b

Browse files
committed
Splaying Trees
1 parent a337467 commit da4ce6b

File tree

2 files changed

+72
-38
lines changed

2 files changed

+72
-38
lines changed

BinarySearchTrees/SplayTrees.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import TreeMap
2+
3+
class SplayTreeMap(TreeMap):
4+
"""Sorted map implementation using a splay tree"""
5+
6+
def _splay(self, p):
7+
8+
while p != self.root():
9+
parent = self.parent(p) # y is parent of x
10+
grand = self.parent(parent) # z is parent of y
11+
12+
if grand is None:
13+
# if there is no z (grand father)
14+
# zig-zag case
15+
self._rotate(p) # from treeMap class
16+
elif (parent == self.left(grand)) == (p == self.left(parent)):
17+
# zig-zig case
18+
self._rotate(parent) # move parent up
19+
self._rotate(p) # move child p (x) up to the root
20+
else:
21+
#zig-zag case
22+
self._rotate(p) # move p up
23+
self._rotate(p) #move p up again
24+
25+
##### OVERRIDE BALANCING HOOKS #######
26+
def _rebalance_insert(self,p):
27+
self._splay(p)
28+
29+
def _rebalance_delete(self, p):
30+
if p is not None:
31+
self._splay(p)
32+
33+
def _rebalance_access(self, p):
34+
self._splay(p)

BinarySearchTrees/TreeMap.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -212,48 +212,48 @@ def __delitem__(self, k):
212212

213213
######### Search Tree Balacing Algorithms for (AVL, Splay Tree, RedBlack Trees) not used by this class, but implemented here for inheritance ######
214214

215-
def _relink(self, parent, child, make_left_child):
216-
"""Relink aprent node with child node (we allow child to be None)"""
215+
def _relink(self, parent, child, make_left_child):
216+
"""Relink aprent node with child node (we allow child to be None)"""
217217

218-
if make_left_child:
219-
parent._left = child #make it a left child
220-
else:
221-
parent._right = child # make it a right child
222-
223-
if child is not None:
224-
child._parent = parent # make child point to parent
218+
if make_left_child:
219+
parent._left = child #make it a left child
220+
else:
221+
parent._right = child # make it a right child
225222

226-
def _rotate(self, p):
227-
"""rotate position p above its parent"""
228-
x = p._node
229-
y = x._parent #assume this exists
230-
z= y._parent # grand parent
223+
if child is not None:
224+
child._parent = parent # make child point to parent
231225

232-
if z is None:
233-
self._root = x #x becomes root if grand parent z is None
234-
x._parent = None
235-
else:
236-
self._relink(z,x, y == z._left) # x becomes a direct child of z
237-
238-
# now rotate x and y, including transfer of middle subtree
239-
if x == y._left:
240-
self._relink(y, x._right, True) # x._right becomes left child of y
241-
self._relink(x, y, False)
242-
else:
243-
self._relink(y, x._left, False) # x._left becomes right child of y
244-
self._relink(x , y, True) # Y becomes left child of x
226+
def _rotate(self, p):
227+
"""rotate position p above its parent"""
228+
x = p._node
229+
y = x._parent #assume this exists
230+
z= y._parent # grand parent
245231

246-
def _restructure(self, x):
247-
"""Perform Trinode Restructuring of Position x with parent/grandparent"""
232+
if z is None:
233+
self._root = x #x becomes root if grand parent z is None (does not exist)
234+
x._parent = None
235+
else:
236+
self._relink(z,x, y == z._left) # x becomes a direct child of z
237+
238+
# now rotate x and y, including transfer of middle subtree
239+
if x == y._left:
240+
self._relink(y, x._right, True) # x._right becomes left child of y
241+
self._relink(x, y, False)
242+
else:
243+
self._relink(y, x._left, False) # x._left becomes right child of y
244+
self._relink(x , y, True) # Y becomes left child of x
245+
246+
def _restructure(self, x):
247+
"""Perform Trinode Restructuring of Position x with parent/grandparent"""
248248

249-
y= self.parent(x)
250-
z = self.parent(y)
249+
y= self.parent(x)
250+
z = self.parent(y)
251251

252-
if (x == self.right(y)) == (y== self.right(z)): #matching alignments
253-
self._rotate(y) #single rotation
254-
return y # y is the new subtree root
255-
else:
256-
self._rotate(x) #double rotation
257-
self._rotate(x)
252+
if (x == self.right(y)) == (y== self.right(z)): #matching alignments
253+
self._rotate(y) #single rotation
254+
return y # y is the new subtree root
255+
else:
256+
self._rotate(x) #double rotation
257+
self._rotate(x)
258258

259-
return x
259+
return x

0 commit comments

Comments
 (0)