private void leftRotate(Node<T> p) { //https://www.processon.com/diagraming/5ef3f1f8f346fb1ae57babff if (p != null) { // 保存当前节点的右子树 Node<T> r = p.right; // 将右子树的左子树赋给当前节点的右子树 p.right = r.left; //如果r.left不为空将r.left的爹设置为p // P // // p.right r.left if (r.left != null) r.left.parent = p; //r.的爹设置为p的爹 r.parent = p.parent; //p没爹代表他是根节点 if (p.parent == null) //r是要替换p的所以r设为根节点 root = r; //p是他爹的左节点 else if (p.parent.left == p) //p他爹的左节点替换成R R p.parent.left = r; else //p是他爹的右节点 //p他爹的右节点替换成R R替换P p.parent.right = r; //r的左儿子=P r.left = p; //P的爹=r p.parent = r; } } private void rightRotate(Node<T> p) { if (p != null) { Node<T> l = p.left; p.left = l.right; if (l.right != null) l.right.parent = p; l.parent = p.parent; if (p.parent == null) root = null; else if (p.parent.right == p) p.parent.right = l; else p.parent.left = l; l.right = p; p.parent = l; } }
private void leftRotate(Node<T> p) {//https://www.processon.com/diagraming/5ef3f1f8f346fb1ae57babffif (p != null) {// 保存当前节点的右子树Node<T> r = p.right;// 将右子树的左子树赋给当前节点的右子树p.right = r.left;//如果r.left不为空将r.left的爹设置为p// P// // p.right r.leftif (r.left != null)r.left.parent = p;//r.的爹设置为p的爹r.parent = p.parent;//p没爹代表他是根节点if (p.parent == null)//r是要替换p的所以r设为根节点root = r; //p是他爹的左节点else if (p.parent.left == p)//p他爹的左节点替换成R Rp.parent.left = r;else//p是他爹的右节点//p他爹的右节点替换成R R替换Pp.parent.right = r;//r的左儿子=Pr.left = p;//P的爹=rp.parent = r;}
}
private void rightRotate(Node<T> p) {
if (p != null) {Node<T> l = p.left;p.left = l.right;if (l.right != null)l.right.parent = p;l.parent = p.parent;if (p.parent == null)root = null;else if (p.parent.right == p)p.parent.right = l;elsep.parent.left = l;l.right = p;p.parent = l;}
}
原文:https://www.cnblogs.com/lanbingnie/p/13732739.html