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
依据给定序列构建排序二叉树,先序、中序、后续遍历
最新推荐文章于 2023-09-13 13:54:49 发布
本文介绍如何根据给定序列构建排序二叉树,并详细探讨了先序、中序和后序遍历的过程。
摘要由CSDN通过智能技术生成