二叉树的前,中,后序排序实现

/**
 * 两路单向链表
 */
public class DoubleLNode<T> {
    private T value;
    private DoubleLNode<T> leftNode;
    private DoubleLNode<T> rightNode;

    public DoubleLNode() {
    }

    public DoubleLNode(T value, DoubleLNode<T> leftNode, DoubleLNode<T> rightNode) {
        this.value = value;
        this.leftNode = leftNode;
        this.rightNode = rightNode;
    }

    public T getValue() {
        return value;
    }

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

    public DoubleLNode<T> getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(DoubleLNode<T> leftNode) {
        this.leftNode = leftNode;
    }

    public DoubleLNode<T> getRightNode() {
        return rightNode;
    }

    public void setRightNode(DoubleLNode<T> rightNode) {
        this.rightNode = rightNode;
    }
}

public class BinaryTreeSort {
    //前序排列  先打印自己 再打印左子树 在打印右子树
    public static void preSort(DoubleLNode rootNode){
        //退出递归条件
        if(rootNode == null){
            //没有子节点时候退出递归
            return;
        }
        //推导递归公式
        System.out.print(" ");
        System.out.print(rootNode.getValue());
        preSort(rootNode.getLeftNode());
        preSort(rootNode.getRightNode());
    }

    //中序排序 先打印左子树 再打印本身 再打印右子树
    public static void inSort(DoubleLNode rootNode){
        if(rootNode == null){
            return;
        }
        inSort(rootNode.getLeftNode());
        System.out.print(" ");
        System.out.print(rootNode.getValue());
        inSort(rootNode.getRightNode());
    }

    //后序排序 先打印左子树 再打印右子树 最后打印本身
    public static void postSort(DoubleLNode rootNode){
        if(rootNode == null){
            return;
        }
        postSort(rootNode.getLeftNode());
        postSort(rootNode.getRightNode());
        System.out.print(" ");
        System.out.print(rootNode.getValue());
    }

    //按层排序
    public static void stepSort(DoubleLNode rootNode){
        if(rootNode.getLeftNode()==null || rootNode.getRightNode()==null){
            return;
        }
        System.out.print(" ");
        System.out.print(rootNode.getValue());
        stepSort(rootNode.getLeftNode());
        stepSort(rootNode.getRightNode());
    }

    //通过一个有序数组构建二叉树
    public static void createBinaryTree(DoubleLNode<Integer> node, int[] arr, int p, int r){
        if(p>=r){
            node.setValue(arr[p]);
            return;
        }
        DoubleLNode<Integer> left = new DoubleLNode<Integer>();
        node.setLeftNode(left);
        if((r-p)>=2){
            int q = p + (r-p)/2;
            node.setValue(arr[q]);
            createBinaryTree(left, arr, p, q-1);
            DoubleLNode<Integer> right = new DoubleLNode<Integer>();
            node.setRightNode(right);
            createBinaryTree(right, arr, q+1, r);
        }else{
            //只有两个元素
            node.setValue(arr[r]);
            createBinaryTree(left, arr, p ,r-1);
        }
    }



    public static void main(String[] args) {
        int[] arr = new int[100];
        for(int i=0;i<100;i++){
            arr[i] = i+1;
        }
        DoubleLNode<Integer> root = new DoubleLNode<Integer>();
        createBinaryTree(root, arr, 0, arr.length-1);
        preSort(root);
        System.out.println();
        inSort(root);
        System.out.println();
        postSort(root);
        System.out.println();
        System.out.println("执行完成!");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值