Skip to content

[GP-165] Implemented tests for RB-tree #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions 2-0-data-structures-and-algorithms/2-2-7-red-black-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Reb-Black Tree
Learn a Red-Black tree data structure and build strong skills creating balanced tree implementation 💪

### Objectives (at the end of the training you will be able to...)
* understand the **main idea** behind the **Red-Black Tree** data structure ✅
* get knowledge in one of the tree balancing strategies
* implement rotations and recoloring algorithms
* catch what is different from simple BinarySearchTree and why it's better
* get stronger knowledge in algorithms field

* The more about Red Black Tree can be found on [Wiki page](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree)
---
#### 🆕 First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-exercises/tree/main/0-0-intro#introduction)
#### ➡️ Have any feedback? – [Please fill the form ](https://forms.gle/7U9XZHuTtT5xpjXR6)

##
<div align="center"><img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/animation/GitHub%20Star_3.gif" height=50/></div>
15 changes: 15 additions & 0 deletions 2-0-data-structures-and-algorithms/2-2-7-red-black-tree/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>2-0-data-structures-and-algorithms</artifactId>
<groupId>com.bobocode</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>2-2-7-red-black-tree</artifactId>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.bobocode.cs;

import java.util.function.Consumer;

public interface BinarySearchTree<T extends Comparable<T>> {
/**
* insert an element
* @return true if element did not exist in the tree and was inserted successfully
*/
boolean insert(T element);

/**
* @return true if tree contains element
*/
boolean contains(T element);

/**
* @return number of elements in the tree
*/
int size();

/**
* @return max. number of transition between root node and any other node; 0 - if tree is empty or contains 1 element
*/
int depth();

/**
* traverse the tree in element's natural order
* @param consumer accepts ref. to node during traversing
*/
void inOrderTraversal(Consumer<T> consumer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.bobocode.cs;

import java.util.function.Consumer;

import com.bobocode.util.ExerciseNotCompletedException;

/**
* {@link RedBlackBinarySearchTree} is an implementation of a {@link BinarySearchTree} that is based on nodes,
* coloring, rotations and recursion. A tree node is represented as a nested class {@link Node}. It holds an element (a value),
* 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<br>
* The BinarySearch tree considered as a RedBlackTree when it meets next properties:<br>
* 1. Every {@link Node} is either RED or BLACK {@link Color}<br>
* 2. RED node can't have RED children nodes<br>
* 3. The root is always BLACK<br>
* 4. For each node, all simple paths from the node to descendant leaves(null nodes) contain the same number of BLACK nodes<br>
* 5. Every leaf(null node) is null and should be treated as a BLACK node<br>
*
* To fulfill these requirements the tree need additional steps after insertion of new node to re-balance itself using
* recoloring of nodes and rotations. Every newly inserted node should be RED before re-balancing.
*
* Red-Black Tree example:<br>
*.........................7(B).................................<br>
*..................../..........\..............................<br>
*.................../............\.............................<br>
*.................3(B)............10(R)........................<br>
*................/...\........../......\.......................<br>
*.............../.....\......../........\......................<br>
*.............2(R)....null...9(B)........12(B).................<br>
*............/....\........./....\....../....\.................<br>
*.........../......\......./......\..../......\................<br>
*.........null....null...null...null..null....33(R)............<br>
*............................................/....\............<br>
* ........................................../......\...........<br>
* ........................................null.....null........<br>
*
* <p><p>
* <strong>TODO: to get the most out of your learning, <a href="https://www.bobocode.com/learn">visit our website</a></strong>
* <p>
*
* @param <T> a type of elements that are stored in the tree
* @author Serhii Bondarenko
* @author Taras Boychuk
*/

public class RedBlackBinarySearchTree<T extends Comparable<T>> implements BinarySearchTree<T> {

public static <T extends Comparable<T>> RedBlackBinarySearchTree<T> of(T... elements) {
throw new ExerciseNotCompletedException();
}

@Override
public boolean insert(T element) {
throw new ExerciseNotCompletedException();
}

@Override
public boolean contains(T element) {
throw new ExerciseNotCompletedException();
}

@Override
public int size() {
throw new ExerciseNotCompletedException();
}

@Override
public int depth() {
throw new ExerciseNotCompletedException();
}

@Override
public void inOrderTraversal(Consumer<T> consumer) {
throw new ExerciseNotCompletedException();
}

}
Loading