Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
RedBlackTree.java 1.93 KiB
package search;

public class RedBlackTree<Key, Value> {
	
	public static void main(String[] args) {
		GeneralCompare<Integer> b1;
		b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
		Integer[] x = {1,2,3,4,5,6,7,8};
		for(int i = 0; i <= x.length; i++){
			put(x[i], x, b1);
		}
	}
	
	private static Node root;
	
	private static <T> int size(Node h){
		return h.n();
	}
	
	public static <T> void put(Comparable<T> key, Comparable<T>[] val, GeneralCompare gc){
		root = put(root, key, val, gc);
		root.color(false);
		System.out.println(root.key());
		System.out.println(root.val());
	}
	
	private static boolean isRed(Node x){
		return x.color();
	}
	
	public static Node rotateLeft(Node h){
		Node x = h.right();
		h.right(x.left());
		x.left(h);
		x.color(h.color());
		h.color(true);
		x.n(h.n());
		h.n(1 + size(h.left()) + size(h.right()));
		return x;
	}
	
	public static Node rotateRight(Node h){
		Node x = h.left();
		h.left(x.right());
		x.right(h);
		x.color(h.color());
		h.color(true);
		x.n(h.n());
		h.n(1 + size(h.left()) + size(h.right()));
		return x;
		
	}
	
	private static void flipColors(Node h){
		if(h.left() != null && h.right() != null){
			h.left().color(false);
			h.right().color(false);
			h.color(true);
		}
	}
	
	private static <T> Node put(Node h, Comparable<T> key, Comparable<T>[] val, GeneralCompare<T> gc){
		
		if(h == null)
			return new Node(key, val, 1, true);
		
		int n = size(h) + 1;
		
		Node root = new Node(key, val, n, true);