二叉树的存储与遍历java实现

package com.atguigu.binaryTree;

import java.util.Objects;

/**
 * 二叉树的存储与遍历
 */
public class BinaryTree {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9,2};
        BinaryTreeTest tree = new BinaryTreeTest();
        for (int num: arr) {
            tree.add(new Node(num));
        }
        System.out.println("中序遍历的结果为");
        tree.infixOrder();

      /*  int temp = 7;

        Node nodes = tree.searchNode(temp);
        System.out.println("搜索结果为" + nodes);
        Node parent = tree.searchParent(temp);
        System.out.println("搜索节点的父节点为" + parent);*/
//        tree.del(1);
//        tree.del(2);
//        tree.del(5);
//        tree.del(3);
        tree.del(2);
        tree.del(5);
        tree.del(9);
        tree.del(12);
        tree.del(7);
        tree.del(3);
//        tree.del(10);
//        tree.del(1);
        System.out.println("删除后的中序遍历结果为");
        tree.infixOrder();

    }
}

/**
 * 二叉树的创建
 */
class BinaryTreeTest{
    public Node root;

    public BinaryTreeTest() {
    }

    public BinaryTreeTest(Node root) {
        this.root = root;
    }
    //添加节点
    public void add(Node node){
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }
    //删除节点
    public void del(int values){
        //查看待删除的节点是否存在
        if (searchNode(values) == null){
            System.out.println("待删除的节点不存在");
        }else {
            Node delNode = searchNode(values);//寻找到待删除的节点
            Node parent = searchParent(values);
            boolean flag1 = (delNode.left != null);
            boolean flag2 = (delNode.right != null);
            boolean flagpar = !(parent == null);
            Node temp;
            if (!flag1 && !flag2){//如果待删除的节点是叶子结点,没有子节点
               if (parent == null){//待删除的是根节点
                   root =null;
               }else {//有父节点
                   if (parent.left != null && parent.left.equals(delNode)){
                       parent.left = null;
                   }else if (parent.right != null && parent.right.values == values){
                       parent.right = null;
                   }
               }
            }else if ( (flag1 && !flag2 )//如果删除的节点有一个子节点
                    ||(!flag1 && flag2)){//只有一个子节点
                if (flag1 && !flagpar){//没有父节点且左边有一个子节点,即根节点
                    root = delNode.left;
                    /*delNode.left = null;
                    delNode = temp;*/
                }else if (flag1 && flagpar){//有父节点且左边有一个子节点
                    if (parent.left != null && parent.left.values == values){//目标节点是父节点的左子节点
                        parent.left = delNode.left;
                    }else if (parent.right != null && parent.right.values == values){//目标节点是父节点的左子节点
                        parent.right = delNode.left;
                    }
                }else if (flag2 && !flagpar){//没有父节点且右边有一个子节点,即根节点
                    root = delNode.right;
                    /*delNode.right = null;
                    delNode = temp;*/
                }else if (flag2 && flagpar){//有父节点且右边有一个子节点
                    if (parent.left != null && parent.left.values == values){//目标节点是父节点的左子节点
                        parent.left = delNode.right;
                    }else if (parent.right != null && parent.right.values == values){//目标节点是父节点的左子节点
                        parent.right = delNode.right;
                    }
                }
            }else if (flag1 && flag2){//如果待删除的节点有两个子节点
               /* temp = delNode.right.fingMin();//法一:从右边寻找最小的
                del(delNode.right.fingMin().values);
                delNode.values = temp.values;*/
               //法二:从左边找最大的
                temp = delNode.left.fingMax();
                del(delNode.left.fingMax().values);
                delNode.values = temp.values;
            }
        }

    }
    //查找等待删除的节点是否存在
    public Node searchNode(int values){
       if (root.values == values){
           return root;
       }else{
           return root.search(values);
       }
    }
    //查找待删除的节点的父节点
    public Node searchParent(int values){
        if (root.values == values){
            return null;
        }else {
            return root.searchParents(values);
        }
    }
    //中序遍历
    public void infixOrder(){
        if (root == null){
            System.out.println("空树");
        }else{
            root.infixOrder();
        }
    }
}
/**
 * 节点
 */
class Node{
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Node node = (Node) o;
        return values == node.values;
    }

    //寻找一个节点下面的最小的节点
    public Node  fingMin(){
        if (this.left == null ){
            return this;
        }else {
            return this.left.fingMin();
        }
    }
    //寻找一个节点下面的最大的节点
    public Node fingMax(){
        if (this.right == null ){
            return this;
        }else {
            return this.right.fingMax();
        }
    }
    @Override
    public int hashCode() {

        return Objects.hash(values);
    }

    public int values;//节点对应的值
    Node left;//左子节点
    Node right;//右子节点

    //查找待删除的节点是否存在
    public Node search(int values){
        if (this.values == values){
            return this;
        }else if (this.left != null && this.values > values){
            return this.left.search(values);
        }else if (this.right != null && this.values <= values){
            return this.right.search(values);
        }else {
            return null;
        }
    }
    //查找待删除节点的父节点
    public Node searchParents(int values){
        if (this.values == values){
            return null;
        }
        if ((this.left != null && this.left.values == values) ||
               this.right != null && this.right.values == values ){
            return this;
        }else if (this.left != null && this.values > values){
            return this.left.searchParents(values);
        }else if (this.right != null && this.values <= values){
            return this.right.searchParents(values);
        }else {
            return null;
        }
    }
    public Node(Node left) {
        this.left = left;
    }

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

    //中序遍历
    public void infixOrder(){
        if (this.left != null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null){
            this.right.infixOrder();
        }
    }
    //添加节点的方法
    public void add(Node node){
        if (node == null){
            return;
        }else{
            if (node.values < this.values){
                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{" +
                "values=" + values +
                '}';
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值