以下为二叉树的节点类,有指向父亲的引用。
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
public class BinTreeNode {
private int data;
private BinTreeNode pre;
private BinTreeNode right;
private BinTreeNode left;
public BinTreeNode(int data,BinTreeNode pre,BinTreeNode left, BinTreeNode right){
this.data = data;
this.pre = pre;
this.left = left;
this.right = right;
}
public int getSize(){
if(this==null){
return 0;
}
else{
return 1+this.left .getSize()+this.right.getSize();
}
}
public int getHeight() {
return 1+Math.max(this.left!=null?this.left.getHeight():-1, this.right!=null?this.right.getHeight():-1);
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BinTreeNode getLeft() {
return left;
}
public void setLeft(BinTreeNode left) {
this.left = left;
}
public BinTreeNode getRight() {
return right;
}
public void setRight(BinTreeNode right) {
this.right = right;
}
//节点所在位置是否平衡
public static boolean isnodebalance(BinTreeNode v){
BinTreeNode p = v;
Queue<BinTreeNode> q = new LinkedBlockingDeque<BinTreeNode>();
q.add(p);
while(!q.isEmpty()){
p = q.poll();
if( Math.abs((p.getRight()!=null?p.getRight().getHeight():-1) - (p.getLeft()!=null?p.getLeft().getHeight():-1))>1) return false;
if(p.getLeft()!=null) q.add(p.getLeft());
if(p.getRight()!=null) q.add(p.getRight());
}
return true;
}
public BinTreeNode getPre() {
return pre;
}
public void setPre(BinTreeNode pre) {
this.pre = pre;
}
}
/*
* AVL树的遍历以及插入,删除,后维持平衡的算法。*/
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingDeque;
public class AVLtree {
private BinTreeNode root;
private BinTreeNode hot;
public AVLtree(int c) {
root = new BinTreeNode (c,null,null,null);
}
//层次遍历
public void layoutprint(){
BinTreeNode p = root;
Queue<BinTreeNode> q = new LinkedBlockingDeque<BinTreeNode>();
q.add(p);
while(!q.isEmpty()){
p = q.poll();
System.out.println(""+p.getData());
if(p.getLeft()!=null) q.add(p.getLeft());
if(p.getRight()!=null) q.add(p.getRight());
}
}
//中序遍历非递归
public void inorderprint(){
Stack<BinTreeNode> s = new Stack<BinTreeNode>();
BinTreeNode p = root;
goleft(p,s);
while(!s.isEmpty()){
p=s.pop();
System.out.println(""+p.getData());
if(p.getRight()!=null) goleft(p.getRight(),s);
}
}
private void goleft(BinTreeNode p,Stack<BinTreeNode> s){
BinTreeNode q = p;
while(q!=null){
s.push(q);
q=q.getLeft();
}
}
//先序遍历
public void fristprint(){
Stack<BinTreeNode> s = new Stack<BinTreeNode>();
BinTreeNode p = root;
visitleft(p, s);
while(!s.isEmpty()){
p=s.pop();
visitleft(p, s);
}
}
private void visitleft(BinTreeNode p,Stack<BinTreeNode> s){
BinTreeNode q = p;
while(q!=null){
System.out.println(""+q.getData());
if(q.getRight()!=null) s.push(q.getRight());
q=q.getLeft();
}
}
//后序遍历
public void lastprint (){
BinTreeNode p = root;
Stack<lastnode> s = new Stack<lastnode>();
lastnode lnode = new lastnode();
lnode.node=p;
lnode.flag=1;
s.add(lnode);
while(!s.isEmpty()){
lnode = s.pop();
if(lnode.flag == 3) System.out.println(""+lnode.node.getData());
else{
s.push(lnode);
if(lnode.node.getRight()!=null){
lnode.flag++;
lastnode londe1 = new lastnode(lnode.node.getRight(), 1);
s.push(londe1);
}
else{
lnode.flag++;
}
if(lnode.node.getLeft()!=null){
lnode.flag++;
lastnode londe1 = new lastnode(lnode.node.getLeft(), 1);
s.push(londe1);
}
else{
lnode.flag++;
}
}
}
}
private class lastnode{
BinTreeNode node;
int flag;
public lastnode(){
}
public lastnode(BinTreeNode p,int i){
this.node = p;
this.flag =i;
}
}
//判断树是否平衡
public boolean isbalance(){
BinTreeNode p = root;
Queue<BinTreeNode> q = new LinkedBlockingDeque<BinTreeNode>();
q.add(p);
while(!q.isEmpty()){
p = q.poll();
if( Math.abs((p.getRight()!=null?p.getRight().getHeight():-1) - (p.getLeft()!=null?p.getLeft().getHeight():-1))>1) return false;
if(p.getLeft()!=null) q.add(p.getLeft());
if(p.getRight()!=null) q.add(p.getRight());
}
return true;
}
//寻找操作
public BinTreeNode search(int x){
BinTreeNode p = root;
hot = p;
while(p!=null){
if(p.getData()==x){
return p;
}
hot = p;
p=p.getData()<x?p.getRight():p.getLeft();
}
return null;
}
//插入操作
public BinTreeNode insert(int x){
BinTreeNode p = null;
BinTreeNode q = null;
p=search(x);
if(p!=null){
return p;
}
else{
q = new BinTreeNode(x,hot,null,null);
if(hot.getData()>x){
hot.setLeft(q);
}
else{
hot.setRight(q);
}
}
for(BinTreeNode g = hot;g!=null;g=g.getPre()){
if(!BinTreeNode.isnodebalance(g)){
g=rebalance(AVLtree.tallerChild(AVLtree.tallerChild(g)));
break;
}
}
return q;
}
//树重平衡操作,传入的为当前失衡节点
private BinTreeNode rebalance(BinTreeNode q){
BinTreeNode v =q,p=v.getPre(),g=p.getPre();
BinTreeNode b = null;
BinTreeNode gper = g.getPre();
BinTreeNode gp = g;
if(g.getLeft()==p&&p.getLeft()==v){
b=connect34(v,p,g,v.getLeft(),v.getRight(),p.getRight(),g.getRight());
}
else if(g.getLeft()==p&&p.getRight()==v){
b=connect34(p,v,g,p.getLeft(),v.getLeft(),v.getRight(),g.getRight());
}
else if(g.getRight()==p&&p.getLeft()==v){
b=connect34(g, v, p, g.getLeft(), v.getLeft(), v.getRight(), p.getRight());
}
else {
b=connect34(g,p,v,g.getLeft(),p.getLeft(),v.getLeft(),v.getRight());
}
if(gper==null){root = b;b.setPre(null);}
else{
if(gper.getLeft()==gp) gper.setLeft(b);
else {
gper.setRight(b);
}
b.setPre(gper);
}
return b;
}
//3+4重构
private BinTreeNode connect34(BinTreeNode a,BinTreeNode b,BinTreeNode c,
BinTreeNode t0,BinTreeNode t1,BinTreeNode t2,BinTreeNode t3){
if(a!=null){a.setLeft(t0);if(t0!=null) t0.setPre(a);}
if(a!=null){a.setRight(t1);if(t1!=null)t1.setPre(a);}
if(b!=null){c.setLeft(t2);if(t2!=null)t2.setPre(c);}
if(b!=null){c.setRight(t3);if(t3!=null)t3.setPre(c);}
b.setLeft(a);a.setPre(b);
b.setRight(c);c.setPre(b);
return b;
}
//根据传入的节点,找到比节点的最高孩子
private static BinTreeNode tallerChild(BinTreeNode v){
int lh = v.getLeft()==null?-1:v.getLeft().getHeight();
int rh = v.getRight()==null?-1:v.getRight().getHeight();
if(lh>rh) return v.getLeft();
else if(lh<rh) return v.getRight();
else return null;
}
//删除操作
public boolean remove(int x){
BinTreeNode p = null;
p=search(x);
if(p==null){
return false;
}
else{
if(p.getLeft()==null){
if(hot.getLeft()==p){
hot.setLeft(p.getRight());
}
if(hot.getRight()==p){
hot.setRight(p.getRight());
}
}
if(p.getRight()==null){
if(hot.getRight()==p){
hot.setRight(p.getLeft());
}
if(hot.getLeft()==p){
hot.setLeft(p.getLeft());
}
}
else{
BinTreeNode q = p.getRight();
hot = p;
while(q.getLeft()!=null){
hot =q;
q=q.getLeft();
}
p.setData(q.getData());
hot.setRight(q.getRight());
}
for(BinTreeNode g=hot; g!=null;g=g.getPre()){
if(!BinTreeNode.isnodebalance(g)){
g=rebalance(tallerChild(tallerChild(g)));
}
}
return true;
}
}
public static void main(String[] args){
AVLtree avl = new AVLtree(18);
avl.insert(17);
avl.insert(16);
avl.insert(15);
avl.insert(14);
avl.insert(13);
avl.insert(12);
avl.insert(11);
avl.insert(10);
avl.insert(27);
avl.insert(26);
avl.insert(25);
avl.insert(24);
avl.insert(23);
avl.insert(22);
avl.insert(21);
avl.insert(20);
avl.insert(37);
avl.insert(36);
avl.insert(35);
avl.insert(34);
avl.insert(33);
avl.insert(32);
avl.insert(31);
avl.insert(30);
System.out.println(".......层次遍历..........");
avl.layoutprint();
System.out.println("........中序遍历.........");
avl.inorderprint();
avl.remove(18);
System.out.println(".......层次遍历..........");
avl.layoutprint();
System.out.println("........中序遍历.........");
avl.inorderprint();
System.out.println("......."+avl.isbalance()+".......");
}
}