二叉排序树的创建,遍历,删除

package day21;

import com.sun.xml.internal.bind.v2.util.StackRecorder;
import org.omg.CORBA.PUBLIC_MEMBER;

public class 二叉排序树 {
//   左节点小于根节点,右节点大于跟节点
public static void main(String[] args) {
    Binarysortree binarysortree = new Binarysortree();
    int[] arr ={ 7,3,10,12,5,1,9,2};
for (int i=0;i<arr.length;i++){
    Node node = new Node(arr[i]);
    binarysortree.add(node);
//    binarysortree.delnode(1);
    binarysortree.delnode(10);

}
    System.out.println("中序遍历二叉排序树 ");
binarysortree.indixorder();
}
}
//创建二叉排序树
//--------------------------------------------------tree---------------------------------------------
class Binarysortree{
    private Node root;
public void add(Node node){
    if (root ==null){
        root=node;
    }else {
        root.add(node);
    }
}
public void indixorder(){
    if (root!=null){
        root.indixorder();
    }else {
        System.out.println("二叉排序树为空");
    }
}
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为根节点的二叉排序树的最小节点

    /**
     *
     *
     * @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 {
//      先找到要删除的节点
        Node searchnode = search(value);
//      如果没有找到要删除的节点
        if (searchnode==null){
            return;
        }
        //如果只有一个root节点
        if (root.left==null&&root.right==null){
            root =null;
            return;
        }
//        去找到serachnode的父节点
        Node parent = searchParent(value);
        //如果要删除的节点是叶子节点
        if (searchnode.left==null&&searchnode.right==null){
//            判断searchnode是左还是右
            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 (searchnode.left!=null&&searchnode.right!=null){
            //要删除的节点是两颗字数的节点
            int minvalue = delrighttreemin(searchnode.right);
            searchnode.value=minvalue;

        }else { //删除只有一颗子树的节点
//            删除的节点有左子节点
            if (searchnode.left != null) {
//                如果删除的节点处于左节点
                if (parent.left.value == value) {
                    parent.left = searchnode.left;
                } else {
                    //删除的节点处于右节点
                    parent.right = searchnode.left;
                }
            } else {//要删除的节点有右子节点
//            如果删除的节点处于父节点的左
                if (parent.left.value == value) {
                    parent.left =searchnode.right;
                }else { //删除的节点处于父节点的右
                    parent.right=searchnode.right;
                }

            }
        }

    }

    }

}

//-----------------------------------------------------node-------------------------------------------

class Node{
    int value;
    Node left;
    Node right;

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    public Node(int value) {
        this.value = value;
    }
//    添加节点
    public void add(Node node){
//        递归添加,需要满足二叉排序树
        if (node==null){
            return;
        }
//       判断传入的节点的值和当前二叉树跟节点比较
        if (node.value<this.value){ //this为根节点
            if (this.left==null){
                this.left=node;
            }else {
                this.left.add(node);//递归的向左子树添加
            }

        }else {//node>value
            if (node.value>this.value){
                if (this.right==null){
                    this.right=node;
                }else {
                    this.right.add(node);//递归添加
                }
            }
        }

    }

//    中序遍历
    public void indixorder(){  //左中右
        if (this.left!=null){
            this.left.indixorder();
        }
        System.out.println(this);
        if (this.right!=null){
            this.right.indixorder();
        }
    }



    //查找要删除的节点
    public Node search(int value){
//     因为在node类中的方法,不需要判断空
        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 (this.value<value &&this.right!=null){
                return this.right.searchparent(value);
            }else if (this.value>=value&&this.left!=null){
            return this.left.searchparent(value);
        }else {
            return null; //没有找到
        }


    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉排序树是一种特殊的二叉树,它的左子树上的所有节点的值都小于根节点的值,右子树上的所有节点的值都大于根节点的值。在二叉排序树上实现查询、插入和删除算法的具体步骤如下: 1. 查询算法:从根节点开始,如果要查找的值等于当前节点的值,则返回该节点;如果要查找的值小于当前节点的值,则在左子树中继续查找;如果要查找的值大于当前节点的值,则在右子树中继续查找。如果查找到叶子节点仍未找到,则说明该值不存在于二叉排序树中。 2. 插入算法:从根节点开始,如果要插入的值等于当前节点的值,则不进行任何操作;如果要插入的值小于当前节点的值,则在左子树中继续插入;如果要插入的值大于当前节点的值,则在右子树中继续插入。如果要插入的节点为空,则将该节点插入到当前位置。 3. 删除算法:从根节点开始,如果要删除的值等于当前节点的值,则分三种情况进行处理:如果该节点没有左右子树,则直接删除该节点;如果该节点只有左子树或右子树,则将该子树替换该节点;如果该节点既有左子树又有右子树,则将该节点的左子树中的最大节点或右子树中的最小节点替换该节点。如果要删除的值小于当前节点的值,则在左子树中继续删除;如果要删除的值大于当前节点的值,则在右子树中继续删除。 4. 中序遍历算法:从根节点开始,先遍历左子树,然后访问当前节点,最后遍历右子树。这样遍历的结果就是二叉排序树中所有节点的值按照从小到大的顺序排列。 下面是C语言实现二叉排序树的代码,包括查询、插入、删除和中序遍历算法: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉排序树节点结构体 typedef struct BSTNode { int data; struct BSTNode *left; struct BSTNode *right; } BSTNode; // 查询算法 BSTNode *search(BSTNode *root, int value) { if (root == NULL || root->data == value) { return root; } else if (value < root->data) { return search(root->left, value); } else { return search(root->right, value); } } // 插入算法 BSTNode *insert(BSTNode *root, int value) { if (root == NULL) { BSTNode *newNode = (BSTNode *)malloc(sizeof(BSTNode)); newNode->data = value; newNode->left = NULL; newNode->right = NULL; return newNode; } else if (value < root->data) { root->left = insert(root->left, value); } else if (value > root->data) { root->right = insert(root->right, value); } return root; } // 删除算法 BSTNode *delete(BSTNode *root, int value) { if (root == NULL) { return root; } else if (value < root->data) { root->left = delete(root->left, value); } else if (value > root->data) { root->right = delete(root->right, value); } else { if (root->left == NULL) { BSTNode *temp = root->right; free(root); return temp; } else if (root->right == NULL) { BSTNode *temp = root->left; free(root); return temp; } BSTNode *temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->data = temp->data; root->right = delete(root->right, temp->data); } return root; } // 中序遍历算法 void inorder(BSTNode *root) { if (root != NULL) { inorder(root->left); printf("%d ", root->data); inorder(root->right); } } int main() { BSTNode *root = NULL; root = insert(root, 5); root = insert(root, 3); root = insert(root, 7); root = insert(root, 1); root = insert(root, 9); printf("中序遍历结果:"); inorder(root); printf("\n"); root = delete(root, 3); printf("删除节点3后的中序遍历结果:"); inorder(root); printf("\n"); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值