博客
关于我
二叉排序树的创建和遍历
阅读量:303 次
发布时间:2019-03-03

本文共 1756 字,大约阅读时间需要 5 分钟。

二叉排序树的实现与应用

节点类定义

在二叉树的实现中,首先定义了一个节点类 Node,每个节点包含一个值和两个子节点。节点类的实现如下:

class Node {    int value;    Node left;    Node right;    public Node(int value) {        super();        this.value = value;    }    @Override    public String toString() {        return "Node [value=" + value + "]";    }}

节点插入方法

二叉排序树的核心是通过递归方法插入节点,并且满足二叉搜索树的性质。在插入节点时,需要判断当前节点的值与目标节点的关系,从而决定将其插入到左子树还是右子树。

public void add(Node node) {    if (node == null) {        return;    }    // 判断当前节点的值与目标节点的关系    if (node.value < this.value) {        if (this.left == null) {            this.left = node;        } else {            this.left.add(node);        }    } else {        if (this.right == null) {            this.right = node;        } else {            this.right.add(node);        }    }}

中序遍历

中序遍历是遍历二叉树的常用方式,可以得到节点的有序访问序列。在递归实现中,先访问左子树,然后访问当前节点,最后访问右子树。

public void zhongxu() {    if (this.left != null) {        this.left.zhongxu();    }    System.out.println(this);    if (this.right != null) {        this.right.zhongxu();    }}

二叉排序树实现

二叉排序树的实现主要包括节点插入和中序遍历两个功能。整个二叉排序树由一个根节点组成,插入节点时需要按照二叉搜索树的规则进行。

class BinarySortTree2 {    private Node root;    public void add(Node node) {        if (root == null) {            root = node;        } else {            root.add(node);        }    }    // 中序遍历    public void zhongxu() {        if (root != null) {            root.zhongxu();        } else {            System.out.println("空树");        }    }}

测试与应用

通过以下测试代码可以验证二叉排序树的实现是否正确:

int[] arr = {7, 3, 10, 12, 5, 1, 9};BinarySortTree2 binarySortTree2 = new BinarySortTree2();for (int i = 0; i < arr.length; i++) {    binarySortTree2.add(new Node(arr[i]));}binarySortTree2.zhongxu();

总结

通过上述实现,可以清晰地看到二叉排序树的核心逻辑及其应用。插入节点的方法通过递归确保了二叉搜索树的性质,而中序遍历则可以按照有序方式访问节点。这种结构在数据处理、搜索等场景中具有广泛应用。

转载地址:http://uihl.baihongyu.com/

你可能感兴趣的文章
Static--用法介绍
查看>>
ssm旅游信息管理系统的设计与实现bus56(程序+开题)
查看>>
order by rand()
查看>>
SSM(Spring+SpringMvc+Mybatis)整合开发笔记
查看>>
ViewHolder的改进写法
查看>>
Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
查看>>
org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
查看>>
sql查询中 查询字段数据类型 int 与 String 出现问题
查看>>
org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
查看>>
org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
查看>>
sqlserver学习笔记(三)—— 为数据库添加新的用户
查看>>
org.apache.http.conn.HttpHostConnectException: Connection to refused
查看>>
org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
查看>>