packageAVLTree;importsun.font.DelegatingShape;public classAVLTreeDemo {public static voidmain(String[] args) {int []arr = {4,3,6,5,7,8};
AVLTree avlTree= newAVLTree();for(int i=0;i
{
avlTree.Treeadd(newNode(arr[i]));
}
System.out.println("中序遍历");
avlTree.TreeinfixOrder();
System.out.println("在平衡处理之后");
System.out.println("树的高度Wie"+avlTree.getRoot().height());
System.out.println("树的左子高度Wie"+avlTree.getRoot().leftHeight());
System.out.println("树的右子高度Wie"+avlTree.getRoot().rightHeight());
}
}classAVLTree{privateNode root;publicNode getRoot() {returnroot;
}public voidTreeadd(Node node){if(root==null)
{
root=node;
}else{
root.add(node);
}
}public voidTreeinfixOrder(){if (root == null) {
System.out.println("为空,无法遍历");
}else{
root.infixOrder();
}
}public Node Treesearch(intvalue)
{if (root == null) {return null;
}else{returnroot.search(value);
}
}public Node TreesearchParent(intvalue)
{if (root == null) {return null;
}else{returnroot.searchPartent(value);
}
}public void delNode(intvalue)
{if(root == null)
{return;
}else{
Node tar=Treesearch(value);//如果没有找到
if(tar==null)
{return;
}if(root.left==null&&root.right==null)
{
root=null;//root置为空
return;
}//找父节点
Node parent =TreesearchParent(value);//如果要删除的节点是叶子节点
if(tar.left==null&&tar.right==null)
{//判断tar是父节点的左子节点还是右子节点
if(parent.left!=null&&parent.left.value==value)
{
parent.left=null; //设置为空,相对于删除并根据jvm的垃圾回收机制,会自动回收。
}else if(parent.right!=null&&parent.right.value==value){
parent.right=null; //设置为空,相对于删除并根据jvm的垃圾回收机制,会自动回收。
}
}
}
}
}classNode{intvalue;
Node left;
Node right;public Node(intvalue) {this.value =value;
}
@OverridepublicString toString() {return "Node{" +
"value=" + value +
'}';
}//返回以该节点为根节点的树的高度
public intheight(){return Math.max(left==null?0:left.height(),right==null?0:right.height()+1);
}//返回左子树的高度
public intleftHeight(){if(left==null)
{return 0;
}else{returnleft.height();
}
}//返回右子树的高度
public intrightHeight(){if(right==null)
{return 0;
}else{returnright.height();
}
}//左旋转的方法
private voidleftRotate(){//创建新节点,以当前根结点的值
Node newNode = newNode(value);//把新的节点的左子树设置成当前节点的左子树
newNode.left =left;//把新的节点的右子树设置成带你过去节点的右子树的左子树
newNode.right =right.left;//当前节点的值替换为右子节点的值
value=right.value;//吧当前节点的右子树设置为当前节点的右子树的右子树
right=right.right;//吧当前节点的左子树或者说左子节点设置成新的节点
left=newNode;
}public voidadd(Node node)
{if(node==null)
{return;
}//判断传入的结点的值,和当前子树的根结点的值关系,
if(node.value
{if(this.left==null)
{this.left=node;
}else{this.left.add(node);
}
}//>即右插
else{if(this.right==null)
{this.right=node;
}else{this.right.add(node);
}
}//当添加完节点后发现满足右子树-左子树>1所以,左旋转
if(rightHeight()-leftHeight()>1)
{
leftRotate();
}
}//中序遍历
public voidinfixOrder(){if (this.left != null)
{this.left.infixOrder();
}
System.out.println(this);if(this.right!=null)
{this.right.infixOrder();
}
}//如果找到返回该节点,没有就返回null
public Node search(intvalue){if(value==this.value) //说明就是该节点
{return this;
}else if(value
if(this.left==null)
{return null;
}return this.left.search(value); //--的过程
}else{if (this.right == null) {return null;
}return this.right.search(value);//--的过程
}
}//查找要删除节点的父节点
public Node searchPartent(intvalue)
{//该节点就是要删除的节点的父节点
if((this.left!=null&&this.left.value==value)||(this.right!=null&&this.right.value==value)){return this;
}else{if(value
{return this.left.searchPartent(value);//向左子树递归查找
}else if(value>=this.value&&this.right!=null)
{return this.right.searchPartent(value);//向右子树递归查找
}else{return null;
}
}
}
}