java数据结构----二叉树的实现

java数据结构----二叉树的实现

代码实现:
定义节点:


public class Node {
    private int data;
    private Node leftChild;
    private Node rightChild;

    public void setRightChild(Node rightChild) {
        this.rightChild = rightChild;
    }

    public void setLeftChild(Node leftChild) {
        this.leftChild = leftChild;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getRightChild() {
        return rightChild;
    }

    public Node getLeftChild() {
        return leftChild;
    }

    public int getData() {
        return data;
    }

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

二叉树的实现:


import java.util.LinkedList;

public class Tree {
    public Node getRoot() {
        return root;
    }

    private  Node root;

    public  void insert(int data)
    {
        Node newNode=new Node(data);
        Node current=root;
        Node parent;
        if(root==null)
        {
            root=newNode;
        }else
        {
            while(true)
            {
                parent=current;
                if(current.getData()>data)
                {
                    current=current.getLeftChild();
                    if(current==null)
                    {
                        parent.setLeftChild(newNode);
                        return ;
                    }
                }else
                {
                    current=current.getRightChild();
                    if(current==null)
                    {
                        parent.setRightChild(newNode);
                        return ;
                    }
                }
            }
        }
    }
    public  Node find(int data)
    {
        Node current=root;
        while(current.getData()!=data)
        {
            if(current.getData()>data)
            {
                current=current.getLeftChild();
            }else
            {
                current=current.getRightChild();
            }
            if(current==null)
                return null;
        }
        return current;
    }
    public  Node getSuccessor(Node delNode)
    {
        Node successor=delNode;
        Node successorParent=delNode;
        Node current=delNode.getRightChild();

        while(current!=null)
        {
            successorParent=successor;
            successor=current;
            current=current.getLeftChild();
        }
        if(successor!=delNode.getRightChild())
        {
            successorParent.setLeftChild(successor.getRightChild());
            successor.setRightChild(delNode.getRightChild());
        }
        return successor;
    }
    /**
     * 先序遍历
     */
    public  void frontOrder(Node root)
    {
        if(root!=null)
        {
            System.out.print(root.getData()+" ");
            frontOrder(root.getLeftChild());
            frontOrder(root.getRightChild());
        }
    }
    /**
     * 中序遍历
     */
    public  void middleOrder(Node root)
    {
        if(root!=null)
        {
            middleOrder(root.getLeftChild());
            System.out.print(root.getData()+" ");
            middleOrder(root.getRightChild());
        }
    }
    /**
     * 后序遍历
     */
    public  void afterOrder(Node root)
    {
        if(root!=null)
        {
            afterOrder(root.getLeftChild());
            afterOrder(root.getRightChild());
            System.out.print(root.getData()+" ");
        }
    }
    /**
     * 删除节点
     */
    public boolean delete(int data)
    {
        Node current=root;
        Node parent=root;
        boolean isLeftChild=true;

        while(current.getData()!=data)
        {
            parent=current;

            if(current.getData()>data)
            {
                current=current.getLeftChild();
            }else
            {
                current=current.getRightChild();
                isLeftChild=false;
            }
            if(current==null)
                return false;
        }
        //删除叶子节点
        if(current.getLeftChild()==null&&current.getRightChild()==null)
        {
            if(current==root)
            {
                root=null;
            }
            if(isLeftChild) {
                parent.setLeftChild(null) ;
            }else
            {
                parent.setRightChild(null);
            }
        }else if(current.getRightChild()==null)
        {
            if(current==root)
            {
                root=current.getLeftChild();
            }
            if(isLeftChild)
            {
                parent.setLeftChild(current.getLeftChild());
            }else
            {
                parent.setRightChild(current.getLeftChild());
            }
        }else if(current.getLeftChild()==null)
        {
            if(current==root)
            {
                root=current.getRightChild();
            }
            if(isLeftChild)
            {
                parent.setLeftChild(current.getRightChild());
            }else
            {
                parent.setRightChild(current.getRightChild());
            }
        }else
        {
            Node successor=getSuccessor(current);
            if(current==root)
            {
                root=successor;
            }else if(isLeftChild)
            {
                parent.setLeftChild(successor);
            }else
            {
                parent.setRightChild(successor);
            }
            successor.setLeftChild(current.getLeftChild());
        }
        return true;
    }

    /**
     * 树的层次遍历
     * @param root
     */
    public void LaywerTraversal(Node root){
        if(root==null) return;
        LinkedList<Node> list = new LinkedList<Node>();
        list.add(root);
        Node currentNode;
        while(!list.isEmpty()){
            currentNode=list.poll();
            System.out.println(currentNode.getData());
            if(currentNode.getLeftChild()!=null){
                list.add(currentNode.getLeftChild());
            }
            if(currentNode.getRightChild()!=null){
                list.add(currentNode.getRightChild());
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Serendipity_ry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值