一、基本概念:
二、二叉排序树的创建和遍历:
package binarysorttree;
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {7,3,10,12,5,1,9};
BinarySortTree binarySortTree = new BinarySortTree();
//添加节点到二叉排序树
for (int i = 0; i < arr.length; i++){
binarySortTree.add(new Node(arr[i]));
}
//中序遍历
System.out.println("中序遍历结果:");
binarySortTree.infixOrder();
}
}
//创建二叉排序树
class BinarySortTree{
private Node root;
public void add(Node node){
if (root == null){
root = node; // 如果根节点为空,则直接指向根节点
}else {
root.add(node);
}
}
//中序遍历
public void infixOrder(){
if (root != null){
root.infixOrder();
}else {
System.out.println("the tree is empty! can't list!");
}
}
}
//创建Node节点
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
//添加节点,需要满足二叉排序树的要求(以递归的方式进行)
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);
}
}
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
public void infixOrder(){
if (this.left != null){
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null){
this.right.infixOrder();
}
}
}
三、二叉排序树删除节点:
对于情况三:左子树找最大,对于targetNode而言,他的左子树的任何一个值都不可能 大于它,所以要向左子树找最大,来替换targetNode的值,才能保证一定能满足二叉排序树的要求;同理,也可向targetNode的右子树中找最小。
总结:找待删除节点的右子树中的最小值,或者待删除节点的左子树中的最大值。
删除此节点,然后将此节点的值赋给待删除节点。
方法:查找要删除的节点 及 查找要删除节点的父节点
//查找要删除的节点
/**
*
* @param value 要删除的节点
* @return 找到就返回该节点
*/
public Node search(int value){
if (this.value == value){
return this;
}else if (this.value > value){//向左子树去找
if (this.left == null){
return null;// 没找到,返回null
}
return this.left.search(value);
}else {//向右子树去找
if (this.right == null){
return null; //右边没找到,返回null
}
return this.right.search(value);
}
}
//查找要删除节点的父节点
/**
*
* @param value 要删除节点的值
* @return 返回要删除节点的父节点,没有就返回null
*/
public Node searchParent(int value){
//返回的条件:当前的节点就是要删除节点的父节点>>>>>>则返回
//当前节点的子节点的value == value(子节点存在的情况下)
if ((this.left.value == value && this.left != null) ||
(this.right.value == value && this.right != null)){
return this;
}else {
if (this.left != null && value < this.value){
//向左递归查找
return this.left.searchParent(value);
}else if (value >= this.value && this.right != null){
//向右递归查找
return this.right.searchParent(value);
}else {
return null; //没有找到父节点
}
}
}
删除节点(整体代码):
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 i = 0; i < arr.length; i++){
binarySortTree.add(new Node(arr[i]));
}
//中序遍历
System.out.println("中序遍历结果:");
binarySortTree.infixOrder();
System.out.println("删除后:");
binarySortTree.delNode(2);
binarySortTree.delNode(5);
binarySortTree.delNode(9);
binarySortTree.delNode(12);
binarySortTree.delNode(7);
binarySortTree.delNode(3);
binarySortTree.delNode(10);
binarySortTree.delNode(1);
// binarySortTree.infixOrder();
}
}
//创建二叉排序树
class BinarySortTree{
private Node root;
public void add(Node node){
if (root == null){
root = node; // 如果根节点为空,则直接指向根节点
}else {
root.add(node);
}
}
//中序遍历
public void infixOrder(){
if (root != null){
root.infixOrder();
}else {
System.out.println("the tree is empty! can't list!");
}
}
//查找要删除的节点
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);
}
}
//
/**
* 返回以node为根节点的二叉排序树中的最小节点的值(最小值一定向左找)
* 并将此节点删除(这种情况下,此节点一定为叶子节点,直接调用delNode(value)即可)。
* @param node
* @return
*/
public int delRightTreeMin(Node node){
Node tempNode = node;
//找最小,最小值一定在左叶子结点上
while (tempNode.left != null){
tempNode = tempNode.left;
}//循环结束,tempNode指向的就是最小的节点
delNode(tempNode.value);
return tempNode.value;
}
//删除节点
public void delNode(int value){
if (root == null){
return;
}else {
//找要删除的节点
Node targetNode = search(value);
if (targetNode == null){
return; // 没找到,退出
}
//如果当前二叉排序树只有一个节点,(就是没有父节点的情况)
if (root.left == null && root.right == null){
root = null;
return;
}
//找到targetNode的父节点
Node parentNode = searchParent(value);
//情况一:要删除的节点是叶子结点。要判断是左叶子结点,还是右叶子结点
//说明此时targetNode是叶子结点
if (targetNode.left == null && targetNode.right == null){
if (parentNode.left != null && (parentNode.left.value == value)){
//左叶子结点
parentNode.left = null;
}else if (parentNode.right != null && parentNode.right.value == value){
//右叶子结点
parentNode.right = null;
}
}else if (targetNode.left != null && targetNode.right != null){
//情况三:删除有两颗子树的节点。
int min = delRightTreeMin(targetNode.right);
targetNode.value = min;
}else {//情况二:删除有一颗子树的节点。(排除掉情况一和情况三就是情况二了。)
//需要知道父节点的情况,才能进行删除。如果targetNode之上没有节点了,则parentNode要进行非空判断
if (targetNode.left != null){
if (parentNode != null){
if (parentNode.left.value == value){
parentNode.left = targetNode.left;
}else {
parentNode.right = targetNode.left;
}
}else {
root = targetNode.left; //修改root指向后,删除的逻辑到底是怎么进行的?
}
}else {
if (parentNode != null){
if (parentNode.right.value == value){
parentNode.right = targetNode.right;
}else {
parentNode.left = targetNode.right;
}
}else {
root = targetNode.right;
}
}
}
}
}
}
//创建Node节点
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
//*************************************************************
//以下为删除节二叉排序树中的节点:
//查找要删除的节点
/**
*
* @param value 要删除的节点
* @return 找到就返回该节点
*/
public Node search(int value){
if (this.value == value){
return this;
}else if (this.value > value){//向左子树去找
if (this.left == null){
return null;// 没找到,返回null
}
return this.left.search(value);
}else {//向右子树去找
if (this.right == null){
return null; //右边没找到,返回null
}
return this.right.search(value);
}
}
//查找要删除节点的父节点
/**
*
* @param value 要删除节点的值
* @return 返回要删除节点的父节点,没有就返回null
*/
public Node searchParent(int value){
//返回的条件:当前的节点就是要删除节点的父节点>>>>>>则返回
//当前节点的子节点的value == 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);
}
}
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
public void infixOrder(){
if (this.left != null){
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null){
this.right.infixOrder();
}
}
}