Created
May 19, 2017 10:55
-
-
Save fermopili/6ee941c1a1df01cf0e92e8e5c7dca69e to your computer and use it in GitHub Desktop.
com.javarush.task.task36.task3604 Разбираемся в красно-черном дереве
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task36.task3604; | |
public class RedBlackTree | |
{ | |
private static final Node EMPTY = new Node ( 0 ); | |
static | |
{ | |
EMPTY.left = EMPTY; | |
EMPTY.right = EMPTY; | |
} | |
protected Node current; | |
private Node parent; | |
private Node grand; | |
private Node great; | |
private Node header; | |
public RedBlackTree() | |
{ | |
header = new Node ( Integer.MIN_VALUE ); | |
header.left = EMPTY; | |
header.right = EMPTY; | |
} | |
public boolean isEmpty() | |
{ | |
// return header.left == EMPTY; | |
return header.right == EMPTY; | |
} | |
public void clear() | |
{ | |
header.right = EMPTY; | |
} | |
public void insert(int item) | |
{ | |
current = grand = parent = header; | |
EMPTY.element = item; | |
while (current.element != item) | |
{ | |
great = grand; | |
grand = parent; | |
parent = current; | |
current = item > current.element ? current.right : current.left; | |
if (current.left.color == Color.RED && current.right.color == Color.RED) | |
{ | |
reorient ( item ); | |
} | |
} | |
if (current != EMPTY) | |
{ | |
return; | |
} | |
current = new Node ( item, EMPTY, EMPTY ); | |
if (item <= parent.element) | |
{ | |
parent.left = current; | |
} | |
else | |
{ | |
parent.right = current; | |
} | |
reorient ( item ); | |
} | |
protected void reorient(int item) | |
{ | |
current.color = Color.RED; | |
current.left.color = Color.BLACK; | |
current.right.color = Color.BLACK; | |
if (parent.color == Color.RED) | |
{ | |
grand.color = Color.RED; | |
if (item < grand.element != item < parent.element) | |
{ | |
parent = rotate ( item, grand ); | |
} | |
current = rotate ( item, great ); | |
current.color = Color.BLACK; | |
} | |
header.right.color = Color.BLACK; | |
} | |
private Node rotate(int item, Node parent) | |
{ | |
if (item < parent.element) | |
{ | |
Node node = parent.left; | |
Node resultNode = item < node.element ? rotateWithLeftNode ( node ) : rotateWithRightNode ( node ); | |
parent.left = resultNode; | |
return parent.left; | |
} | |
else | |
{ | |
Node node = parent.right; | |
Node resultNode = item < node.element ? rotateWithLeftNode ( node ) : rotateWithRightNode ( node ); | |
parent.right = resultNode; | |
return parent.right; | |
} | |
} | |
private Node rotateWithLeftNode(Node element) | |
{ | |
Node tmp = element.left; | |
element.left = tmp.right; | |
tmp.right = element; | |
return tmp; | |
} | |
private Node rotateWithRightNode(Node element) | |
{ | |
Node tmp = element.right; // Node tmp = element.left; | |
element.right = tmp.left; // element.left = tmp.right; | |
tmp.left = element; // tmp.right = element; | |
return tmp; | |
} | |
public static enum Color | |
{ | |
BLACK, | |
RED | |
} | |
public static class Node | |
{ | |
private int element; | |
private Node left; | |
private Node right; | |
private Color color; | |
public Node(int element) | |
{ | |
this ( element, null, null ); | |
} | |
public Node(int element, Node left, Node right) | |
{ | |
this.left = left; | |
this.right = right; | |
this.element = element; | |
this.color = Color.BLACK; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task36.task3604; | |
/* | |
Разбираемся в красно-черном дереве | |
*/ | |
public class Solution { | |
public static void main(String[] args) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taskKey="com.javarush.task.task36.task3604" | |
Разбираемся в красно-черном дереве | |
Дана реализация красно-черного дерева. | |
Некоторые методы сломаны. Разберись в коде и исправь ошибки. | |
Метод main не участвует в тестировании. | |
Все модификаторы правильные. | |
Имена переменных и методов не изменяйте. | |
Требования: | |
1. Исправь ошибку в методе isEmpty в классе RedBlackTree. | |
2. Исправь ошибку в методе rotateWithRightNode в классе RedBlackTree. | |
3. Исправь ошибку в методе insert в классе RedBlackTree. | |
4. Класс RedBlackTree должен реализовывать красно-черное дерево. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment