[Java] java 二叉查找树(增删改查操作) →→→→→进入此内容的聊天室

来自 , 2019-08-25, 写在 Java, 查看 121 次.
URL http://www.code666.cn/view/38913e1d
  1. /**
  2.  * java 二叉查找树(增删改查操作)
  3.  */
  4. public class Main
  5. {
  6.         public static void main ( String[] args )
  7.         {
  8.                 BinarySearchTree btr = new BinarySearchTree();
  9.                 btr.insert ( 6 );
  10.                 btr.insert ( 2 );
  11.                 btr.insert ( 1 );
  12.                 btr.insert ( 3 );
  13.                 btr.insert ( 4 );
  14.                 btr.insert ( 8 );
  15.                 System.out.println ( btr.find ( 10 ) );
  16.                 System.out.println ( btr.findMin() );
  17.                 System.out.println ( btr.findMax() );
  18.         }
  19. }
  20.  
  21. // 定义树节点
  22. class BinaryNode
  23. {
  24.         Comparable element; // 保存节点内容
  25.         BinaryNode left; // 保存节点的左孩子
  26.         BinaryNode right; // 保存节点的右孩子
  27.  
  28.         // 定义构造函数,初始化成员
  29.         BinaryNode ( Comparable theElement )
  30.         {
  31.                 this ( theElement, null, null );
  32.         }
  33.  
  34.         BinaryNode ( Comparable theElement, BinaryNode lt, BinaryNode rt )
  35.         {
  36.                 element = theElement;
  37.                 left = lt;
  38.                 right = rt;
  39.         }
  40. }
  41.  
  42. // 定义二叉查找树,将树节点封装成树并进行各种操作
  43. class BinarySearchTree
  44. {
  45.         private BinaryNode root;
  46.  
  47.         public BinarySearchTree()
  48.         {
  49.                 root = null;
  50.         }
  51.  
  52.         // 判断树是否为空
  53.         public boolean isEmpty()
  54.         {
  55.                 return root == null;
  56.         }
  57.  
  58.         // 查找树中是否存在某节点
  59.         public Comparable find ( Comparable x )
  60.         {
  61.                 return find2 ( x, root ).element;
  62.         }
  63.  
  64.         // 查找树中最小的节点
  65.         public Comparable findMin()
  66.         {
  67.                 return findMin2 ( root ).element;
  68.         }
  69.  
  70.         // 查找树中最大的节点
  71.         public Comparable findMax()
  72.         {
  73.                 return findMax2 ( root ).element;
  74.         }
  75.  
  76.         // 向树中插入某节点
  77.         public void insert ( Comparable x )
  78.         {
  79.                 root = insert2 ( x, root );
  80.         }
  81.  
  82.         // 删除树中某节点
  83.         public void remove ( Comparable x )
  84.         {
  85.                 root = remove2 ( x, root );
  86.         }
  87.  
  88.         // 查找的具体操作,该操作对外是透明的,后面的操作同理
  89.         private BinaryNode find2 ( Comparable x, BinaryNode t )
  90.         {
  91.                 // 如果不存在,就新添加一个辅助树节点,并将其内容设为不存在
  92.                 if ( t == null )
  93.                 {
  94.                         BinaryNode s = new BinaryNode ( "不存在该元素!" );
  95.                         return s;
  96.                 }
  97.  
  98.                 if ( x.compareTo ( t.element ) < 0 )   // 如果查找的元素比当前根节点小,则继续再该节点的左子树中查找,直至根节点为空
  99.                 {
  100.                         return find2 ( x, t.left );
  101.                 }
  102.                 else if ( x.compareTo ( t.element ) > 0 )   // 如果查找的元素比当前根节点大,则继续再该节点的右子树中查找,直至根节点为空
  103.                 {
  104.                         return find2 ( x, t.right );
  105.                 }
  106.                 else
  107.                         return t; // 如果查找的节点内容和当前根节点的内容相等,则返回当前根节点
  108.         }
  109.  
  110.         // 找最小节点的具体过程
  111.         private BinaryNode findMin2 ( BinaryNode t )
  112.         {
  113.                 if ( t == null )
  114.                 {
  115.                         return null;
  116.                 }
  117.                 else if ( t.left == null )
  118.                 {
  119.                         return t;
  120.                 }
  121.                 return findMin2 ( t.left );
  122.         }
  123.  
  124.         // 找最大节点的具体过程
  125.         private BinaryNode findMax2 ( BinaryNode t )
  126.         {
  127.                 if ( t != null )
  128.                 {
  129.                         while ( t.right != null )
  130.                         {
  131.                                 t = t.right;
  132.                         }
  133.                 }
  134.                 return t;
  135.         }
  136.  
  137.         // 构造二叉查找树的具体过程
  138.         private BinaryNode insert2 ( Comparable x, BinaryNode t )
  139.         {
  140.                 if ( t == null ) // 若树是空的,则构造一棵新的树,t为树的根
  141.                 {
  142.                         t = new BinaryNode ( x, null, null );
  143.                 }
  144.                 else if ( x.compareTo ( t.element ) < 0 )   // 如果要插入的元素小于当前节点,则插入在该节点的左边
  145.                 {
  146.                         t.left = insert2 ( x, t.left );
  147.                 }
  148.                 else if ( x.compareTo ( t.element ) > 0 )   // 如果要插入的元素大于当前节点,则插入在该节点的又边
  149.                 {
  150.                         t.right = insert2 ( x, t.right );
  151.                 }
  152.                 else
  153.                         ; // 否则什么也不做
  154.                 return t;
  155.         }
  156.  
  157.         // 删除节点的具体操作过程
  158.         private BinaryNode remove2 ( Comparable x, BinaryNode t )
  159.         {
  160.                 if ( t == null )
  161.                 {
  162.                         return t;
  163.                 }
  164.                 if ( x.compareTo ( t.element ) < 0 )
  165.                 {
  166.                         t.left = remove2 ( x, t.left );
  167.                 }
  168.                 else if ( x.compareTo ( t.element ) > 0 )
  169.                 {
  170.                         t.right = remove2 ( x, t.right );
  171.                 }
  172.                 else if ( t.left != null && t.right != null )
  173.                 {
  174.                         t.element = findMin2 ( t.right ).element;
  175.                         t.right = remove2 ( x, t.right );
  176.                 }
  177.                 else
  178.                 {
  179.                         t = ( t.left != null ) ? t.left : t.right;
  180.                 }
  181.                 return t;
  182.         }
  183. }
  184.  
  185.  

回复 "java 二叉查找树(增删改查操作)"

这儿你可以回复上面这条便签

captcha