二叉树的基本操作

二叉树节点的创建

public class Node {

    //节点的值
    public int data;
    //左子节点
    public Node leftNode;
    //右子节点
    public Node rightNode;
    /** * @Description TODO(构造函数) */
    public Node(int data) {
        this.data = data;
    }
}

二叉树节点的创建

public Node root;

    /** * @Description 插入操作 * @param value */
    public void insert(int value) {
        Node newNode = new Node(value);
        Node current = root;
        Node parent;
        if (current == null) {
            root = newNode;
        } else {
            while (true) {
                parent = current;
                if(current.data == value){
                    return;
                }
                if (current.data > value) {
                    current = current.leftNode;
                    if (current == null) {
                        parent.leftNode = newNode;
                        return;
                    } 
                }else if (current.data < value) {
                        current = current.rightNode;
                        if (current == null) {
                            parent.rightNode = newNode;
                            return;
                        }
                    }
            }
        }
    }

二叉树节点的查找

//第一种没有简洁性
public void find(int value){
        Node current = root;

            while(true)
            if(current.data == value){
                System.out.println("查找到");
                return;
            }
            else if(current.data > value){
                if(current.leftNode == null){
                    System.out.println("二叉树上没有该值");
                    return;
                }else{
                    current = current.leftNode;
                }
            }
            else if(current.data < value){
                if(current.rightNode == null){
                    System.out.println("二叉树上没有该值");
                    return;
                }else{
                    current = current.rightNode;
                }
            }

        }

//简洁
public Node find(long value) {
        //引用当前节点,从根节点开始
        Node current = root;
        //循环,只要查找值不等于当前节点的数据项
        while(current.data != value) {
            //进行比较,比较查找值和当前节点的大小
            if(current.data > value) {
                current = current.leftChild;
            } else {
                current = current.rightChild;
            }
            //如果查找不到
            if(current == null) {
                return null;
            }
        }
        return current;
    }

二叉树节点的删除

  • 节点没有子节,直接将该节点赋null
  • 节点有一个子节点,将此子节点直接与父节点相连接
  • 节点有左右节点,采用中继后序的方法(将该节点的右节点上的最左边的左子节点的值赋给该节点
public boolean delete(long value) {
            //引用当前节点,从根节点开始
            Node current = root;

            //应用当前节点的父节点
            Node parent = root;
            //是否为左节点
            boolean isleftNode = true;

            while(current.data != value) {
                parent = current;
                //进行比较,比较查找值和当前节点的大小
                if(current.data > value) {
                    current = current.leftNode
                            ;
                    isleftNode = true;
                } else {
                    current = current.rightNode;
                    isleftNode = false;
                }
                //如果查找不到
                if(current == null) {
                    return false;
                }
            }

            //删除叶子节点,也就是该节点没有子节点
            if(current.leftNode == null && current.rightNode == null) {
                if(current == root) {
                    root = null;
                } else if(isleftNode) {
                    parent.leftNode = null;
                } else {
                    parent.rightNode = null;
                }
            } else if(current.rightNode == null) {
                if(current == root) {
                    root = current.leftNode;
                }else if(isleftNode) {
                    parent.leftNode = current.leftNode;
                } else {
                    parent.rightNode = current.leftNode;
                }
            } else {
                Node successor = getSuccessor(current);


                if(current == root) {
                    root = successor;
                } else if(isleftNode) {
                    parent.leftNode = successor;
                } else{
                    parent.rightNode = successor;
                }

                successor.leftNode = current.leftNode;
            }

            return true;


        }
    //用于寻找左子节点 
        public Node getSuccessor(Node delNode) {
            Node successor = delNode;
            Node successorParent = delNode;
            Node current = delNode.rightNode;

            while(current != null) {
                successorParent = successor;
                successor = current;
                current = current.leftNode;
            }

            if(successor != delNode.rightNode) {
                successorParent.leftNode = successor.rightNode;
                successor.rightNode = delNode.rightNode;
            }
            return successor;
        }
点赞