두 갈래 찾기 트 리 간단 실현

트 리 찾기 는 데이터 구조 로 다양한 동적 집합 작업 을 지원 합 니 다. 임의의 키워드 노드 를 찾 고 최대 노드 를 되 돌려 주 며 최소 노드 를 되 돌려 주 고 전구 와 후계 결점 을 되 돌려 주 며 삽입 과 삭 제 를 지원 합 니 다. 하위 사전 으로 도 사용 할 수 있 고 우선 대기 열 로 도 사용 할 수 있 습 니 다.
    이 진 트 리 는 기본 데이터 구조 에 속 하고 이 진 트 리 에서 실 행 된 절 본 작업 의 시간 은 나무의 높이 와 정비례 한다.다음은 이 진 트 리 찾기 의 간단 한 실현 으로 학습 에 참고 할 수 있 습 니 다.

  
  
  
  
  1. /** 
  2.  *    
  3. * @author Jayson
  4.  */ 
  5. public class TwoSearchTree<T> 
  6. { 
  7.     private class Node 
  8.     { 
  9.         private int key;        //  
  10.         private Object data;    //  
  11.         private Node left;      //  
  12.         private Node right;     //  
  13.         private Node parent;    //  
  14.         public Node(int key,Object data,Node parent,Node left,Node right) 
  15.         { 
  16.             this.key=key; 
  17.             this.data=data; 
  18.             this.parent=parent; 
  19.             this.left=left; 
  20.             this.right=right; 
  21.         } 
  22.     } 
  23.  
  24.     private Node root; 
  25.     private int height; 
  26.     public TwoSearchTree(int key){ 
  27.      
  28.         this.root=new Node(key,null,null,null,null); 
  29.     } 
  30.  
  31.     public TwoSearchTree(int key,T data) 
  32.     { 
  33.         this.root=new Node(key,data,null,null,null);     
  34.     } 
  35.  
  36.     public Node getNode(Node current,int key) 
  37.     { 
  38.         if(current==null||key==current.key) 
  39.         { 
  40.             return current; 
  41.         } 
  42.         if(key<current.key) 
  43.         { 
  44.             return getNode(current.left,key); 
  45.         } 
  46.         else 
  47.         { 
  48.             return getNode(current.right,key); 
  49.         } 
  50.     } 
  51.     /* 
  52.      *  
  53.      */ 
  54.     public Node getNodeByKey(int key) 
  55.     { 
  56.         return getNode(root,key); 
  57.     } 
  58.      
  59.     public T getData(Node node) 
  60.     { 
  61.         return (T)node.data; 
  62.     } 
  63.     /* 
  64.      *    
  65.      */ 
  66.     public Node getMax(Node current) 
  67.     { 
  68.         if(current.right==null) 
  69.         { 
  70.             return current; 
  71.         } 
  72.         else 
  73.         { 
  74.             return getMax(current.right); 
  75.         } 
  76.     } 
  77.     /* 
  78.      *    
  79.      */ 
  80.     public Node getMin(Node current) 
  81.     { 
  82.         if(current.left==null) 
  83.         { 
  84.             return current; 
  85.         } 
  86.         else 
  87.         { 
  88.             return getMin(current.left); 
  89.         } 
  90.     } 
  91.     /* 
  92.      *    
  93.      */ 
  94.     public Node getSucceed(Node current){ 
  95.         if(current.right!=null) 
  96.         { 
  97.             return getMin(current.right); 
  98.         } 
  99.         Node now=current.parent; 
  100.         while(now!=null&&current==now.right) 
  101.         { 
  102.             current=now; 
  103.             now=now.parent; 
  104.         } 
  105.         return now; 
  106.     } 
  107.     /* 
  108.      *    
  109.      */ 
  110.     public void addElement(int key,T data) 
  111.     { 
  112.         if(root==null) 
  113.         { 
  114.             root=new Node(key,data,null,null,null); 
  115.         } 
  116.         else 
  117.         { 
  118.             Node current=root; 
  119.             while(current!=null) 
  120.             { 
  121.                 if(key<current.key) 
  122.                 { 
  123.                     if(current.left==null) 
  124.                     { 
  125.                         Node newNode=new Node(key,data,current,null,null); 
  126.                         current.left=newNode; 
  127.                         break; 
  128.                     } 
  129.                     current=current.left; 
  130.                 } 
  131.                 else 
  132.                 { 
  133.                     if(current.right==null) 
  134.                     { 
  135.                         Node newNode=new Node(key,data,current,null,null); 
  136.                         current.right=newNode; 
  137.                         break; 
  138.                     } 
  139.                     current=current.right; 
  140.                 } 
  141.             } 
  142.         } 
  143.     } 
  144.     /* 
  145.      *    
  146.      */ 
  147.     public Node delElement(Node node) 
  148.     { 
  149.         Node rear=null; 
  150.         Node next=null; 
  151.         if(node.left==null||node.right==null) 
  152.         { 
  153.             rear=node; 
  154.         } 
  155.         else 
  156.         { 
  157. // ,
  158.             rear=getSucceed(node); 
  159.         } 
  160.         if(rear.left!=null) 
  161.         { 
  162.             next=rear.left; 
  163.         } 
  164.         else 
  165.         { 
  166.             next=rear.right; 
  167.         } 
  168.         if(next!=null) 
  169.         { 
  170.             next.parent=rear.parent; 
  171.         } 
  172.         if(rear.parent==null)    
  173.         { 
  174.             root=next; 
  175.         } 
  176.         else if(rear==rear.parent.left) 
  177.         { 
  178.             rear.parent.left=next; 
  179.         } 
  180.         else 
  181.         { 
  182.             rear.parent.right=next; 
  183.         } 
  184.         if(rear!=next)   
  185.         { 
  186.             node.key=rear.key; 
  187.             node.data=rear.data; 
  188.         } 
  189.         return rear; 
  190.     } 
  191.     /* 
  192.      *    
  193.      */ 
  194.     public int deep(Node node) 
  195. { 
  196.         if(node==null) 
  197.         { 
  198.             return 0; 
  199.         } 
  200.         if(node.left==null&&node.right==null) 
  201.         { 
  202.             return 1; 
  203.         } 
  204.         else 
  205.         { 
  206.             int deep1=deep(node.left); 
  207.             int deep2=deep(node.right); 
  208.             int deep=deep1>deep2 ? deep1:deep2; 
  209.             return deep+1; 
  210.         }    
  211.     } 
  212.  
  213.     public int deep(){ 
  214.         return deep(root); 
  215.     } 
  216.      
  217.     public void browseAllElement(Node node) 
  218.     {    
  219.         //
  220.         if(node!=null) 
  221.         { 
  222.             System.out.print("("+node.key+","+node.data+")"+" "); 
  223.             browse(node.left); 
  224.             browse(node.right); 
  225.         } 
  226.     } 
  227.     public void browseAllElement() 
  228.     { 
  229.         browseAllElement(root); 
  230.         System.out.println(""); 
  231.     } 
  1. /*
  2. *
  3. */
  4.     public static void main(String[] star){ 
  5.         TwoSearchTree<String> tst=new TwoSearchTree<String>(8,"d"); 
  6.         tst.addElement(12,"e"); 
  7.         tst.addElement(2,"a"); 
  8.         tst.addElement(4,"b"); 
  9.         tst.addElement(14,"f"); 
  10.         tst.addElement(16,"g"); 
  11.         tst.addElement(6,"c"); 
  12.         tst.addElement(1,"h"); 
  13.         tst.addElement(11,"i"); 
  14.         tst.delElement(tst.getNodeByKey(8)); // 8
  15.         tst.browseAllElement(); 
  16.         System.out.println(tst.deep()); 
  17.     } 
  18. } 

좋은 웹페이지 즐겨찾기