Skip to content

Commit a9797cd

Browse files
authored
Merge pull request animator#1070 from kosuri-indu/add/splay-trees
Added Splay Trees in DSA section
2 parents 69ed7ff + c2c84a0 commit a9797cd

File tree

2 files changed

+164
-1
lines changed

2 files changed

+164
-1
lines changed

contrib/ds-algorithms/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
- [Trie](trie.md)
1616
- [Two Pointer Technique](two-pointer-technique.md)
1717
- [Hashing through Linear Probing](hashing-linear-probing.md)
18-
- [Hashing through Chaining](hashing-chaining.md)
18+
- [Hashing through Chaining](hashing-chaining.md)
19+
- [Splay Trees](splay-trees.md)
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Splay Tree
2+
3+
In Data Structures and Algorithms, a **Splay Tree** is a self-adjusting binary search tree with the additional property that recently accessed elements are quick to access again. It performs basic operations such as insertion, search, and deletion in O(log n) amortized time. This is achieved by a process called **splaying**, where the accessed node is moved to the root through a series of tree rotations.
4+
5+
## Points to be Remembered
6+
7+
- **Splaying**: Moving the accessed node to the root using rotations.
8+
- **Rotations**: Tree rotations (left and right) are used to balance the tree during splaying.
9+
- **Self-adjusting**: The tree adjusts itself with each access, keeping frequently accessed nodes near the root.
10+
11+
## Real Life Examples of Splay Trees
12+
13+
- **Cache Implementation**: Frequently accessed data is kept near the top of the tree, making repeated accesses faster.
14+
- **Networking**: Routing tables in network switches can use splay trees to prioritize frequently accessed routes.
15+
16+
## Applications of Splay Trees
17+
18+
Splay trees are used in various applications in Computer Science:
19+
20+
- **Cache Implementations**
21+
- **Garbage Collection Algorithms**
22+
- **Data Compression Algorithms (e.g., LZ78)**
23+
24+
Understanding these applications is essential for Software Development.
25+
26+
## Operations in Splay Tree
27+
28+
Key operations include:
29+
30+
- **INSERT**: Insert a new element into the splay tree.
31+
- **SEARCH**: Find the position of an element in the splay tree.
32+
- **DELETE**: Remove an element from the splay tree.
33+
34+
## Implementing Splay Tree in Python
35+
36+
```python
37+
class SplayTreeNode:
38+
def __init__(self, key):
39+
self.key = key
40+
self.left = None
41+
self.right = None
42+
43+
class SplayTree:
44+
def __init__(self):
45+
self.root = None
46+
47+
def insert(self, key):
48+
self.root = self.splay_insert(self.root, key)
49+
50+
def search(self, key):
51+
self.root = self.splay_search(self.root, key)
52+
return self.root
53+
54+
def splay(self, root, key):
55+
if not root or root.key == key:
56+
return root
57+
58+
if root.key > key:
59+
if not root.left:
60+
return root
61+
if root.left.key > key:
62+
root.left.left = self.splay(root.left.left, key)
63+
root = self.rotateRight(root)
64+
elif root.left.key < key:
65+
root.left.right = self.splay(root.left.right, key)
66+
if root.left.right:
67+
root.left = self.rotateLeft(root.left)
68+
return root if not root.left else self.rotateRight(root)
69+
70+
else:
71+
if not root.right:
72+
return root
73+
if root.right.key > key:
74+
root.right.left = self.splay(root.right.left, key)
75+
if root.right.left:
76+
root.right = self.rotateRight(root.right)
77+
elif root.right.key < key:
78+
root.right.right = self.splay(root.right.right, key)
79+
root = self.rotateLeft(root)
80+
return root if not root.right else self.rotateLeft(root)
81+
82+
def splay_insert(self, root, key):
83+
if not root:
84+
return SplayTreeNode(key)
85+
86+
root = self.splay(root, key)
87+
88+
if root.key == key:
89+
return root
90+
91+
new_node = SplayTreeNode(key)
92+
93+
if root.key > key:
94+
new_node.right = root
95+
new_node.left = root.left
96+
root.left = None
97+
else:
98+
new_node.left = root
99+
new_node.right = root.right
100+
root.right = None
101+
102+
return new_node
103+
104+
def splay_search(self, root, key):
105+
return self.splay(root, key)
106+
107+
def rotateRight(self, node):
108+
temp = node.left
109+
node.left = temp.right
110+
temp.right = node
111+
return temp
112+
113+
def rotateLeft(self, node):
114+
temp = node.right
115+
node.right = temp.left
116+
temp.left = node
117+
return temp
118+
119+
def preOrder(self, root):
120+
if root:
121+
print(root.key, end=' ')
122+
self.preOrder(root.left)
123+
self.preOrder(root.right)
124+
125+
#Example usage:
126+
splay_tree = SplayTree()
127+
splay_tree.insert(50)
128+
splay_tree.insert(30)
129+
splay_tree.insert(20)
130+
splay_tree.insert(40)
131+
splay_tree.insert(70)
132+
splay_tree.insert(60)
133+
splay_tree.insert(80)
134+
135+
print("Preorder traversal of the Splay tree is:")
136+
splay_tree.preOrder(splay_tree.root)
137+
138+
splay_tree.search(60)
139+
140+
print("\nSplay tree after search operation for key 60:")
141+
splay_tree.preOrder(splay_tree.root)
142+
```
143+
144+
## Output
145+
146+
```markdown
147+
Preorder traversal of the Splay tree is:
148+
50 30 20 40 70 60 80
149+
150+
Splay tree after search operation for key 60:
151+
60 50 30 20 40 70 80
152+
```
153+
154+
## Complexity Analysis
155+
156+
The worst-case time complexities of the main operations in a Splay Tree are as follows:
157+
158+
- **Insertion**: (O(n)). In the worst case, insertion may take linear time if the tree is highly unbalanced.
159+
- **Search**: (O(n)). In the worst case, searching for a node may take linear time if the tree is highly unbalanced.
160+
- **Deletion**: (O(n)). In the worst case, deleting a node may take linear time if the tree is highly unbalanced.
161+
162+
While these operations can take linear time in the worst case, the splay operation ensures that the tree remains balanced over a sequence of operations, leading to better average-case performance.

0 commit comments

Comments
 (0)