2021-08-12二叉排序树的增删改查

package com.chj.BinaryOrderTree;

import java.util.HashMap;
import java.util.Map;

/**

  • @author chj

  • @create 2021-08-12-10:41
    */
    public class test{
    public static void main(String[] args) {
    int[] arr={7,3,10,12,15,1,9,2};
    BinarySortTree binarySortTree=new BinarySortTree();
    for(int i=0;i<arr.length;i++){
    binarySortTree.add(new Node(arr[i]));
    }
    binarySortTree.infixOrder();
    System.out.println();
    binarySortTree.delete(10);
    binarySortTree.infixOrder();

    }
    }
    class BinarySortTree{
    private Node root;
    public void add(Node node){
    if (rootnull){
    root=node;
    }else {
    root.add(node);
    }
    }
    /中序遍历,得到的排列刚好从小到大排列/
    public void infixOrder(){
    if (root
    null){
    System.out.println(“树为空”);
    }
    else {
    root.infixOrder();
    }
    }
    //找到要删除的节点本身和其父节点
    public Map search(int value){
    Map search=new HashMap();
    if (rootnull){
    System.out.println(“树空”);
    }else {
    if (root.value
    value){
    search.put(root,root);
    }else {
    search = root.search(value);
    }
    }
    return search;
    }
    /删除节点,分四种情况/
    public void delete(int value){
    Map search = search(value);
    Node parent=null;
    Node target=null;
    /1.没找到/
    if (searchnull||search.size()0){
    System.out.println(“没找到”);
    }else{
    for(Object entry:search.entrySet()) {
    Map.Entry entry1=(Map.Entry)entry;
    parent = (Node) entry1.getKey();
    target=(Node)entry1.getValue();
    }
    /2.要删除的节点是叶子结点/
    if (target.left
    null&&target.right
    null){
    if (parent.lefttarget){
    parent.left=null;
    }else {
    parent.right=null;
    }
    /3.要删除的节点是带有左节点和右节点的根节点/
    }else if (target.left!=null&&target.right!=null){
    //右子节点找最小
    int rightMin = getRightMin(target.right);
    target.value=rightMin;
    /4.要删除的节点带有单一的左节点或者右节点/
    } else{
    if (parent.left
    target){
    if (target.left!=null){
    parent.left=target.left;
    }else {
    parent.left=target.right;
    }
    }else {
    if (target.left!=null){
    parent.right=target.left;
    }else {
    parent.right=target.right;
    }
    }
    }
    }
    }
    /得到该节点右子树上的最小权值/
    public int getRightMin(Node node){
    Node temp=node;
    while (temp.left!=null){
    temp=temp.left;
    }
    delete(temp.value);
    return temp.value;
    }
    }
    class Node {
    public int value;
    public Node left;
    public Node right;
    private Map map=new HashMap();
    public Node() {
    }

    public Node(int value) {
    this.value = value;
    }

    public int getValue() {
    return value;
    }

    public void setValue(int value) {
    this.value = value;
    }

    public Node getLeft() {
    return left;
    }

    public void setLeft(Node left) {
    this.left = left;
    }

    public Node getRight() {
    return right;
    }

    public void setRight(Node right) {
    this.right = right;
    }
    /增加节点/
    public void add(Node node){
    if (nodenull){
    return;
    }
    if (this.value>node.value){
    if (this.left
    null){
    this.left=node;
    }else {
    this.left.add(node);
    }
    }else{
    if (this.rightnull){
    this.right=node;
    }else {
    this.right.add(node);
    }
    }
    }
    /查询要删除的目标节点和其父节点,并将其保存进一个map里/
    public Map search(int value){
    Node parent=this;
    if (value<parent.value){
    if (parent.left.value
    value){
    map.put(parent,parent.left);
    return map;
    }
    return parent.left.search(value);
    }else {
    if (parent.right.value==value){
    map.put(parent,parent.right);
    return map;
    }
    return parent.right.search(value);
    }
    }
    /中序遍历/
    public void infixOrder(){
    if (this.left!=null){
    this.left.infixOrder();
    }
    System.out.print(this.value+"\t");
    if (this.right!=null){
    this.right.infixOrder();
    }
    }

    @Override
    public String toString() {
    return “Node{” +
    “value=” + value +
    ‘}’;
    }
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值