java平衡树算法_数据结构与算法-java-平衡二叉树(AVL树)

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;

}

}

}

}

做一门精致,全面详细的 java数据结构与算法!!!让天下没有难学的数据结构,让天下没有难学的算法,不吹不黑,我们的讲师及其敬业,可以看到课程视频,课件,代码的录制撰写,都是在深夜,如此用心,其心可鉴,他不掉头发,谁掉头发???总之你知道的,不知道的,我们都讲,并且持续更新,走过路过,不要错过,不敢说是史上最全的课程,怕违反广告法,总而言之,言而总之,这门课你值得拥有,好吃不贵,对于你知识的渴求,我们管够管饱话不多说,牛不多吹,我们要讲的本门课程内容:稀疏数组、单向队列、环形队列、单向链表、双向链表、环形链表、约瑟夫问题、栈、前缀、中缀、后缀表达式、中缀表达式转换为后缀表达式、递归与回溯、迷宫问题、八皇后问题、算法的时间复杂度、冒泡排序、选择排序、插入排序、快速排序、归并排序、希尔排序、基数排序(桶排序)、堆排序、排序速度分析、二分查找、插值查找、斐波那契查找、散列、哈希表、二叉树、二叉树与数组转换、二叉排序树(BST)、AVL树、线索二叉树、赫夫曼树、赫夫曼编码、多路查找树(B树B+树和B*树)、图、图的DFS算法和BFS、程序员常用10大算法、二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法马踏棋盘算法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值