java avlTree insert()
package avltree;
public class AVLTree {
static class AVLTreeNode{
public int value = 0;
public int balance_factor = 0;
public AVLTreeNode left = null;
public AVLTreeNode right = null;
public AVLTreeNode parent = null;
public AVLTreeNode(int value) {
this.value = value;
}
}
public AVLTreeNode root = null;
public boolean insert(int val) throws Exception {
if(root == null){
root = new AVLTreeNode(val);
return true;
}
AVLTreeNode parent = null;
AVLTreeNode cur = root;
while(cur != null){
if(cur.value < val){
parent = cur;
cur = cur.right;
}else if(cur.value > val){
parent = cur;
cur = cur.left;
}else {
return false;
}
}
cur = new AVLTreeNode(val);
if(parent.value < val){
parent.right = cur;
}else {
parent.left = cur;
}
cur.parent = parent;
while(parent != null){
if(cur == parent.left){
parent.balance_factor--;
}else {
parent.balance_factor++;
}
if(parent.balance_factor == 0){
break;
}else if(parent.balance_factor == 1 || parent.balance_factor == -1){
cur = parent;
parent = parent.parent;
}else if(parent.balance_factor == 2){
if(cur.balance_factor == 1){
RotateL(parent);
}else {
RotateRL(parent);
}
break;
}else if(parent.balance_factor == -2){
if(cur.balance_factor == -1){
RotateR(parent);
}else{
RotateLR(parent);
}
break;
}else {
throw new Exception("balance_factor 异常 ");
}
}
return true;
}
private void RotateRL(AVLTreeNode parent) {
AVLTreeNode cur = parent.right;
AVLTreeNode curL = cur.left;
int balance_factor = curL.balance_factor;
RotateR(cur);
RotateL(parent);
if(balance_factor == -1){
parent.balance_factor = 0;
cur.balance_factor = 1;
curL.balance_factor = 0;
}else if(balance_factor == 1){
parent.balance_factor = -1;
cur.balance_factor = 0;
curL.balance_factor = 0;
}else {
parent.balance_factor = 0;
cur.balance_factor = 0;
curL.balance_factor = 0;
}
}
private void RotateLR(AVLTreeNode parent) {
AVLTreeNode cur = parent.left;
AVLTreeNode curR = cur.right;
int balance_factor = curR.balance_factor;
RotateL(cur);
RotateR(parent);
if(balance_factor == -1){
cur.balance_factor = 0;
parent.balance_factor = 1;
curR.balance_factor = 0;
}else if(balance_factor == 1){
parent.balance_factor = 0;
cur.balance_factor = -1;
curR.balance_factor = 0;
}else {
parent.balance_factor = 0;
cur.balance_factor = 0;
curR.balance_factor = 0;
}
}
private void RotateL(AVLTreeNode parent) {
AVLTreeNode cur = parent.right;
AVLTreeNode curL = cur.left;
parent.right = curL;
if(parent.parent != null){
if(parent == parent.parent.left){
parent.parent.left = cur;
}else {
parent.parent.right = cur;
}
cur.parent = parent.parent;
}else {
root = cur;
root.parent = null;
}
parent.parent = cur;
cur.left = parent;
if(curL != null){
curL.parent = parent;
}
parent.balance_factor = 0;
cur.balance_factor = 0;
}
private void RotateR(AVLTreeNode parent) {
AVLTreeNode cur = parent.left;
AVLTreeNode curR = cur.right;
parent.left = curR;
if(parent.parent != null){
if(parent == parent.parent.left){
parent.parent.left = cur;
}else {
parent.parent.right = cur;
}
cur.parent = parent.parent;
}else {
root = cur;
root.parent = null;
}
parent.parent = cur;
cur.right = parent;
if(curR != null){
curR.parent = parent;
}
parent.balance_factor = 0;
cur.balance_factor = 0;
}
void midOrder(AVLTreeNode root){
if(root == null) return ;
midOrder(root.left);
System.out.print(root.value+" ");
midOrder(root.right);
}
public static void main(String[] args) throws Exception {
AVLTree avlTree = new AVLTree();
int[] arr = {3,6,8,9,23,7,6,90};
for(int num : arr){
avlTree.insert(num);
}
avlTree.midOrder(avlTree.root);
}
}
|