Skip to content

Commit 4a04eff

Browse files
Merge branch 'master' into patch-1
2 parents bb8cb72 + 54567e2 commit 4a04eff

File tree

5 files changed

+232
-72
lines changed

5 files changed

+232
-72
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@
912912
* [StackArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java)
913913
* [StackOfLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java)
914914
* trees
915+
* [AVLTreeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/AVLTreeTest.java)
915916
* [BinaryTreeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java)
916917
* [BoundaryTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BoundaryTraversalTest.java)
917918
* [BSTFromSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BSTFromSortedArrayTest.java)

src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Swap every pair of adjacent bits of a given number.
5-
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
4+
* A utility class to swap every pair of adjacent bits in a given integer.
5+
* This operation shifts the even-positioned bits to odd positions and vice versa.
6+
*
7+
* Example:
8+
* - Input: 2 (binary: `10`) → Output: 1 (binary: `01`)
9+
* - Input: 43 (binary: `101011`) → Output: 23 (binary: `010111`)
10+
*
11+
* **Explanation of the Algorithm:**
12+
* 1. Mask even-positioned bits: Using `0xAAAAAAAA` (binary: `101010...`),
13+
* which selects bits in even positions.
14+
* 2. Mask odd-positioned bits: Using `0x55555555` (binary: `010101...`),
15+
* which selects bits in odd positions.
16+
* 3. Shift bits:
17+
* - Right-shift even-positioned bits by 1 to move them to odd positions.
18+
* - Left-shift odd-positioned bits by 1 to move them to even positions.
19+
* 4. Combine both shifted results using bitwise OR (`|`) to produce the final result.
20+
*
21+
* Use Case: This algorithm can be useful in applications involving low-level bit manipulation,
22+
* such as encoding, data compression, or cryptographic transformations.
23+
*
24+
* Time Complexity: O(1) (constant time, since operations are bitwise).
25+
*
26+
* Author: Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
627
*/
7-
828
public final class SwapAdjacentBits {
929
private SwapAdjacentBits() {
1030
}
1131

32+
/**
33+
* Swaps every pair of adjacent bits of a given integer.
34+
* Steps:
35+
* 1. Mask the even-positioned bits.
36+
* 2. Mask the odd-positioned bits.
37+
* 3. Shift the even bits to the right and the odd bits to the left.
38+
* 4. Combine the shifted bits.
39+
*
40+
* @param num the integer whose bits are to be swapped
41+
* @return the integer after swapping every pair of adjacent bits
42+
*/
1243
public static int swapAdjacentBits(int num) {
1344
// mask the even bits (0xAAAAAAAA => 10101010...)
1445
int evenBits = num & 0xAAAAAAAA;

src/main/java/com/thealgorithms/datastructures/trees/AVLTree.java

Lines changed: 82 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package com.thealgorithms.datastructures.trees;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Represents an AVL Tree, a self-balancing binary search tree.
8+
* In an AVL tree, the heights of the two child subtrees of any node
9+
* differ by at most one. If they differ by more than one at any time,
10+
* rebalancing is performed to restore this property.
11+
*/
312
public class AVLTree {
413

514
private Node root;
615

7-
private class Node {
8-
16+
private static class Node {
917
private int key;
1018
private int balance;
1119
private int height;
@@ -17,8 +25,18 @@ private class Node {
1725
key = k;
1826
parent = p;
1927
}
28+
29+
public Integer getBalance() {
30+
return balance;
31+
}
2032
}
2133

34+
/**
35+
* Inserts a new key into the AVL tree.
36+
*
37+
* @param key the key to be inserted
38+
* @return {@code true} if the key was inserted, {@code false} if the key already exists
39+
*/
2240
public boolean insert(int key) {
2341
if (root == null) {
2442
root = new Node(key, null);
@@ -31,7 +49,6 @@ public boolean insert(int key) {
3149
}
3250

3351
parent = n;
34-
3552
boolean goLeft = n.key > key;
3653
n = goLeft ? n.left : n.right;
3754

@@ -49,8 +66,32 @@ public boolean insert(int key) {
4966
return true;
5067
}
5168

69+
/**
70+
* Deletes a key from the AVL tree.
71+
*
72+
* @param delKey the key to be deleted
73+
*/
74+
public void delete(int delKey) {
75+
if (root == null) {
76+
return;
77+
}
78+
79+
// Find the node to be deleted
80+
Node node = root;
81+
Node child = root;
82+
while (child != null) {
83+
node = child;
84+
child = delKey >= node.key ? node.right : node.left;
85+
if (delKey == node.key) {
86+
delete(node);
87+
return;
88+
}
89+
}
90+
}
91+
5292
private void delete(Node node) {
5393
if (node.left == null && node.right == null) {
94+
// Leaf node
5495
if (node.parent == null) {
5596
root = null;
5697
} else {
@@ -64,6 +105,8 @@ private void delete(Node node) {
64105
}
65106
return;
66107
}
108+
109+
// Node has one or two children
67110
Node child;
68111
if (node.left != null) {
69112
child = node.left;
@@ -80,26 +123,49 @@ private void delete(Node node) {
80123
delete(child);
81124
}
82125

83-
public void delete(int delKey) {
84-
if (root == null) {
85-
return;
126+
/**
127+
* Returns a list of balance factors for each node in the tree.
128+
*
129+
* @return a list of integers representing the balance factors of the nodes
130+
*/
131+
public List<Integer> returnBalance() {
132+
List<Integer> balances = new ArrayList<>();
133+
returnBalance(root, balances);
134+
return balances;
135+
}
136+
137+
private void returnBalance(Node n, List<Integer> balances) {
138+
if (n != null) {
139+
returnBalance(n.left, balances);
140+
balances.add(n.getBalance());
141+
returnBalance(n.right, balances);
86142
}
87-
Node node = root;
88-
Node child = root;
143+
}
89144

90-
while (child != null) {
91-
node = child;
92-
child = delKey >= node.key ? node.right : node.left;
93-
if (delKey == node.key) {
94-
delete(node);
95-
return;
96-
}
145+
/**
146+
* Searches for a key in the AVL tree.
147+
*
148+
* @param key the key to be searched
149+
* @return true if the key is found, false otherwise
150+
*/
151+
public boolean search(int key) {
152+
Node result = searchHelper(this.root, key);
153+
return result != null;
154+
}
155+
156+
private Node searchHelper(Node root, int key) {
157+
if (root == null || root.key == key) {
158+
return root;
97159
}
160+
161+
if (root.key > key) {
162+
return searchHelper(root.left, key);
163+
}
164+
return searchHelper(root.right, key);
98165
}
99166

100167
private void rebalance(Node n) {
101168
setBalance(n);
102-
103169
if (n.balance == -2) {
104170
if (height(n.left.left) >= height(n.left.right)) {
105171
n = rotateRight(n);
@@ -143,7 +209,6 @@ private Node rotateLeft(Node a) {
143209
}
144210

145211
setBalance(a, b);
146-
147212
return b;
148213
}
149214

@@ -169,7 +234,6 @@ private Node rotateRight(Node a) {
169234
}
170235

171236
setBalance(a, b);
172-
173237
return b;
174238
}
175239

@@ -197,53 +261,9 @@ private void setBalance(Node... nodes) {
197261
}
198262
}
199263

200-
public void printBalance() {
201-
printBalance(root);
202-
}
203-
204-
private void printBalance(Node n) {
205-
if (n != null) {
206-
printBalance(n.left);
207-
System.out.printf("%s ", n.balance);
208-
printBalance(n.right);
209-
}
210-
}
211-
212264
private void reheight(Node node) {
213265
if (node != null) {
214266
node.height = 1 + Math.max(height(node.left), height(node.right));
215267
}
216268
}
217-
218-
public boolean search(int key) {
219-
Node result = searchHelper(this.root, key);
220-
return result != null;
221-
}
222-
223-
private Node searchHelper(Node root, int key) {
224-
// root is null or key is present at root
225-
if (root == null || root.key == key) {
226-
return root;
227-
}
228-
229-
// key is greater than root's key
230-
if (root.key > key) {
231-
return searchHelper(root.left, key); // call the function on the node's left child
232-
}
233-
// key is less than root's key then
234-
// call the function on the node's right child as it is greater
235-
return searchHelper(root.right, key);
236-
}
237-
238-
public static void main(String[] args) {
239-
AVLTree tree = new AVLTree();
240-
241-
System.out.println("Inserting values 1 to 10");
242-
for (int i = 1; i < 10; i++) {
243-
tree.insert(i);
244-
}
245-
246-
System.out.print("Printing balance: ");
247-
tree.printBalance();
248-
}
249269
}

src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
class SwapAdjacentBitsTest {
99

1010
@ParameterizedTest
11-
@CsvSource({
12-
"2, 1", // 2 (10 in binary) should become 1 (01 in binary)
13-
"43, 23", // 43 should become 23
14-
"153, 102", // 153 should become 102
15-
"15, 15", // 15 (1111) remains 15 (1111)
16-
"0, 0" // 0 (0000) remains 0 (0000)
17-
})
11+
@CsvSource({"2, 1", // 2 (binary: 10) -> 1 (binary: 01)
12+
"43, 23", // 43 (binary: 101011) -> 23 (binary: 010111)
13+
"153, 102", // 153 (binary: 10011001) -> 102 (binary: 01100110)
14+
"15, 15", // 15 (binary: 1111) -> 15 (binary: 1111) (no change)
15+
"0, 0", // 0 (binary: 0000) -> 0 (binary: 0000) (no change)
16+
"1, 2", // 1 (binary: 01) -> 2 (binary: 10)
17+
"170, 85", // 170 (binary: 10101010) -> 85 (binary: 01010101)
18+
"85, 170", // 85 (binary: 01010101) -> 170 (binary: 10101010)
19+
"255, 255", // 255 (binary: 11111111) -> 255 (binary: 11111111) (no change)
20+
"128, 64", // 128 (binary: 10000000) -> 64 (binary: 01000000)
21+
"1024, 2048",
22+
"-1, -1", // -1 (all bits 1) remains -1 (no change due to two's complement)
23+
"-2, -3", // -2 (binary: ...1110) -> -3 (binary: ...1101)
24+
"2147483647, -1073741825", "-2147483648, -1073741824"})
1825
void
1926
testSwapAdjacentBits(int input, int expected) {
2027
assertEquals(expected, SwapAdjacentBits.swapAdjacentBits(input));

0 commit comments

Comments
 (0)