Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package classes;
- import java.sql.SQLOutput;
- public class Map {
- class Node {
- private Node parent;
- private Node leftChild;
- private Node rightChild;
- private int value;
- private int key;
- private boolean color; //red = true, black = false
- Node(int newKey, int newValue) {
- value = newValue;
- key = newKey;
- parent = null;
- leftChild = null;
- rightChild = null;
- color = true;
- }
- Node() {
- parent = null;
- leftChild = null;
- rightChild = null;
- color = true;
- }
- public String toString() {
- String colorName = color ? "RED" : "BLACK";
- String msg = "key: " + key + ", value: " + value + ", color: " + colorName;
- if (this.parent != null)
- msg += ", parent's key: " + parent.key;
- return msg;
- }
- }
- private Node root = null;
- public void setValue(int key, int value) {
- Node newNode = new Node(key, value);
- if (root == null) {
- System.out.println("Zrobilo roota " + newNode.value);
- root = newNode;
- root.color = false;
- return;
- }
- Node actualNode = root;
- boolean running = true;
- while (running) {
- if (actualNode.key < key && actualNode.rightChild != null) { //prawa droga
- actualNode = actualNode.rightChild;
- } else if (actualNode.key < key) {
- newNode.parent = actualNode;
- actualNode.rightChild = newNode;
- actualNode = actualNode.rightChild;
- running = false;
- }
- if (actualNode.key > key && actualNode.leftChild != null) { //lewa droga
- actualNode = actualNode.leftChild;
- } else if (actualNode.key > key) {
- newNode.parent = actualNode;
- actualNode.leftChild = newNode;
- actualNode = actualNode.leftChild;
- running = false;
- }
- }
- restore(actualNode);
- }
- private void restore(Node actualNode) {
- System.out.println("Weszlo do restora " + actualNode.value);
- while (actualNode.parent != root && actualNode.parent != null) {
- System.out.println("Weszlo do while " + actualNode.value);
- switch (isLeftChild(actualNode.parent)) {
- case 1:
- System.out.println("Case ojciec lewe dziecko " + actualNode.value);
- if (brotherColor(actualNode.parent)) {
- System.out.println("Brat czerwony " + actualNode.value);
- actualNode.parent.color = false;
- actualNode.parent.parent.rightChild.color = false;
- actualNode = actualNode.parent.parent;
- actualNode.color = true;
- break;
- }
- if (isLeftChild(actualNode) == 0) {
- System.out.println("Prawe dziecko, brat czarny" + actualNode.value);
- actualNode = actualNode.parent;
- leftRotate(actualNode);
- }
- System.out.println("Lewe dziecko brat czarny " + actualNode.value);
- actualNode.parent.color = false;
- actualNode.parent.parent.color = true;
- rightRotate(actualNode.parent.parent);
- break;
- case 0:
- System.out.println("Case ojciec prawe dziecko"+ actualNode.value);
- if (brotherColor(actualNode.parent)) {
- System.out.println("Brat czerwony "+ actualNode.value);
- actualNode.parent.color = false;
- actualNode.parent.parent.leftChild.color = false;
- actualNode = actualNode.parent.parent;
- actualNode.color = true;
- break;
- }
- if (isLeftChild(actualNode) == 0) {
- System.out.println("Prawe dziecko, brat czarny" + actualNode.value);
- actualNode = actualNode.parent;
- rightRotate(actualNode);
- }
- System.out.println("Lewe dziecko brat czarny " + actualNode.value);
- actualNode.parent.color = false;
- actualNode.parent.parent.color = true;
- leftRotate(actualNode.parent.parent);
- break;
- }
- }
- root.color = false;
- }
- private int isLeftChild(Node node) {
- if (node.parent.leftChild != null) {
- if (node.parent.leftChild.key == node.key)
- return 1; //jest lewym synem - 1, jest prawym 0
- else return 0;
- } else return 0;
- }
- private boolean brotherColor(Node node) {
- if (node.parent.leftChild == node) {
- if (node.parent.rightChild == null) return false;
- else return node.parent.rightChild.color;
- } else if (node.parent.leftChild == null) return false;
- else return node.parent.leftChild.color;
- }
- private void rightRotate(Node node) {
- Node inputNode = node;
- Node P = node.leftChild;
- if (P.rightChild != null) {
- node.leftChild = P.rightChild;
- node.leftChild.parent = node;
- }else node.leftChild = null;
- P.parent = node.parent;
- node.parent = P;
- P.rightChild = node;
- if (inputNode == root) {
- root = P;
- } else {
- if (isLeftChild(inputNode) == 1)
- inputNode.parent.leftChild = P;
- else inputNode.parent.rightChild = P;
- }
- }
- private void leftRotate(Node node) {
- Node inputNode = node;
- Node Q = node.rightChild;
- if (Q.leftChild != null) {
- node.rightChild = Q.leftChild;
- node.rightChild.parent = node;
- } else node.rightChild = null;
- Q.parent = node.parent;
- node.parent = Q;
- Q.leftChild = node;
- if (inputNode == root) {
- root = Q;
- } else {
- if (isLeftChild(node) == 1)
- inputNode.parent.leftChild = Q;
- else
- inputNode.parent.rightChild = Q;
- }
- }
- public void write() {
- writeRec(root);
- }
- private void writeRec(Node cur) {
- if(cur != null) {
- writeRec(cur.leftChild);
- System.out.println(cur);
- writeRec(cur.rightChild);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement