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. jQuery 的ready事件和 JavaScript 的load事件对比

    为了理解2个事件的异同,先了解一下HTML文档加载顺序 HTML DOM文档加载步骤 HTML DOM文档加载是按顺序执行的,这与浏览器的渲染方式有关,一般浏览器渲染操作的顺序大致按如下几个步骤 1, ...

  2. Java中的ExceptionInInitializerError异常及解决方法

    当在静态初始化块中出现了异常的时候,JVM会抛出 java.lang.ExceptionInInitializerError异常.如果你了解Java中的静态变量,你会知道它们是在类加载的时候进行初始化 ...

  3. yii2源码学习笔记

    assets   前端资源文件夹,用于管理css js等前端资源文件等 commands   包含命令行命令,文件为控制器文件 config 应用的配置文件 controllers 控制器文件 mai ...

  4. PHP应用程序的安全性

    无论在开发中,还是在面试时或者技术讨论时,安全性都是需要深入了解及掌握的. 目标 本教程目标是使您了解应该如何保护自己构建的 Web 应用程序.讲解如何防御最常见的安全威胁:SQL 注入.操纵 GET ...

  5. Delphi-UpperCase 函数

    函数名称 UpperCase 所在单元 System.SysUtils 函数原型 function UpperCase(const S: string): string; 函数功能 将字符串中所有的小 ...

  6. 在PyQt4中使用matplotlib

    matplotlib作为Python中著名的数据可视化工具,其官网也提供了在PyQt4中使用的源码,这里举一个应用实例,以备不时之需. 1) 利用Qt Designer创建GUI界面 Demo的GUI ...

  7. WinPcap编程(前言&&学习)

    计算机网络课设要求用WinPcap写对ARP包的接收与发送. 所以学了一下WinPcap的内容. 参考的博客: http://blog.csdn.net/htttw/article/details/7 ...

  8. C#程序中将图片转换为二进制字符串,并将二进制字符串转换为图片

    /// <summary> /// 将图片以二进制流 /// </summary> /// <param name="path"></pa ...

  9. 常用排序算法之——快速排序(C语言+VC6.0平台)

    经典排序算法中快速排序具有较好的效率,但其实现思路相对较难理解. #include<stdio.h> int partition(int num[],int low,int high) / ...

  10. sql server 调优----索引缺失

    SELECT mig.index_group_handle, mid.index_handle, CONVERT (decimal (28,1), migs.avg_total_user_cost * ...