注:文章中代码来自于何老师的代码,本文意在对于代码有更深的了解的解释文章
在二叉搜索树中,我们经常可能会遇到树的大小不均等的问题,可能会对时间复杂度有较大的影响,这个时候可以使用红黑树的连接法,来保证二叉搜索树的深度均等,来保证搜索时间复杂度维持在一个比较理想的区间。
在此函数中,get函数与二叉搜索树相同不变
- public Value get (Key key) {
- Node x = root;
- while (x != null) {
- int cmp = key.compareTo(x.key);
- if (cmp < 0) x = x.left;
- else if (cmp > 0) x = x.right;
- else if (cmp == 0) return x.val;
- }
- return null;
- }
root我们也需要进行一些改进,添加了color作为判断节点与父节点之间的关系(是否属于相同登记)使用BLACK and RED来代表红黑树的黑色和红色(代表在2—3树中等效),用来判断右旋和左旋以及变向的条件
- private class Node{
- private Key key;
- private Value val;
- private int count;
- private Node left,right;
- boolean color;
- public Node(Key key,Value val,boolean color){
- this.key=key;
- this.val=val;
- this.color=color;
- }
- }
- private Node rotateLeft(Node h){
- assert isRed(h.right);
- Node x=h.right;
- h.right=x.left;
- x.left=h;
- x.color=h.color;
- h.color=RED;
- return x;
- }
- private Node rotateRight(Node h){
- assert isRed(h.left);
- Node x=h.left;
- h.left=x.right;
- x.right=h;
- x.color=h.color;
- h.color=RED;
- return x;
- }
- private void flipColors(Node h) {
-
- assert !isRed( h);
- assert isRed( h.left);
- assert isRed( h.right);
- h.color = RED;
- h.left.color = BLACK;
- h.right.color = BLACK;
- }
put函数的改变比较大,因为为了维持二叉搜索树的深度适当,可能需要不停变换根节点以及各个父节点,因此在put进去一个node的时候,将node的颜色变成红色,并且向上逐一判断是否需要进行左旋右旋以及翻转
- private Node put (Node h, Key key, Value val) {
-
- if (h == null) return new Node( key, val, RED);
- int cmp = key.compareTo( h.key);
- if (cmp < 0) h.left = put( h.left, key, val);
- else if (cmp > 0) h.right = put( h.right, key, val);
- else if (cmp == 0) h.val = val;
- if (isRed(h.right) && !isRed(h.left)) h = rotateLeft( h);
- if (isRed(h.left) && isRed(h.left.left)) h = rotateRight( h);
- if (isRed(h.left) && isRed(h.right)) flipColors( h);
- return h;
- }
代码总体实例:
- public class BST2 <Key extends Comparable<Key>, Value>{
- private static final boolean RED=true;
- private static final boolean BLACK=false;
-
- private Node root;
- private class Node{
- private Key key;
- private Value val;
- private int count;
- private Node left,right;
- boolean color;
- public Node(Key key,Value val,boolean color){
- this.key=key;
- this.val=val;
- this.color=color;
- }
- }
- public int size()
- { return size( root); }
- private int size(BST2.Node x){
- if (x==null) return 0;
- return x.count;
- }
- public Value get (Key key) {
- Node x = root;
- while (x != null) {
- int cmp = key.compareTo(x.key);
- if (cmp < 0) x = x.left;
- else if (cmp > 0) x = x.right;
- else if (cmp == 0) return x.val;
- }
- return null;
- }
- private boolean isRed(Node x){
- if (x==null) return false;
- return x.color==RED;
- }
- private Node rotateLeft(Node h){
- assert isRed(h.right);
- Node x=h.right;
- h.right=x.left;
- x.left=h;
- x.color=h.color;
- h.color=RED;
- return x;
- }
- private Node rotateRight(Node h){
- assert isRed(h.left);
- Node x=h.left;
- h.left=x.right;
- x.right=h;
- x.color=h.color;
- h.color=RED;
- return x;
- }
- private void flipColors(Node h) {
-
- assert !isRed( h);
- assert isRed( h.left);
- assert isRed( h.right);
- h.color = RED;
- h.left.color = BLACK;
- h.right.color = BLACK;
- }
- private Node put (Node h, Key key, Value val) {
-
- if (h == null) return new Node( key, val, RED);
- int cmp = key.compareTo( h.key);
- if (cmp < 0) h.left = put( h.left, key, val);
- else if (cmp > 0) h.right = put( h.right, key, val);
- else if (cmp == 0) h.val = val;
- if (isRed(h.right) && !isRed(h.left)) h = rotateLeft( h);
- if (isRed(h.left) && isRed(h.left.left)) h = rotateRight( h);
- if (isRed(h.left) && isRed(h.right)) flipColors( h);
- return h;
- }
- }