二叉树
package zhang.tree.rbtree;
import zhang.tree.rbtree.printer.BinaryTreeInfo;
import java.util.LinkedList;
import java.util.Queue;
public class BinaryTree<E> implements BinaryTreeInfo {
protected int size;
protected Node<E> root;
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void clear() {
root = null;
size = 0;
}
public void preorderTraversal() {
preorderTraversal(root);
}
private void preorderTraversal(Node<E> node) {
if (node == null) return;
System.out.print(node.element + " ");
preorderTraversal(node.left);
preorderTraversal(node.right);
}
public void inorderTraversal() {
inorderTraversal(root);
}
private void inorderTraversal(Node<E> node) {
if (node == null) return;
inorderTraversal(node.left);
System.out.print(node.element + " ");
inorderTraversal(node.right);
}
public void postorderTraversal() {
postorderTraversal(root);
}
private void postorderTraversal(Node<E> node) {
if (node == null) return;
postorderTraversal(node.left);
postorderTraversal(node.right);
System.out.print(node.element + " ");
}
public static abstract class Visitor<E> {
boolean stop;
public abstract boolean visit(E element);
}
public void preorderTraversal(Visitor<E> visitor) {
if (visitor == null) return;
preorderTraversal(root, visitor);
}
private void preorderTraversal(Node<E> node, Visitor<E> visitor) {
if (node == null || visitor.stop) return;
visitor.stop = visitor.visit(node.element);
preorderTraversal(node.left, visitor);
preorderTraversal(node.right, visitor);
}
public void inorderTraversal(Visitor<E> visitor) {
if (visitor == null) return;
inorderTraversal(root, visitor);
}
private void inorderTraversal(Node<E> node, Visitor<E> visitor) {
if (node == null || visitor.stop) return;
inorderTraversal(node.left, visitor);
if (visitor.stop) {
return;
}
visitor.stop = visitor.visit(node.element);
inorderTraversal(node.right, visitor);
}
public void postorderTraversal(Visitor<E> visitor) {
if (visitor == null) return;
postorderTraversal(root, visitor);
}
private void postorderTraversal(Node<E> node, Visitor<E> visitor) {
if (node == null || visitor.stop) return;
postorderTraversal(node.left, visitor);
postorderTraversal(node.right, visitor);
if (visitor.stop) return;
visitor.stop = visitor.visit(node.element);
}
public void levelOrdertraversal() {
if (root == null) return;
Queue<Node<E>> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
Node<E> node = queue.poll();
System.out.print(node.element + " ");
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
protected static class Node<E> {
E element;
Node<E> left;
Node<E> right;
Node<E> parent;
public Node(E element, Node<E> parent) {
this.element = element;
this.parent = parent;
}
public boolean isLeft() {
return left == null && right == null;
}
public boolean hasTwoChidren() {
return left != null && right != null;
}
public boolean isLeftChild(){
return parent != null && this == parent.left ;
}
public boolean isRightChild(){
return parent != null && this == parent.right ;
}
public Node<E> sibling(){
return isLeftChild() ? parent.right : (isRightChild() ? parent.left : null ) ;
}
}
public int height() {
return height(root);
}
public int height(Node<E> node) {
if (node == null) return 0;
return 1 + Math.max(height(node.left), height(node.right));
}
protected Node<E> createNode(E element, Node<E> parent){
return new Node<E>(element,parent);
}
public int levelheight() {
if (root == null) return 0;
int height = 0;
int levelSize = 1;
Queue<Node<E>> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
Node<E> node = queue.poll();
levelSize--;
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
if (levelSize == 0) {
levelSize = queue.size();
height++;
}
}
return height;
}
public boolean isCompleteTree() {
if (root == null) return false;
Queue<Node<E>> queue = new LinkedList<>();
queue.offer(root);
boolean left = false;
while (!queue.isEmpty()) {
Node<E> node = queue.poll();
if (left && !node.isLeft()) {
return false;
}
if (node.hasTwoChidren()) {
queue.offer(node.left);
queue.offer(node.right);
} else if (node.left == null && node.right != null) {
return false;
} else {
left = true;
}
}
return true;
}
protected Node<E> predesessor(Node<E> node) {
if (node == null) return null;
Node<E> p = node.left;
if (p != null) {
while (p.right != null) {
p = p.right;
}
return p;
}
while (node.parent != null && node == node.parent.left) {
node = node.parent;
}
return node.parent;
}
protected Node<E> successor(Node<E> node) {
if (node == null) return null;
Node<E> p = node.right;
if (p != null) {
while (p.left != null) {
p = p.left;
}
return p;
}
while (node.parent != null && node == node.parent.right) {
node = node.parent;
}
return node.parent;
}
@Override
public Object root() {
return root;
}
@Override
public Object left(Object node) {
return ((Node<E>) node).left;
}
@Override
public Object right(Object node) {
return ((Node<E>) node).right;
}
@Override
public Object string(Object node) {
Node<E> myNode = (Node<E>) node;
return myNode;
}
}
公共部分
package zhang.tree.rbtree;
import java.util.Comparator;
public class BBST<E> extends BST<E>{
public BBST(Comparator<E> comparator) {
super(comparator);
}
public BBST() {
}
protected void rotateLeft(Node<E> grand){
Node<E> parent = grand.right ;
Node<E> child = parent.left ;
grand.right = child ;
parent.left = grand ;
afterRotate(grand,parent,child) ;
}
protected void rotateRight(Node<E> grand){
Node<E> parent = grand.left ;
Node<E> child = parent.right ;
grand.left = child;
parent.right = grand ;
afterRotate(grand,parent,child) ;
}
protected void afterRotate(Node<E> grand,Node<E> parent,Node<E> child){
parent.parent = grand.parent ;
if (grand.isLeftChild()){
grand.parent.left = parent ;
}else if (grand.isRightChild()){
grand.parent.right = parent ;
}else {
root =parent ;
}
if (child!=null){
child.parent = grand ;
}
grand.parent = parent ;
}
protected void rotate(
Node<E> r,
Node<E> b, Node<E> c,
Node<E> d,
Node<E> e, Node<E> f) {
d.parent = r.parent;
if(r.isLeftChild()){
r.parent.left = d;
}else if(r.isRightChild()){
r.parent.right = d;
}else{
root = d;
}
b.right = c;
if(c!=null){
c.parent = b;
}
f.left = e;
if(e != null){
e.parent = f;
}
d.left = b;
d.right = f;
b.parent = d;
f.parent = d;
}
}
二叉搜索树
package zhang.tree.rbtree;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Queue;
public class BST<E> extends BinaryTree<E> {
private final Comparator<E> comparator;
public BST(Comparator<E> comparator) {
this.comparator = comparator;
}
public BST() {
this(null);
}
public void add(E element) {
elementNotNullCheck(element);
if (root == null) {
root = createNode(element,null);
size++;
afterAdd(root);
return;
}
Node<E> parent = null;
Node<E> node = root;
int compare = 0;
while (node != null) {
compare = compare(element, node.element);
parent = node;
if (compare > 0) {
node = node.right;
} else if (compare < 0) {
node = node.left;
} else {
node.element = element;
return;
}
}
Node<E> newNode = createNode(element,parent);
if (compare > 0) {
parent.right = newNode;
} else {
parent.left = newNode;
}
size++;
afterAdd(newNode);
}
protected void afterAdd(Node<E> node){
}
protected void afterRemove(Node<E> node){
}
public void remove(E element) {
remove(node(element));
}
public void remove(Node<E> node){
if (node==null) return;
if (node.hasTwoChidren()){
Node<E> predesessor = predesessor(node);
node.element = predesessor.element;
node = predesessor;
}
Node<E> replacement = node.left!=null?node.left:node.right ;
if(replacement!=null){
replacement.parent=node.parent ;
if (node.parent == null){
root = replacement ;
}else if (node==node.parent.left){
node.parent.left = replacement ;
}else{
node.parent.right = replacement ;
}
afterRemove(replacement);
}else if (node.parent==null){
root = null ;
afterRemove(node);
}else {
if (node==node.parent.left){
node.parent.left = null ;
}else {
node.parent.right = null ;
}
afterRemove(node);
}
size--;
}
private Node<E> node(E element){
Node<E> node = root ;
while (node!=null){
int cmp = compare(element,node.element);
if (cmp==0){
return node;
}
if (cmp>0){
node = node.right ;
}else {
node = node.left ;
}
}
return null ;
}
public boolean contains(E element) {
return node(element) != null;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
return "BinarySearchTree{" +
"size=" + size +
", root=" + root +
", comparator=" + comparator +
'}';
}
AVL树
package zhang.tree.rbtree;
import java.util.Comparator;
public class AVLTree<E> extends BBST<E> {
public AVLTree() {
this(null);
}
public AVLTree(Comparator<E> comparator) {
super(comparator);
}
@Override
protected void afterAdd(Node<E> node) {
while ((node = node.parent) != null) {
if (isBalanced(node)) {
updateHeight(node);
} else {
rebalance(node);
break;
}
}
}
@Override
protected void afterRemove(Node<E> node) {
while ((node = node.parent) != null) {
if (isBalanced(node)) {
updateHeight(node);
} else {
rebalance(node);
}
}
}
@Override
protected Node<E> createNode(E element, Node<E> parent) {
return new AVLNode<E>(element, parent);
}
private void rebalance(Node<E> grand) {
Node<E> parent = ((AVLNode<E>)grand).tallerChild();
Node<E> node = ((AVLNode<E>)parent).tallerChild();
if (parent.isLeftChild()){
if (node.isLeftChild()){
rotateRight(grand);
}else {
rotateLeft(parent);
rotateRight(grand);
}
}else {
if(node.isLeftChild()){
rotateRight(parent);
rotateLeft(grand);
}else {
rotateLeft(grand);
}
}
}
private void rebalance2(Node<E> grand){
Node<E> parent = ((AVLNode<E>)grand).tallerChild();
Node<E> node = ((AVLNode<E>)parent).tallerChild();
if(parent.isLeftChild()){
if(node.isLeftChild()){
rotate(grand, node, node.right, parent, parent.right, grand);
}else{
rotate(grand, parent, node.left, node, node.right, grand);
}
}else{
if(node.isLeftChild()){
rotate(grand, grand, node.left, node, node.right, parent);
}else{
rotate(grand, grand, parent.left, parent, node.left, node);
}
}
}
@Override
protected void afterRotate(Node<E> grand, Node<E> parent, Node<E> child) {
super.afterRotate(grand, parent, child);
updateHeight(grand);
updateHeight(parent);
}
@Override
protected void rotate(Node<E> r, Node<E> b, Node<E> c, Node<E> d, Node<E> e, Node<E> f) {
super.rotate(r, b, c, d, e, f);
updateHeight(b);
updateHeight(f);
updateHeight(d);
}
private boolean isBalanced(Node<E> node) {
return Math.abs(((AVLNode<E>) node).balanceFactor()) <= 1;
}
private void updateHeight(Node<E> node) {
((AVLNode<E>) node).updateHeight();
}
private static class AVLNode<E> extends Node<E> {
public AVLNode(E element, Node<E> parent) {
super(element, parent);
}
int height = 1;
public int balanceFactor() {
int leftHeight = left == null ? 0 : ((AVLNode<E>) left).height;
int rightHeight = right == null ? 0 : ((AVLNode<E>) right).height;
return leftHeight - rightHeight;
}
private void updateHeight() {
int leftHeight = left == null ? 0 : ((AVLNode<E>) left).height;
int rightHeight = right == null ? 0 : ((AVLNode<E>) right).height;
height = 1 + Math.max(leftHeight, rightHeight);
}
public Node<E> tallerChild(){
int leftHeight = left == null ? 0 : ((AVLNode<E>) left).height;
int rightHeight = right == null ? 0 : ((AVLNode<E>) right).height;
if (leftHeight > rightHeight) return left ;
if (leftHeight < rightHeight) return right ;
return isLeftChild() ? left : right ;
}
}
}
红黑树
package zhang.tree.rbtree;
import java.util.Comparator;
public class RBTree<E> extends BBST<E> {
public static final boolean RED = false;
public static final boolean BLACK = true;
public RBTree(Comparator<E> comparator) {
super(comparator);
}
public RBTree() {
this(null);
}
@Override
protected void afterAdd(Node<E> node) {
Node<E> parent = node.parent;
if (parent == null) {
black(node);
return;
}
if (isBlack(parent)) return;
Node<E> uncle = parent.sibling();
Node<E> grand = red(parent.parent);
if (isRed(uncle)) {
black(parent);
black(uncle);
afterAdd(grand);
return;
}
if (parent.isLeftChild()) {
if (node.isLeftChild()) {
black(parent);
} else {
black(node);
rotateLeft(parent);
}
rotateRight(grand);
} else {
if (node.isLeftChild()) {
black(node);
rotateRight(parent);
} else {
black(parent);
}
rotateLeft(grand);
}
}
@Override
protected void afterRemove(Node<E> node) {
if (isRed(node)) return;
if (isRed(node)) {
black(node);
return;
}
Node<E> parent = node.parent;
if (parent == null) return;
boolean left = parent.left == null || node.isLeftChild() ;
Node<E> sibling = left ? parent.right : parent.left;
if (left) {
if (isRed(sibling)) {
black(sibling);
red(parent);
rotateLeft(parent);
sibling = parent.right;
}
if (isBlack(sibling.right) && isBlack(sibling.left)) {
boolean parentBlack = isBlack(parent);
black(parent);
red(sibling);
if (parentBlack) {
afterRemove(parent);
}
} else {
if (isBlack(sibling.right)) {
rotateRight(sibling);
sibling = parent.right;
}
color(sibling, colorOf(parent));
black(parent);
black(sibling.right);
rotateLeft(parent);
}
} else {
if (isRed(sibling)) {
black(sibling);
red(parent);
rotateRight(parent);
sibling = parent.left;
}
if (isBlack(sibling.right) && isBlack(sibling.left)) {
boolean parentBlack = isBlack(parent);
black(parent);
red(sibling);
if (parentBlack) {
afterRemove(parent);
}
} else {
if (isBlack(sibling.left)) {
rotateLeft(sibling);
sibling = parent.left;
}
color(sibling, colorOf(parent));
black(parent);
black(sibling.left);
rotateRight(parent);
}
}
}
public Node<E> color(Node<E> node, boolean color) {
if (node == null) return node;
((RBNode<E>) node).color = color;
return node;
}
private Node<E> red(Node<E> node) {
return color(node, RED);
}
private Node<E> black(Node<E> node) {
return color(node, BLACK);
}
private boolean colorOf(Node<E> node) {
return node == null ? BLACK : ((RBNode<E>) node).color;
}
private boolean isBlack(Node<E> node) {
return colorOf(node) == BLACK;
}
private boolean isRed(Node<E> node) {
return colorOf(node) == RED;
}
@Override
protected Node<E> createNode(E element, Node<E> parent) {
return new RBNode<>(element, parent);
}
public static class RBNode<E> extends Node<E> {
boolean color = RED;
public RBNode(E element, Node<E> parent) {
super(element, parent);
}
@Override
public String toString() {
String str = "";
if (color == RED) {
str = "R_";
}
return str + element.toString();
}
}
}