1. publicclassTest{
  2.     publicstaticvoid main(String[] args){
  3.         int[] r =newint[]{5,1,3,4,6,7,2,8,9,0};
  4.         BST binarySearchTree =new BST(r);
  5.         binarySearchTree.inOrder();
  6.         System.out.println();
  7.         binarySearchTree.searchBST(6);//查找成功
  8.         System.out.println();
  9.         binarySearchTree.searchBST(10);//查找失败->插入
  10.         System.out.println();
  11.     }
  12.  
  13. }
 
 
  1. publicclass BST {
  2.  
  3.     //二叉树结点
  4.     publicclassBiNode{
  5.         int data;
  6.         BiNode left;
  7.         BiNode right;
  8.  
  9.         BiNode(int data,BiNode left,BiNode right){
  10.             this.data = data;
  11.             this.left = left;
  12.             this.right = right;
  13.         }
  14.     }
  15.  
  16.     privateBiNode root = null;
  17.  
  18.     BST(int[] r)
  19.     {
  20.         for(int i =0; i < r.length; i++)
  21.         {
  22.             BiNode s =newBiNode(r[i],null,null);
  23.             root = insertBST(root, s);
  24. //            insertBST1(s);
  25.         }
  26.     }
  27.  
  28.     //插入(递归)
  29.     publicBiNode insertBST(BiNode head,BiNode s)
  30.     {
  31.         if(head == null){
  32.             head = s;
  33.             return head;
  34.         }
  35.  
  36.         if(s.data < head.data)
  37.             head.left = insertBST(head.left, s);
  38.         else
  39.            head.right = insertBST(head.right,s);
  40.  
  41.         return  head;
  42.     }
  43.  
  44.     //插入(非递归:循环、用临时变量保存过程)
  45.     publicvoid insertBST1(BiNode s)
  46.     {
  47.         if(root == null){
  48.             root = s;
  49.             return;
  50.         }
  51.  
  52.         BiNode temp = root;//需要临时结点记录
  53.  
  54.         while(true)
  55.         {
  56.             if(s.data < temp.data)
  57.             {
  58.                 if(temp.left == null)
  59.                 {
  60.                     temp.left = s;
  61.                     return;
  62.                 }
  63.                 temp = temp.left;
  64.             }
  65.             else
  66.             {
  67.                 if(temp.right == null)
  68.                 {
  69.                     temp.right = s;
  70.                     return;
  71.                 }
  72.                 temp = temp.right;
  73.             }
  74.         }
  75.     }
  76.  
  77.  
  78.     //查找:成功->返回;失败:插入
  79.     publicBiNode searchBST(int a)
  80.     {
  81.         BiNode s1 = searchBST(root,a);
  82.         if(s1 == null){
  83.             BiNode s2 =newBiNode(a,null,null);
  84.             insertBST1(s2);
  85.             System.out.println("search fail ; insert success: "+ s2.data);
  86.             inOrder();
  87.             return s2;
  88.         }else{
  89.             System.out.println("search success: "+ s1.data);
  90.             return s1;
  91.         }
  92.     }
  93.     //查找
  94.     privateBiNode searchBST(BiNode head,int a)
  95.     {
  96.         if(head == null)
  97.             return null;
  98.  
  99.         if(a < head.data)
  100.             return searchBST(head.left, a);
  101.         elseif(a > head.data)
  102.             return searchBST(head.right, a);
  103.         else
  104.             return head;
  105.     }
  106.  
  107. //    删除
  108. //    删除f的孩子p
  109.     publicvoid deleteLeftBST(BiNode f,BiNode p)
  110.     {
  111.         if(p.left == null && p.right == null)    //p为叶子
  112.         {
  113.             f.left = null;
  114.             p = null;
  115.         }
  116.         elseif(p.right == null)                  //p只有左子树
  117.         {
  118.             f.left = p.left;
  119.             p = null;
  120.         }
  121.         elseif(p.left == null)                  //p只有右子树
  122.         {
  123.             f.left = p.right;
  124.             p = null;
  125.         }
  126.         else                                       //p的左右子树均不空
  127.         {
  128.             BiNode par = p, s = par.right;          //用par s 去查找p的右子树的最左下结点
  129.             while(s.left != null)
  130.             {
  131.                 par = s;
  132.                 s = par.left;
  133.             }
  134.             p.data = s.data;                       //交换最左下结点s与p结点数据
  135.  
  136.                                                     //剪枝(删除s结点)
  137.             if(par == p)                          //处理特殊情况
  138.             {
  139.                 par.right = s.right;
  140.                 s = null;
  141.             }
  142.             else                                   //处理一般情况
  143.             {
  144.                 par.left = s.right;
  145.                 s = null;
  146.             }
  147.  
  148.         }
  149.     }
  150.  
  151.     //先序遍历
  152.     publicvoid preOrder(){
  153.         System.out.print("preOrder traversal with recursion:");
  154.         preOrder(root);
  155.         System.out.println();
  156.     }
  157.     //递归
  158.     privatevoid preOrder(BiNode root){
  159.         if(root == null)return;
  160.  
  161.         System.out.print(root.data);       //访问结点
  162.         preOrder(root.left);
  163.         preOrder(root.right);
  164.     }
  165.  
  166.     //中序遍历
  167.     publicvoid inOrder(){
  168.         System.out.print("inOrder traversal with recursion:");
  169.         inOrder(root);
  170.         System.out.println();
  171.     }
  172.     //递归
  173.     privatevoid inOrder(BiNode root){
  174.         if(root == null)//访问结点
  175.             return;
  176.  
  177.         inOrder(root.left);
  178.         System.out.print(root.data);      //访问结点
  179.         inOrder(root.right);
  180.     }
  181.  
  182.     //后序遍历
  183.     publicvoid postOrder(){
  184.         System.out.print("postOrder traversal with recursion:");
  185.         postOrder(root);
  186.         System.out.println();
  187.     }
  188.     //递归
  189.     privatevoid postOrder(BiNode root){
  190.         if(root == null)return;
  191.  
  192.         postOrder(root.left);
  193.         postOrder(root.right);
  194.         System.out.print(root.data);     //访问结点
  195.     }
  196. }
 
