春風博客

          春天里,百花香...

          導航

          <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)原創,轉載請注明出處.
          主站蜘蛛池模板: 肃宁县| 和龙市| 秦安县| 大冶市| 锦州市| 新蔡县| 高平市| 永济市| 象山县| 上饶县| 会东县| 衡阳市| 宜阳县| 武乡县| 呼图壁县| 汽车| 安塞县| 武穴市| 普兰店市| 伊宁县| 合肥市| 镇巴县| 辽阳县| 西畴县| 依兰县| 凤冈县| 铁岭市| 砀山县| 陕西省| 色达县| 隆林| 峨山| 伊春市| 莲花县| 石首市| 巨野县| 长岭县| 扬州市| 佛学| 平利县| 蒙自县|