二叉排序树
二叉排序树的删除
package com.tt12.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 i = 0; i < arr.length; i++) {
binarySortTree.add(new Node(arr[i]));
}
System.out.println("原始二叉树");
binarySortTree.infixOreder(); //1 2 3 5 7 9 10 12
//测试删除叶子结点
System.out.println("测试删除叶子结点");
binarySortTree.delNode(2);
binarySortTree.infixOreder(); //删除结点后 1 3 5 7 9 10 12
//测试删除只有一颗子树的结点
System.out.println("测试删除只有一颗子树的结点");
binarySortTree.delNode(1);
binarySortTree.infixOreder();
//测试删除有两颗子树的结点
System.out.println("测试删除有两颗子树的结点");
binarySortTree.delNode(10);
binarySortTree.infixOreder();
}
}
class BinarySortTree {
public 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);
}
}
//编写方法
/**
*
* @param node 传入的结点
* @return 返回以 node 为根结点的二叉排序树的最小结点的值
*/
public int delRightTreeMin(Node node) {
Node target = node;
while (target.left != null) {
target = target.left;
}
delNode(target.value);
return target.value;
}
//删除结点
public void delNode(int value) {
if(root == null) {
return;
}else {
Node targetNode = search(value);
if(targetNode == null) {
return;
}
//上面已经找到了target结点,如果恰好二叉树又只有一个结点那就直接删除
if(root.right == null && root.left == null) {
root = null;
return;
}
//去找target的父结点
Node parent = searchParent(value);
if(targetNode.left == null && targetNode.right == null) {
//如果要删除的是叶子结点
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) {
if(parent.left.value == value) {
parent.left = targetNode.left;
}else {
parent.right = targetNode.left;
}
}else {
//要删除的结点的父结点为空 恰好删除的是根结点 根结点只有左子树
root = targetNode.left;
}
}else {
if(parent != null) {
if(parent.left.value == value) {
parent.left = targetNode.right;
}else {
parent.right = targetNode.right;
}
}else {
root = targetNode.right;
}
}
}
}
}
public void add(Node node) {
if(root == null) {
root = node;
}else {
root.add(node);
}
}
public void infixOreder() {
if(root != null) {
root.infixOrder();
}else {
System.out.println("此二叉排序树为空");
}
}
}
class Node {
int value;
Node left = null;
Node right = null;
public Node(int value) {
this.value = value;
}
public String toString() {
return "Node [value=" + value +"]";
}
//查找要删除的结点
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);
}
}
//查找要删除结点的父结点
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) {
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();
}
}
}
平衡二叉树
引言
基本介绍
应用案例——单旋转(左旋转)
应用案例——双旋转
package com.tt12.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}; //双旋转
//创建一个AVL对象
AVLTree avlTree = new AVLTree();
//添加结点
for(int i = 0; i < arr.length; i++) {
avlTree.add(new Node(arr[i]));
}
//中序遍历
avlTree.infixOreder();
System.out.println("树的高度为 " + avlTree.getRoot().height());
System.out.println("左子树的高度为 " + avlTree.getRoot().leftHeight());
System.out.println("右子树的高度为 " + avlTree.getRoot().rightHeight());
}
}
//创建AVL树
class AVLTree {
public Node root;
public Node getRoot() {
return 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);
}
}
//编写方法
/**
*
* @param node 传入的结点
* @return 返回以 node 为根结点的二叉排序树的最小结点的值
*/
public int delRightTreeMin(Node node) {
Node target = node;
while (target.left != null) {
target = target.left;
}
delNode(target.value);
return target.value;
}
//删除结点
public void delNode(int value) {
if(root == null) {
return;
}else {
Node targetNode = search(value);
if(targetNode == null) {
return;
}
//上面已经找到了target结点,如果恰好二叉树又只有一个结点那就直接删除
if(root.right == null && root.left == null) {
root = null;
return;
}
//去找target的父结点
Node parent = searchParent(value);
if(targetNode.left == null && targetNode.right == null) {
//如果要删除的是叶子结点
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) {
if(parent.left.value == value) {
parent.left = targetNode.left;
}else {
parent.right = targetNode.left;
}
}else {
//要删除的结点的父结点为空 恰好删除的是根结点 根结点只有左子树
root = targetNode.left;
}
}else {
if(parent != null) {
if(parent.left.value == value) {
parent.left = targetNode.right;
}else {
parent.right = targetNode.right;
}
}else {
root = targetNode.right;
}
}
}
}
}
//添加结点
public void add(Node node) {
if(root == null) {
root = node;
}else {
root.add(node);
}
}
//中序遍历
public void infixOreder() {
if(root != null) {
root.infixOrder();
}else {
System.out.println("此二叉排序树为空");
}
}
}
class Node {
int value;
Node left;
Node right;
//构造器
public Node(int value) {
this.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;
}
//左旋转的方法
public void leftRotate() {
//以当前根结点的权 创建新的结点
Node newNode = new Node(value);
//把新的结点的左子树设置成当前结点的左子树
newNode.left = left;
//把新的结点的右子树设置成当前结点的右子树的左子树
newNode.right = right.left;
//把当前结点的权替换成右子结点的权
value = right.value;
//把当前结点的右子树设置成右子树的右子树
right = right.right;
//把当前结点的左子树设置成创建的新结点
left = newNode;
}
//右旋转的方法
public void rightRotate() {
Node newNode = new Node(value);
newNode.right = right.right;
newNode.left = left.right;
value = left.value;
left = left.left;
right= newNode;
}
public String toString() {
return "Node [value=" + value +"]";
}
//查找要删除的结点
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);
}
}
//查找要删除结点的父结点
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) {
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();
}
}
//右旋转
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();
}
}
}
多路查找树
引言——二叉树的问题分析
多叉树
B树的基本介绍
2-3树(最简单的B树结构)
图步骤
其他说明
B树,B+树,B*树