输出:
  1. inOrder traversal with recursion:0123456789
  2.  
  3. search success:6
  4.  
  5. search fail ; insert success:10
  6. inOrder traversal with recursion:012345678910
 
 
 

二叉排序树BST代码(JAVA)的更多相关文章

  1. 二叉排序树(BST)创建,删除,查找操作

    binary search tree,中文翻译为二叉搜索树.二叉查找树或者二叉排序树.简称为BST 一:二叉搜索树的定义 他的定义与树的定义是类似的,也是一个递归的定义: 1.要么是一棵空树 2.如果 ...

  2. Atitit.http代理的实现 代码java php c# python

    Atitit.http代理的实现 代码java php c# python 1. 代理服务器用途 代理服务器看成是一种扩展浏览器功能的途径.例如,在把数据发送给浏览器之前,可以用代理服务器压缩数据 调 ...

  3. 二叉排序树(BST)构造与应用

             二叉排序树(BST)构造与应用       本文取自<数据结构与算法>(C语言版)(第三版).出版社是清华大学出版社.       本博文作为学习资料整理. 源码是VC+ ...

  4. 分页 工具类 前后台代码 Java JavaScript (ajax) 实现 讲解

    [博客园cnblogs笔者m-yb原创, 转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708]http ...

  5. Java数据结构和算法(五)二叉排序树(BST)

    Java数据结构和算法(五)二叉排序树(BST) 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 二叉排序树(Binary S ...

  6. stl文件格式解析代码--java版

    代码是参考three.js中的stlLoader.js写的. 需要注意的地方,java中byte取值-128~127 package test_stl.test_entry; import java. ...

  7. n皇后2种解题思路与代码-Java与C++实现

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了n皇后问题的解题思路,并分别用java和c++实现了过程,最后,对于算法改进 ...

  8. 如何写出无法维护的代码(JAVA版)

    程序命名(针对那些不能混淆的代码) 容易输入的名字.比如:Fred,asdf 单字母的变量名.比如:a,b,c, x,y,z,或者干脆上中文比如(阿隆索肯德基) 有创意地拼写错误.比如:SetPint ...

  9. [改善Java代码]Java的泛型是类型擦除的

    泛型可以减少强制类型的转换,可规范集合的元素类型,还可以提高代码的安全性和可读性,正是因为有了这些优点,自从Java引入泛型之后,项目的编码规则上便多了一条,优先使用泛型. Java泛型(Generi ...

随机推荐

  1. webAPP前端必备知识

    了解各浏览器内核 Firefox:-moz-box-shadow Safari:-webkit-box-shadow Opera:-o-box-shadow IE:-ms-box-shadow Web ...

  2. C# 中使用 OpenSSL 的公钥/私钥进行加密和解密

    在C#中进行RSA解密,需要用RSACryptoServiceProvider,但是不支持OpenSSL格式的公钥或者私钥. X509 公钥 -----BEGIN PUBLIC KEY----- MI ...

  3. window.showModalDialog以及window.open用法简介

    .可以通过window.returnValue向打开对话框的窗口返回信息,当然也可以是对象.例如:------------------------------parent.htm<script& ...

  4. elasticsearch常用的插件

    deb 安装/usr/share/elasticsearch/   https://github.com/hangxin1940/elasticsearch-cn-out-of-box 包可以参考   ...

  5. C# Attribute

    Attribute 是C#非常重要的一块内容,需要研究一下. Attribute  的简单使用:简而言之,就是可以自定义通用标志位,而不是在每个所需的类型中分别增加标志位. //class专用attr ...

  6. Redis系列(1)之安装

    Redis系列(1)之安装 由于项目的需要,最近需要研究下Redis.Redis是个很轻量级的NoSql内存数据库,它有多轻量级的呢,用C写的,源码只有3万行,空的数据库只占1M内存.它的功能很丰富, ...

  7. 转:VS2010与SVN

    在VS2010中使用SVN,必须先安装SVN的客户端,再安装VisualSVN(SVN的插件).必须保证两者的版本不冲突,我现在安装的是TortoiseSVN-1.7.10.23359-win32-s ...

  8. Qt Quick分组属性案例

    import QtQuick 2.4 import QtQuick.Controls 1.4 import QtQuick.Controls.Styles 1.4 import QtQuick.Win ...

  9. MFC浅析(4) CObject浅析

    MFC CObject浅析 1.CObject简要声明 2.CRuntimeClass结构 3.RUNTIME_CLASS 4.DYNAMIC支持 5.DYNCREATE支持 6.SERIAL支持 C ...

  10. HDU3549 Flow Problem(网络流增广路算法)

    题目链接. 分析: 网络流增广路算法模板题.http://www.cnblogs.com/tanhehe/p/3234248.html AC代码: #include <iostream> ...