平衡二叉树(AVL)java实现
数的节点
package com.ydp.tree.AVLTree;
public class Node{
private int data = 0;
private Node lchild = null;
private Node rchild = null;
private Node parent = null; public Node(){};
public Node(int data){
this.data = data;
}
public Node(int data,Node parent){
this.data = data;
this.parent = parent;
} public boolean hasLChild(){
return lchild != null;
} public boolean hasRChild(){
return rchild != null;
}
//当前节点树的深度
public int getTreeDepth(){
return getTreeDepth(this);
}
//指定节点树的深度
protected int getTreeDepth(Node node){
int depth = 0;
if(node != null){
int ldepth = getTreeDepth(node.getLchild());
int rdepth = getTreeDepth(node.getRchild());
depth =1 + (ldepth>rdepth?ldepth:rdepth);
}
return depth;
} public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getLchild() {
return lchild;
}
public void setLchild(Node lchild) {
this.lchild = lchild;
}
public Node getRchild() {
return rchild;
}
public void setRchild(Node rchild) {
this.rchild = rchild;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
} public boolean isLChild(){ return this.parent.hasLChild()?this.parent.getLchild().getData()==this.data:false;
} public boolean equals(Node node){
return this.data == node.getData();
} }
平衡二叉树的实现
package com.ydp.tree.AVLTree;
import java.util.Stack;
public class AVLTree {
Node root = null;
public static void main(String[] args) {
AVLTree tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(30);
tree.print();
tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(60);
tree.insert(30);
tree.insert(45);
tree.insert(20);
tree.print();
tree = new AVLTree();
tree.insert(50);
tree.insert(60);
tree.insert(70);
tree.print();
tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(60);
tree.insert(55);
tree.insert(70);
tree.insert(80);
tree.print();
tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(45);
tree.print();
tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(60);
tree.insert(30);
tree.insert(45);
tree.insert(47);
tree.print();
tree = new AVLTree();
tree.insert(50);
tree.insert(60);
tree.insert(55);
tree.print();
tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(55);
tree.insert(53);
tree.insert(60);
tree.insert(70);
tree.print();
}
public void print(){
System.out.println("树的深度:"+this.getRoot().getTreeDepth());
this.preOrder();
System.out.println();
this.midOrder();
System.out.println("\n");
}
//插入节点数据
public void insert(int data){
if(this.root == null){
this.root = new Node(data);
}else{
insert(data,this.root);
}
}
//递归插入,将数据插入到合适的位置
protected void insert(int data,Node node){
if(data>node.getData()){
if(node.hasRChild()){
insert(data,node.getRchild());
}else{
node.setRchild(new Node(data,node));
}
if(getTreeDepth(node.getRchild())-getTreeDepth(node.getLchild())==2){
if(data>node.getRchild().getData()){
leftRotate(node);
}else{
rightLeftRotate(node);
}
}
}else if(data<node.getData()){
if(node.hasLChild()){
insert(data,node.getLchild());
}else{
node.setLchild(new Node(data,node));
}
if(getTreeDepth(node.getLchild())-getTreeDepth(node.getRchild())==2){
if(data<node.getLchild().getData()){
rightRotate(node);
}else{
leftRightRotate(node);
}
}
}
}
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
//顺时针旋转
public void rightRotate(Node node){
System.out.println("顺时针:"+node.getData());
Node tmp = node.getLchild();
if(node.getParent() == null){
this.root=node.getLchild();
}else{
if(node.isLChild()){
node.getParent().setLchild(tmp);
}else{
node.getParent().setRchild(tmp);
}
}
tmp.setParent(node.getParent());
node.setLchild(tmp.getRchild());
node.setParent(tmp);
tmp.setRchild(node);
}
//先顺后逆时针
public void rightLeftRotate(Node node){
System.out.println("先顺后逆时针:"+node.getData());
rightRotate(node.getRchild());
leftRotate(node);
}
//逆时针
public void leftRotate(Node node){
System.out.println("逆时针:"+node.getData());
Node tmp = node.getRchild();
if(node.getParent() == null){
this.root=node.getRchild();
}else{
if(node.isLChild()){
node.getParent().setLchild(tmp);
}else{
node.getParent().setRchild(tmp);
}
}
tmp.setParent(node.getParent());
node.setRchild(tmp.getLchild());
node.setParent(tmp);
tmp.setLchild(node);
}
//逆时针
public void leftRightRotate(Node node){
System.out.println("先逆后顺时针:"+node.getData());
leftRotate(node.getLchild());
rightRotate(node);
}
//先序遍历
public void preOrder(){
Stack<Node> stack = new Stack<Node>();
Node node = root;
while(node != null || !stack.empty()){
while(node != null){
System.out.print(node.getData()+" ");
stack.push(node);
node = node.getLchild();
}
node = stack.pop();
node = node.getRchild();
}
}
//中序遍历
public void midOrder(){
Stack<Node> stack = new Stack<Node>();
Node node = root;
while(node != null || !stack.empty()){
while(node != null){
stack.push(node);
node = node.getLchild();
}
node = stack.pop();
System.out.print(node.getData()+" ");
node = node.getRchild();
}
}
protected int getTreeDepth(Node node){
int depth = 0;
if(node != null){
int ldepth = getTreeDepth(node.getLchild());
int rdepth = getTreeDepth(node.getRchild());
depth =1 + (ldepth>rdepth?ldepth:rdepth);
}
return depth;
}
}
平衡二叉树(AVL)java实现的更多相关文章
- 平衡二叉树(AVL)的理解和实现(Java)
AVL的定义 平衡二叉树:是一种特殊的二叉排序树,其中每一个节点的左子树和右子树的高度差至多等于1.从平衡二叉树的名字中可以看出来,它是一种高度平衡的二叉排序树.那么什么叫做高度平衡呢?意思就是要么它 ...
- Java 树结构实际应用 四(平衡二叉树/AVL树)
平衡二叉树(AVL 树) 1 看一个案例(说明二叉排序树可能的问题) 给你一个数列{1,2,3,4,5,6},要求创建一颗二叉排序树(BST), 并分析问题所在. 左边 BST 存在的问题分析: ...
- 数据结构与算法--从平衡二叉树(AVL)到红黑树
数据结构与算法--从平衡二叉树(AVL)到红黑树 上节学习了二叉查找树.算法的性能取决于树的形状,而树的形状取决于插入键的顺序.在最好的情况下,n个结点的树是完全平衡的,如下图"最好情况&q ...
- 二叉查找树(BST)、平衡二叉树(AVL树)(只有插入说明)
二叉查找树(BST).平衡二叉树(AVL树)(只有插入说明) 二叉查找树(BST) 特殊的二叉树,又称为排序二叉树.二叉搜索树.二叉排序树. 二叉查找树实际上是数据域有序的二叉树,即对树上的每个结点, ...
- 平衡二叉树AVL - 插入节点后旋转方法分析
平衡二叉树 AVL( 发明者为Adel'son-Vel'skii 和 Landis)是一种二叉排序树,其中每一个节点的左子树和右子树的高度差至多等于1. 首先我们知道,当插入一个节点,从此插入点到树根 ...
- 二叉查找树、平衡二叉树(AVL)、B+树、联合索引
1. [定义] 二叉排序树(二拆查找树)中,左子树都比节点小,右子树都比节点大,递归定义. [性能] 二叉排序树的性能取决于二叉树的层数 最好的情况是 O(logn),存在于完全二叉排序树情况下,其访 ...
- java平衡二叉树AVL数
平衡二叉树(Balanced Binary Tree)具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树 右旋:在插入二叉树的时候,根节点的右侧高 ...
- 【数据结构】平衡二叉树—AVL树
(百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...
- 平衡二叉树AVL删除
平衡二叉树的插入过程:http://www.cnblogs.com/hujunzheng/p/4665451.html 对于二叉平衡树的删除采用的是二叉排序树删除的思路: 假设被删结点是*p,其双亲是 ...
- 平衡二叉树AVL插入
平衡二叉树(Balancedbinary tree)是由阿德尔森-维尔斯和兰迪斯(Adelson-Velskiiand Landis)于1962年首先提出的,所以又称为AVL树. 定义:平衡二叉树或为 ...
随机推荐
- CSS Clip剪切元素实例
一.实例1: .fixed { position: fixed; width: 382px; height: 100px; background: red; border: 1px solid blu ...
- EF Lambda 多表查询
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mv ...
- bash: ./configure: 权限不够 怎么办?
configure没有执行权限 通过chmod给其加上x权限 chmod +x configure 再在该用户下执行 ./configure
- sublime text 2 笔记
sublime text 2 ,是代码程序员最佳编辑器,不是之一.其快捷优雅的操作风格,和便利的快捷键,是程序员码农的不二选择. 网上下载sublime text 2,支持文件拖放,文件夹拖放.3.0 ...
- 关于cocoapods和swift中使用oc第三方
mac 系统自带ruby,使用cocoapods,直接安装cocoapods就行 终端:$ sudo gem install cocoapods {安装较慢是因为有墙,查看ruby镜像列表:$ gem ...
- hdoj 1251 字典树
代码: #include <stdio.h>#define MAX 26 typedef struct TrieNode{ int nCount; struct ...
- OPENGL 地形
用OPNEGL弄了好久,终于有个地形的样子了! 看起来还是很糟糕....
- 启动scala的方法
1.从官网 http://www.scala-lang.org/download/ 下载scala二进制通用版本以后,在终端命令行添加下载解压包的bin目录到环境变量: export PATH=/Us ...
- ios开发之xcode6中如何添加pch全局引用文件
xcode6中去掉了默认添加pch文件,这就需要我们自己手动添加pch文件了,添加pch文件是为了一些琐碎的头文件引用,加快编译速度! 下面就说下该如何手动添加pch文件: 1.添加一个文件,在oth ...
- 关掉PUTTY后,进程仍可以运行。
如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令.该命令可以在你退出帐户之后继续运行相应的进程.no hup就是不挂起的意思( no hang up).该命令 ...