Skip to content

Commit b96ca41

Browse files
committed
[GP-165] Implemented tests for RB-tree
1 parent 84eb88a commit b96ca41

File tree

6 files changed

+711
-0
lines changed

6 files changed

+711
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Reb-Black Tree
2+
Learn a Red-Black tree data structure and build strong skills creating balanced tree implementation 💪
3+
4+
### Objectives (at the end of the training you will be able to...)
5+
* understand the **main idea** behind the **Red-Black Tree** data structure ✅
6+
* get knowledge in one of the tree balancing strategies
7+
* implement rotations and recoloring algorithms
8+
* catch what is different from simple BinarySearchTree and why it's better
9+
* get stronger knowledge in algorithms field
10+
11+
* The more about Red Black Tree can be found on [Wiki page](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree)
12+
---
13+
#### 🆕 First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-exercises/tree/main/0-0-intro#introduction)
14+
#### ➡️ Have any feedback? – [Please fill the form ](https://forms.gle/7U9XZHuTtT5xpjXR6)
15+
16+
##
17+
<div align="center"><img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/animation/GitHub%20Star_3.gif" height=50/></div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>2-0-data-structures-and-algorithms</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>2-2-7-red-black-tree</artifactId>
13+
14+
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.bobocode.cs;
2+
3+
import java.util.function.Consumer;
4+
5+
public interface BinarySearchTree<T extends Comparable<T>> {
6+
/**
7+
* insert an element
8+
* @return true if element did not exist in the tree and was inserted successfully
9+
*/
10+
boolean insert(T element);
11+
12+
/**
13+
* @return true if tree contains element
14+
*/
15+
boolean contains(T element);
16+
17+
/**
18+
* @return number of elements in the tree
19+
*/
20+
int size();
21+
22+
/**
23+
* @return max. number of transition between root node and any other node; 0 - if tree is empty or contains 1 element
24+
*/
25+
int depth();
26+
27+
/**
28+
* traverse the tree in element's natural order
29+
* @param consumer accepts ref. to node during traversing
30+
*/
31+
void inOrderTraversal(Consumer<T> consumer);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.bobocode.cs;
2+
3+
import java.util.function.Consumer;
4+
5+
import com.bobocode.util.ExerciseNotCompletedException;
6+
7+
/**
8+
* {@link RedBlackBinarySearchTree} is an implementation of a {@link BinarySearchTree} that is based on nodes,
9+
* coloring, rotations and recursion. A tree node is represented as a nested class {@link Node}. It holds an element (a value),
10+
* references to the parent node, left/right child nodes and also a field type of {@link Color} which is Enum with two possible values RED and BLACK.
11+
* The BinarySearch tree considered as a RedBlackTree when it meets next properties:
12+
* 1. Every {@link Node} is either RED or BLACK {@link Color}.
13+
* 2. RED node can't have RED children nodes
14+
* 3. The root is always BLACK.
15+
* 4. For each node, all simple paths from the node to descendant leaves(null nodes) contain the same number of BLACK nodes
16+
* 5. Every leaf(null node) is null and should be treated as a BLACK node
17+
*
18+
* To fulfill these requirements the tree need additional steps after insertion of new node to re-balance itself using
19+
* recoloring of nodes and rotations. Every newly inserted node should be RED before re-balancing.
20+
*
21+
* Red-Black Tree example:
22+
* 7(B)
23+
* / \
24+
* / \
25+
* 3(B) 10(R)
26+
* / \ / \
27+
* / \ / \
28+
* 2(R) null 9(B) 12(B)
29+
* / \ / \ / \
30+
* / \ / \ / \
31+
* null null null null null 33(R)
32+
*
33+
*
34+
* <p><p>
35+
* <strong>TODO: to get the most out of your learning, <a href="https://www.bobocode.com/learn">visit our website</a></strong>
36+
* <p>
37+
*
38+
* @param <T> a type of elements that are stored in the tree
39+
* @author Serhii Bondarenko
40+
* @author Taras Boychuk
41+
*/
42+
43+
public class RedBlackBinarySearchTree<T extends Comparable<T>> implements BinarySearchTree<T> {
44+
45+
public static <T extends Comparable<T>> RedBlackBinarySearchTree<T> of(T... elements) {
46+
throw new ExerciseNotCompletedException();
47+
}
48+
49+
@Override
50+
public boolean insert(T element) {
51+
throw new ExerciseNotCompletedException();
52+
}
53+
54+
@Override
55+
public boolean contains(T element) {
56+
throw new ExerciseNotCompletedException();
57+
}
58+
59+
@Override
60+
public int size() {
61+
throw new ExerciseNotCompletedException();
62+
}
63+
64+
@Override
65+
public int depth() {
66+
throw new ExerciseNotCompletedException();
67+
}
68+
69+
@Override
70+
public void inOrderTraversal(Consumer<T> consumer) {
71+
throw new ExerciseNotCompletedException();
72+
}
73+
74+
}

0 commit comments

Comments
 (0)