依据给定序列构建排序二叉树,先序、中序、后续遍历

本文介绍如何根据给定序列构建排序二叉树,并详细探讨了先序、中序和后序遍历的过程。
摘要由CSDN通过智能技术生成
package com.kali.structure.binarytree.tree;

/**
 * 依据给定序列arr,构建一棵二叉排序树
 */
public class TestBinary {
    public static void main(String[] args) {
        //给定序列
        int[] arr = {50,66,60,26,21,30,70,68};
        BST bst = new BST();
        bst.createBst(arr);
        System.out.println("\n先序遍历");
        bst.preOrder(bst.root);
        System.out.println("\n中序遍历");
        bst.inOrder(bst.root);
        System.out.println("\n后序遍历");
        bst.postOrder(bst.root);
    }
}

/**
 * 排序二叉树
 */
class BST{
    public Node root;

    /*构建二叉排序树*/
    public void createBst(int[] tree){
        int i = 0;
        while (i < tree.length){
            insert(root,tree[i]);
            i ++;
        }

    }

    /*插入节点*/
    public void insert(Node node,int n){
        Node var = new Node(n);
        if(node == null){
            this.root = var;
            return;
        }
        if(no
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值