先看一个需求
给你一个数列(7,3,10,12,5,1,9),要求能够高效的完成对数据的查询和添加
使用数组
数组未排序,优点:直接在数组尾添加,速度快。缺点:查找速度慢.[示意图]
数组排序,优点:可以使用二分查找,查找速度快,缺点:为了保证数组有序,在添加新数据时,找到插入位置后,后面的数据需整体移动,速度慢。[示意图]
使用链式存储-链表
不管链表是否有序,查找速度都慢,添加数据速度比数组快,不需要数据整体移动。[示意图]
使用二叉排序树
二叉排序树介绍
二叉排序树:BST:(Binary Sort(Search)Tree),对于二叉排序树的任何一个非叶子节点,
要求:左字节的的值比当前节点的值小,右字节的的值比当前节点的值大.
特别说明:如果有相同的值,可以将该节点放在左子节点或右子节点
比如针对前面的数据(7,3,10,12,5,1,9),对于的二叉排序树为:
二叉排序树的创建和遍历
一个数组创建成对应的二叉排序树,并使用中序遍历二叉排序树
二叉排序树的删除
二叉排序树的删除情况比较复杂,有下面三种情况需要考虑
(1)删除叶子节点(比如:2,5,9,12)
(2)删除只有一颗子树的节点(比如:1)
(3)删除有两颗子树的节点.(比如:7,3,10)
操作思路分析
第一种情况: parent.left = targetNode.right;
|
二叉排序树删除节点代码实现:
package binarySortTree;
/**
* 二 叉排序树
*/
public class BinarySortTreeDemo {
public static void main(String[] args) {
int [] arr ={7,3,10,12,5,1,9,2};
BinarySortTree binarySortTree = new BinarySortTree();
//循环的添加结点到二 叉排序树
for (int j : arr) {
binarySortTree.add(new Node(j));
}
//中序遍历二 叉排序树
System.out.println("中序遍历二叉排序树");
binarySortTree.infixOrder();
//测试一下删除叶子节点
binarySortTree.delNode(2);
binarySortTree.delNode(5);
binarySortTree.delNode(9);
binarySortTree.delNode(12);
binarySortTree.delNode(7);
binarySortTree.delNode(3);
binarySortTree.delNode(10);
// binarySortTree.delNode(1);
System.out.println("删除后");
binarySortTree.infixOrder();
}
}
//创建二 叉排序树
class BinarySortTree{
private Node root;
//查找要删除的节点
public Node search(int value){
if (root == null){
return null;
}else {
return root.search(value);
}
}
//查找父节点
public Node searchParent(int value){
if (root == null){
return null;
}else {
return root.searchParent(value);
}
}
/**
* 1.返回以node为根节点的二叉排序树的最小节点的值
* 2.删除node为根节点的二叉排序树的最小节点
* @param node 传入的节点(当前二叉排序树的根节点)
* @return 返回的以node 为根节点的二叉排序树的最小节点的值
*/
public int delRightTreeMin(Node node){
Node target = node;
//循环的查找左节点,找到最小值
while (target.left != null){
target = target.left;
}
//这时target就指向了最小节点
//删除最小节点
delNode(target.value);
return target.value;
}
//删除节点
public void delNode(int value){
if (root == null){
return;
}else {
//1.需求先去找要删除的节点 targetNode
Node targetNode = search(value);
//如果没有找到要删除的节点
if (targetNode == null){
return;
}
//如果我们发现当前这棵二 叉排序树只有一个节点
if (root.left == null && root.right == null){
root = null;
return;
}
//去找到targetNode的父节点
Node parent = searchParent(value);
//如果要删除的节点是叶子节点
if (targetNode.left == null && targetNode.right == null){
//判断targetNode 是父节点的左子节点,还是右子节点
if (parent.left != null && parent.left.value == value){ //是左子节点
parent.left = null;
}else if (parent.right != null && parent.right.value == value){ //是右子节点
parent.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) { //删除有两颗子树的节点
int minVal = delRightTreeMin(targetNode.right);
targetNode.value = minVal;
}else { //删除只有一颗子树的节点
//如果要删除的节点有左子节点
if (targetNode.left != null){
if (parent != null){
//如果targetNode 是parent的左子节点
if (parent.left.value == value) {
parent.left = targetNode.left;
}
}else {
//targetNode 是parent的右子节点
root = targetNode.left;
}
}else { //如果要删除的节点有右子节点
//如果targetNode 是parent的左子节点
if (parent != null) {
if (parent.left.value == value) {
parent.left = targetNode.right;
} else {//如果targetNode 是parent的右子节点
root = targetNode.right;
}
}
}
}
}
}
//添加结点的方法
public void add(Node node){
if (root == null){
root = node; //如果root为空则直接让root指向node
}else {
root.add(node);
}
}
//中序遍历
public void infixOrder(){
if(root != null){
root.infixOrder();
}else {
System.out.println("二叉排序树为空,无法遍历");
}
}
}
//创建Node结点
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
/**
* 查找要删除的节点
* @param value 希望删除的节点的值
* @return 如果找到返回该节点,否则返回null
*/
public Node search(int value){
if (value == this.value) {//找到介绍该节点
return this;
}else if (value < this.value){ //如果查找到的值小于当前节点,向左子树递归查找
//如果左子节点为空
if (this.left == null){
return null;
}
return this.left.search(value);
}else { //如果查找的值不小于当前节点,向右子树递归查找
if (this.right == null) {
return null;
}
return this.right.search(value);
}
}
/**
* 查找要删除节点的父节点
* @param value 要找到的节点的值
* @return 返回的是要删除节点的父节点,如果没有就返回null
*/
public Node searchParent(int value){
//如果当前节点就是要删除的节点的父节点,就返回
if ((this.left != null && this.left.value == value) ||
(this.right != null && this.right.value == value)){
return this;
}else{
//如果查找的值小于当前节点的值,并且当前节点的左子节点不为空
if (value < this.value && this.left != null){
return this.left.searchParent(value);//向左子树递归查询
}else if (value >= this.value && this.right != null){
return this.right.searchParent(value);//向右子树递归查找
}else {
return null;//没有找到父节点
}
}
}
//添加结点的方法
//递归的形式添加结点,注意需要满足二 叉排序树的要求
public void add(Node node){
if (node == null){
return;
}
//排队传入结点的值,和当前子树的根结点的值关系
if (node.value < this.value) {
//如果当前结点左子结点为null
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);
}
}
}
/**
* 中序遍历
*/
public void infixOrder() {
//递归向左子树中序遍历
if (this.left != null) {
this.left.infixOrder();
}
//输出父节点
System.out.println(this);
//递归向右子树中序遍历
if (this.right != null) {
this.right.infixOrder();
}
}
}
平衡二叉树(AVL树)
看一个案例(说明二叉排序树可能的问题)
基本介绍
(1)平衡二叉树也叫平衡二叉搜索树(Self-balancing binary search tree)又被称为AVL树,可以保证查询效率较高。
(2)具有以下特点:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。平衡二叉树的常用实现方法有红黑树、AVL、替罪羊树、Treap、伸展树等。
(3)举例说明,看看下面哪些AVL树,为什么?
应用案例-单旋转(左旋转)
(1)要求:给你一个数列,创建出对应的平衡二叉树,数列{4,3,5,5,7,8}
(2)思路分析
代码实现
//左旋转方法
private void leftRotate(){
//创建新的节点,以当前根节点的值
Node newNode = new Node(value);
//把新的结点的左子树设置成当前结点的左子树
newNode.left = left;
//把新的结点的右子树设置成带你过去结点的右子树的左子树
newNode .right = right.left;
//把当前结点的值替换成右子结点的值
value = right.value;
//把当前结点的右子树设置成当前结点右子树的右子树
right = right.right;
//把当前结点的左子树(左子结点)设置成新的结点
left = newNode;
}
应用案例-单旋转(右旋转)
要求:给你一个数列,创建出对应的平衡二叉树,数列{10,12,8,9,7,6}
思路分析
代码实现
//右旋转方法
private void rightRotate(){
Node newNode = new Node(value);
newNode.right = right;
newNode .left = left.right;
value = left.value;
left = left.left;
right = newNode;
}
应用案例-双旋转
前面的两个数列,进行单旋转(即一次旋转)就可以将非平衡二叉树转成平衡二叉树,但是在某些情况下,单旋转不能完成平衡二叉树的转换。比如数列
int[]arr ={ 10,11,7,6,8,9};运行原来的代码可以看到,并没有转成AVL树.int[]arr ={2,1,6,5,7,3;//运行原来的代码可以看到,并没有转成AVL树
问题分析
代码实现 (AVL树完整代码)
package avl;
public class AVLTreeDemo {
public static void main(String[] args) {
//int[] arr = {4,3,6,5,7,8};
//int[] arr = {10,12,8,9,7,6};
int[] arr = {10,11,7,6,8,9};
//创建一个AVLTree对象
AVLTree avlTree = new AVLTree();
//添加节点
for (int i=0; i < arr.length; i++){
avlTree.add(new Node(arr[i]));
}
//遍历
System.out.println("中序遍历");
avlTree.infixOrder();
System.out.println("在平衡处理后~~");
System.out.println("树的高度="+avlTree.getRoot().height());
System.out.println("树的左子树高度="+avlTree.getRoot().leftHeight());
System.out.println("树的右子树高度="+avlTree.getRoot().rightHeight());
System.out.println("当前的根结点"+avlTree.getRoot());
System.out.println("根结点的左子结点"+avlTree.getRoot().left);
}
}
//创建AVLTree
class AVLTree{
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
//查找要删除的节点
public Node search(int value){
if (root == null){
return null;
}else {
return root.search(value);
}
}
//查找父节点
public Node searchParent(int value){
if (root == null){
return null;
}else {
return root.searchParent(value);
}
}
/**
* 1.返回以node为根节点的二叉排序树的最小节点的值
* 2.删除node为根节点的二叉排序树的最小节点
* @param node 传入的节点(当前二 叉排序树的根节点)
* @return 返回的以node 为根节点的二 叉排序树的最小节点的值
*/
public int delRightTreeMin(Node node){
Node target = node;
//循环的查找左节点,找到最小值
while (target.left != null){
target = target.left;
}
//这时target就指向了最小节点
//删除最小节点
delNode(target.value);
return target.value;
}
//删除节点
public void delNode(int value){
if (root == null){
return;
}else {
//1.需求先去找要删除的节点 targetNode
Node targetNode = search(value);
//如果没有找到要删除的节点
if (targetNode == null){
return;
}
//如果我们发现当前这棵二 叉排序树只有一个节点
if (root.left == null && root.right == null){
root = null;
return;
}
//去找到targetNode的父节点
Node parent = searchParent(value);
//如果要删除的节点是叶子节点
if (targetNode.left == null && targetNode.right == null){
//判断targetNode 是父节点的左子节点,还是右子节点
if (parent.left != null && parent.left.value == value){ //是左子节点
parent.left = null;
}else if (parent.right != null && parent.right.value == value){ //是右子节点
parent.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) { //删除有两颗子树的节点
int minVal = delRightTreeMin(targetNode.right);
targetNode.value = minVal;
}else { //删除只有一颗子树的节点
//如果要删除的节点有左子节点
if (targetNode.left != null){
if (parent != null){
//如果targetNode 是parent的左子节点
if (parent.left.value == value) {
parent.left = targetNode.left;
}
}else {
//targetNode 是parent的右子节点
root = targetNode.left;
}
}else { //如果要删除的节点有右子节点
//如果targetNode 是parent的左子节点
if (parent != null) {
if (parent.left.value == value) {
parent.left = targetNode.right;
} else {//如果targetNode 是parent的右子节点
root = targetNode.right;
}
}
}
}
}
}
//添加结点的方法
public void add(Node node){
if (root == null){
root = node; //如果root为空则直接让root指向node
}else {
root.add(node);
}
}
//中序遍历
public void infixOrder(){
if(root != null){
root.infixOrder();
}else {
System.out.println("二叉排序树为空,无法遍历");
}
}
}
//创建Node结点
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
//返回左子树的高度
public int leftHeight(){
if (left == null){
return 0;
}
return left.height();
}
//返回右子树的高度
public int rightHeight(){
if (right == null){
return 0;
}
return right.height();
}
//返回以该节点为根节点树的高度
public int height(){
return Math.max(left == null ? 0 :
left.height(), right == null ? 0 :
right.height()) + 1;
}
//左旋转方法
private void leftRotate(){
//创建新的节点,以当前根节点的值
Node newNode = new Node(value);
//把新的结点的左子树设置成当前结点的左子树
newNode.left = left;
//把新的结点的右子树设置成带你过去结点的右子树的左子树
newNode .right = right.left;
//把当前结点的值替换成右子结点的值
value = right.value;
//把当前结点的右子树设置成当前结点右子树的右子树
right = right.right;
//把当前结点的左子树(左子结点)设置成新的结点
left = newNode;
}
//右旋转方法
private void rightRotate(){
Node newNode = new Node(value);
newNode.right = right;
newNode .left = left.right;
value = left.value;
left = left.left;
right = newNode;
}
/**
* 查找要删除的节点
* @param value 希望删除的节点的值
* @return 如果找到返回该节点,否则返回null
*/
public Node search(int value){
if (value == this.value) {//找到介绍该节点
return this;
}else if (value < this.value){ //如果查找到的值小于当前节点,向左子树递归查找
//如果左子节点为空
if (this.left == null){
return null;
}
return this.left.search(value);
}else { //如果查找的值不小于当前节点,向右子树递归查找
if (this.right == null) {
return null;
}
return this.right.search(value);
}
}
/**
* 查找要删除节点的父节点
* @param value 要找到的节点的值
* @return 返回的是要删除节点的父节点,如果没有就返回null
*/
public Node searchParent(int value){
//如果当前节点就是要删除的节点的父节点,就返回
if ((this.left != null && this.left.value == value) ||
(this.right != null && this.right.value == value)){
return this;
}else{
//如果查找的值小于当前节点的值,并且当前节点的左子节点不为空
if (value < this.value && this.left != null){
return this.left.searchParent(value);//向左子树递归查询
}else if (value >= this.value && this.right != null){
return this.right.searchParent(value);//向右子树递归查找
}else {
return null;//没有找到父节点
}
}
}
//添加结点的方法
//递归的形式添加结点,注意需要满足二 叉排序树的要求
public void add(Node node) {
if (node == null) {
return;
}
//排队传入结点的值,和当前子树的根结点的值关系
if (node.value < this.value) {
//如果当前结点左子结点为null
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) {
//如果它的右子树的左子树的高度大于它右子树的高度
if (right != null && right.leftHeight() > right.rightHeight()) {
//先对右子结点进行右旋转
right.rightRotate();
//再对当前结点进行左旋转
leftRotate();
} else {
leftRotate(); //左旋转
}
//当添加完一个结点后,如果(左子树的高度-右子树的高度)>1,右旋转
}else if (leftHeight() - rightHeight() > 1) {
//如果它的左子树的右子树高度大于它的左子树的高度
if (left != null && left.rightHeight() > left.leftHeight()) {
//先对当前结点的左结点(左子树)->左旋转
left.leftRotate();
//再对当前结点进行右旋转
rightRotate();
}else {
//直接进行右旋转即可
rightRotate();
}
}
}
/**
* 中序遍历
*/
public void infixOrder() {
//递归向左子树中序遍历
if (this.left != null) {
this.left.infixOrder();
}
//输出父节点
System.out.println(this);
//递归向右子树中序遍历
if (this.right != null) {
this.right.infixOrder();
}
}
}