-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplay_tree_insert.py
136 lines (118 loc) · 3.96 KB
/
splay_tree_insert.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
# Python implementation of Splay Tree
# insertion. A splay tree is a self-balancing
# data structure where the last accessed key is
# always at the root. The insertion operation
# is similar to a Binary Search Tree insert with
# some additional steps to ensure that the newly
# inserted ket becomes the new root and the splay
# structure is maintained.
#
# The following are different cases of inserting
# a key k into a splay tree.
# 1) Root is NULL. Simply allocate a new node and
# return it is root.
# 2) Splay the given key k. If k is already present, then
#it becomes the new root. If not, then the last accessed
# node becomes the new root.
# Node class to represent each node in the splay tree
# 3) If a new root's key is the same as k, do nothing,
# 4) Else allocate memory for new node and compare root's
# key with k.
# 4a) If k is smaller than root's key, make root the right
#child of new node, copy left child of root as left child of
# new node and set left child of root to NULL.
# 4b) If k is greater than root's ket, make toor the left
# child of new node, copy right child of root as right child
# of new node and make set right child of root to NULL.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
# Splay tree class
class SplayTree:
def __init__(self):
self.root = None
# Function to perform a zig rotation
def zig(self, x):
y = x.left
x.left = y.right
y.right = x
return y
# Function to perform a zag rotation
def zag(self, x):
y = x.right
x.right = y.left
y.left = x
return y
# Function to perform splay operation
def splay(self, key):
if self.root is None or self.root.key == key:
return
dummy = Node(None)
left_tree = dummy
right_tree = dummy
curr = self.root
while True:
if key < curr.key:
if curr.left is None:
break
if key < curr.left.key:
curr = self.zig(curr)
if curr.left is None:
break
right_tree.left = curr
right_tree = curr
curr = curr.left
right_tree.left = None
elif key > curr.key:
if curr.right is None:
break
if key > curr.right.key:
curr = self.zag(curr)
if curr.right is None:
break
left_tree.right = curr
left_tree = curr
curr = curr.right
left_tree.right = None
else:
break
left_tree.right = curr.left
right_tree.left = curr.right
curr.left = dummy.right
curr.right = dummy.left
self.root = curr
# Function to insert a new element in the splay tree
def insert(self, key):
if self.root is None:
self.root = Node(key)
else:
self.splay(key)
if key < self.root.key:
new_node = Node(key)
new_node.left = self.root.left
new_node.right = self.root
self.root.left = None
self.root = new_node
elif key > self.root.key:
new_node = Node(key)
new_node.right = self.root.right
new_node.left = self.root
self.root.right = None
self.root = new_node
# Function to print the elements of the splay tree (in-order traversal)
def inorder(self, node):
if node is None:
return
self.inorder(node.left)
print(node.key, end=" ")
self.inorder(node.right)
# Test the program
splay_tree = SplayTree()
splay_tree.insert(5)
splay_tree.insert(10)
splay_tree.insert(1)
splay_tree.insert(7)
print("Elements of splay tree after insertion: ")
splay_tree.inorder(splay_tree.root)