春風博客

          春天里,百花香...

          導航

          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          統計

          公告

          MAIL: junglesong@gmail.com
          MSN: junglesong_5@hotmail.com

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          二叉樹搜索樹代碼

          /**
           * 二叉樹節點類
           * 
          @author HEYANG
           * 
          @since 2008-7-26 下午02:59:06
           
          */

          class Node<extends Comparable> {
            
          public Node(T data){
              
          this.data=data;
            }

            
            T data;
            Node
          <T> left;
            Node
          <T> right;
          }


          /**
           * 二叉樹類
           * 
          @author HEYANG
           * 
          @since 2008-7-26 下午02:55:33
           
          */

          public class BinaryTree<extends Comparable> {

            
          /**
             * 根節點
             
          */

            
          private Node<T> root;

            
          /**
             * 插入一個值
             * 
          @param value
             
          */

            
          public void insert(T value) {
              Node
          <T> node = new Node<T>(value);

              
          if (root == null{
                root 
          = node;
              }
           else {
                Node
          <T> curr = root;
                Node
          <T> parrent;

                
          while (true{
                  parrent 
          = curr;

                  
          if (value.compareTo(curr.data) >= 0{
                    curr 
          = curr.right;

                    
          if (curr == null{
                      parrent.right
          =node;
                      
          return;
                    }

                  }
           else {
                    curr 
          = curr.left;

                    
          if (curr == null{
                      parrent.left
          =node;
                      
          return;
                    }

                  }

                }

              }

            }


            
          /**
             * 尋找一個值對應的節點
             * 
          @param value
             * 
          @return
             
          */

            
          public Node<T> find(T value) {
              Node
          <T> curr = root;

              
          while (curr.data.equals(value) == false{
                
          if (value.compareTo(curr.data) > 0{
                  curr 
          = curr.right;
                }
           else {
                  curr 
          = curr.left;
                }


                
          if (curr == null{
                  
          return null;
                }

              }


              
          return curr;
            }


            
          /**
             * 輸出
             *
             
          */

            
          public void printAll() {
              System.out.println(
          "--------------先序遍歷------------------");
              preOrder(root);
              System.out.println(
          "--------------中序遍歷------------------");
              inorder(root);
              System.out.println(
          "--------------后序遍歷------------------");
              postOrder(root);
            }

            
            
          /**
             * 先序遍歷
             * 
          @param node
             
          */

            
          private void preOrder(Node<T> node) {
              
          if (node != null{
                System.out.println(node.data);
                preOrder(node.left);      
                preOrder(node.right);
              }

            }


            
          /**
             * 中序遍歷
             * 
          @param node
             
          */

            
          private void inorder(Node<T> node) {
              
          if (node != null{
                inorder(node.left);
                System.out.println(node.data);
                inorder(node.right);
              }

            }

            
            
          /**
             * 后序遍歷
             * 
          @param node
             
          */

            
          private void postOrder(Node<T> node) {
              
          if (node != null{
                postOrder(node.left);     
                postOrder(node.right);
                System.out.println(node.data);
              }

            }


            
          public static void main(String[] args) {
              
              Integer[] arr
          ={31,25,47,42,50};
              
              
          // 以數組2為基礎創建二叉樹
              BinaryTree<Integer> tree1=new BinaryTree<Integer>();   
              
          for(Integer i:arr){
                tree1.insert(i);
              }

              
              tree1.printAll();
              
              
              String[] arr02
          ={"Ceaser","Andy","Martin","Bill","Felex","Fowler","Green","Alice","Gates"};
              
              
          // 以數組2為基礎創建二叉樹
              BinaryTree<String> tree=new BinaryTree<String>();   
              
          for(String str:arr02){
                tree.insert(str);
              }

              
              tree.printAll();
              
              
          // 將在二叉樹中不存在的元素放入鏈錶
              String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
              List
          <String> ls=new ArrayList<String>();    
              
          for(String str:arr01){
                
          if(tree.find(str)==null){
                  ls.add(str);
                }

              }

              
              
          // 輸出
              System.out.println("--------------二叉樹中不存在的元素有------");
              
          for(String str:ls){
                System.out.println(str);
              }

            }

          }

          posted on 2008-07-26 16:25 sitinspring 閱讀(1181) 評論(1)  編輯  收藏 所屬分類: 算法數據結構

          評論

          # re: 二叉樹搜索樹代碼 2008-10-04 08:34 sclsch

          老兄,二叉樹有沒有實際應用的實例。  回復  更多評論   

          sitinspring(http://www.aygfsteel.com)原創,轉載請注明出處.
          主站蜘蛛池模板: 逊克县| 道真| 商水县| 宁化县| 紫阳县| 临武县| 黎城县| 彭阳县| 建平县| 信丰县| 宜丰县| 富阳市| 大名县| 会泽县| 攀枝花市| 天全县| 南阳市| 临城县| 鹤庆县| 馆陶县| 辉县市| 丰原市| 余干县| 安国市| 望都县| 老河口市| 宁化县| 岳池县| 宝鸡市| 酉阳| 成安县| 凌海市| 随州市| 堆龙德庆县| 南安市| 铁岭县| 花莲市| 贵州省| 上饶市| 拜城县| 九龙坡区|