查找树的java实现

笔者懒散,各位朋友有什么指教探讨的地方可以在博客留言
  
  
  
  
  1. package seventh; 
  2.  
  3. public class SearchTree { 
  4.     public SearchTree leftNode=null
  5.     public SearchTree rightNode=null
  6.     public SearchTree cTree=null
  7.     public int data; 
  8.      
  9.     public  SearchTree(int data){ 
  10.         this.data=data; 
  11.     } 
  12.     /** 
  13.      * 清空整棵树 
  14.      * @param tree 代表要从哪个节点开始清空,默认使用根节点 
  15.      */ 
  16.      
  17.     public void emptyTree(){ 
  18.         SearchTree tree=this
  19.         if(tree.leftNode!=null
  20.         { 
  21.             emptyTree(tree.leftNode); 
  22.         } 
  23.         if(tree.rightNode!=null
  24.         { 
  25.             emptyTree(tree.rightNode); 
  26.         } 
  27.         tree=null
  28.     } 
  29.      
  30.     public void emptyTree(SearchTree tree){ 
  31.         if(tree.leftNode!=null
  32.         { 
  33.             emptyTree(tree.leftNode); 
  34.         } 
  35.         if(tree.rightNode!=null
  36.         { 
  37.             emptyTree(tree.rightNode); 
  38.         } 
  39.         tree=null
  40.     } 
  41.     /** 
  42.      * 递归查找 
  43.      * @param data表示要被查找的数据 
  44.      * @param data表示要从哪个节点开始查找, 默认使用根节点 
  45.      * @return 
  46.      */ 
  47.  
  48.     public SearchTree findNode(int data){ 
  49.         SearchTree tree=this
  50.         if(data<tree.data) 
  51.         { 
  52.             return findNode(tree.leftNode,data); 
  53.         } 
  54.         else if(data>tree.data) 
  55.         { 
  56.             return findNode(tree.rightNode,data); 
  57.         } 
  58.         else  
  59.             return tree; 
  60.     } 
  61.      
  62.     public SearchTree findNode(SearchTree tree,int data){ 
  63.         if(tree==null){ 
  64.             return null
  65.         } 
  66.         else if(data<tree.data) 
  67.         { 
  68.             return findNode(tree.leftNode,data); 
  69.         } 
  70.         else if(data>tree.data) 
  71.         { 
  72.             return findNode(tree.rightNode,data); 
  73.         } 
  74.         else  
  75.             return tree; 
  76.     } 
  77.     /*查找最小的节点 
  78.      * @parameter tree 代表要从哪个节点开始查,默认使用根 
  79.      *  
  80.      */ 
  81.     public SearchTree findMin(){ 
  82.         SearchTree tree=this
  83.         if(tree.leftNode!=null
  84.         { 
  85.             return findMin(tree.leftNode); 
  86.         } 
  87.         else 
  88.             return this
  89.     } 
  90.     public SearchTree findMin(SearchTree tree){ 
  91.         if(tree.leftNode!=null
  92.         { 
  93.             return findMin(tree); 
  94.         } 
  95.         else 
  96.             return tree; 
  97.     } 
  98.     /** 
  99.      *删除二叉排序树种的节点 ,分两种情况(1.只有一个孩子或者没有孩子,那么只要删除该叶子节点或者使用孩子节点的值替换自己 
  100.      *2.有两个孩子,使用节点的右孩子中最小的节点值替换自己,并删除右孩子中带有最小值得那个节点  
  101.      * @param a 代表要删除的节点的值 
  102.      * @param tree 默认使用被查找到的与被删除data一样的值得节点,也可以显示的指定要被删除的节点 
  103.      */ 
  104.     public void deleteNode(int a){ 
  105.         SearchTree tree=findNode(a); 
  106.         if(tree==null){ 
  107.             return
  108.         } 
  109.         //当被删除的节点有两个孩子的情况 
  110.         if(tree.leftNode!=null&&tree.rightNode!=null
  111.         { 
  112.             SearchTree sTree=findMin(tree); 
  113.             tree.data=sTree.data; 
  114.             deleteNode(sTree,sTree.data); 
  115.         } 
  116.          
  117.         //当被删除的节点有一个孩子或者没有的情况 
  118.         else 
  119.         { 
  120.             if(tree.leftNode==null
  121.             { 
  122.                 tree=tree.rightNode; 
  123.             } 
  124.             else if(tree.rightNode==null
  125.             { 
  126.                 tree=tree.leftNode; 
  127.             } 
  128.         } 
  129.     } 
  130.      
  131.     public void deleteNode(SearchTree tree,int a){ 
  132.         if(tree.leftNode!=null&&tree.rightNode!=null
  133.         { 
  134.             SearchTree sTree=findMin(tree); 
  135.             tree.data=sTree.data; 
  136.             deleteNode(sTree.data); 
  137.         } 
  138.     } 
  139.  
  140.      
  141.     /** 
  142.      * 用递归的方法实现插入与 查找方法如初一折 
  143.      * @param tree 代表要从哪里开始查找插入节点,默认使用根,如果这个节点的值大于data就递归查询它做节点,反之亦然 
  144.      * @param data 代表要插入的数据 
  145.      */  
  146.     public void insertNode(int data){ 
  147.         SearchTree tree=this
  148.         if(data<tree.data) 
  149.         { 
  150.             insertNode(tree.leftNode,data); 
  151.         } 
  152.         else if(data>tree.data) 
  153.         { 
  154.             insertNode(tree.rightNode,data); 
  155.         } 
  156.         else 
  157.         { 
  158.             return
  159.         } 
  160.     } 
  161.      
  162.     public void insertNode(SearchTree tree,int data){ 
  163.         if(tree==null){ 
  164.             tree=new SearchTree(data); 
  165.         } 
  166.         else if(data<tree.data) 
  167.         { 
  168.             insertNode(tree.leftNode,data); 
  169.         } 
  170.         else if(data>tree.data) 
  171.         { 
  172.             insertNode(tree.rightNode,data); 
  173.         } 
  174.         else 
  175.         { 
  176.             return
  177.         } 
  178.     } 
  179.      

你可能感兴趣的:(java,数据结构,职场,休闲,查找树)