konhon

          忘掉過去,展望未來。找回自我,超越自我。
          逃避不一定躲的過, 面對不一定最難過, 孤單不一定不快樂, 得到不一定能長久, 失去不一定不再擁有, 可能因為某個理由而傷心難過, 但我卻能找個理由讓自己快樂.

          Google

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks
          我喜歡解決算法問題 哈哈
          為了解決這個問題 我寫了一個樹狀數(shù)據(jù)結(jié)構(gòu)
          根節(jié)點(diǎn)為世界(我想這個根夠大了吧)
          下面有兩個子節(jié)點(diǎn) 分別為亞洲 歐洲
          亞洲的下面還有幾個節(jié)點(diǎn)分別是中國 韓國......
          中國下面的幾個節(jié)點(diǎn)分別是 福建省 湖北省......
          以次類推
          就是一個樹啦 (不知道這個樹是不是樓主需要的 反正沒事寫著玩唄 起碼能對樓主有所啟發(fā)吧)
          下面是代碼

          首先是 Type (一個對象是一條記錄)

          package?beans;

          public?class?Type?{
          ????????
          private?int?id;

          ????????
          private?String?name;

          ????????
          private?int?parentid;

          ????????
          public?int?getId()?{
          ????????????????
          return?id;
          ????????}


          ????????
          public?void?setId(int?id)?{
          ????????????????
          this.id?=?id;
          ????????}


          ????????
          public?String?getName()?{
          ????????????????
          return?name;
          ????????}


          ????????
          public?void?setName(String?name)?{
          ????????????????
          this.name?=?name;
          ????????}


          ????????
          public?int?getParentid()?{
          ????????????????
          return?parentid;
          ????????}


          ????????
          public?void?setParentid(int?parentid)?{
          ????????????????
          this.parentid?=?parentid;
          ????????}


          }



          然后是 TypeTree 繼承與Type 比Type 多一個ArrayList屬性是 childtype 里面的每一個元素應(yīng)該是 其子節(jié)點(diǎn)TypeTree
          (arrayList 里面可能有多個元素,每個元素都是個TypeTree??也可能沒有)


          package?beans;

          import?java.util.ArrayList;

          public?class?TypeTree?extends?Type?{

          ????????
          private?ArrayList?childtype;

          ????????
          public?ArrayList?getChildtype()?{
          ????????????????
          return?childtype;
          ????????}


          ????????
          public?void?setChildtype(ArrayList?childtype)?{
          ????????????????
          this.childtype?=?childtype;
          ????????}

          }


          然后是最重要的 生成TypeTree 的類??這個類可以把 Type[] 轉(zhuǎn)換成一個 TypeTree 對象
          這個TypeTree 對象是模擬樹狀結(jié)構(gòu)的??每一個節(jié)點(diǎn)都是一個Type
          樹的結(jié)構(gòu)也都是按照 數(shù)據(jù)庫表中??id??與parentid 結(jié)構(gòu)的

          package?beans;

          import?java.util.ArrayList;

          public?class?Tree?{
          ????????
          private?Type[]?type;

          ????????
          private?TypeTree?typeTree;

          ????????
          public?ArrayList?getTree(int?parentid)?{
          ????????????????ArrayList?arrayList?
          =?new?ArrayList();
          ????????????????
          for?(int?i?=?0;?i?<?type.length;?i++)?{
          ????????????????????????
          if?(type[i].getParentid()?==?parentid)?{
          ????????????????????????????????TypeTree?t?
          =?new?TypeTree();
          ????????????????????????????????t.setId(type[i].getId());
          ????????????????????????????????t.setName(type[i].getName());
          ????????????????????????????????t.setParentid(type[i].getParentid());
          ????????????????????????????????t.setChildtype(getTree(type[i].getId()));
          ????????????????????????????????arrayList.add(t);
          ????????????????????????????????System.out.println(
          "Tree"?+?t.getId()?+?"?"?+?t.getName()?+?"?"
          ????????????????????????????????????????????????
          +?t.getParentid());
          ????????????????????????}

          ????????????????}

          ????????????????
          return?arrayList;
          ????????}


          ????????
          public?TypeTree?createTree()?{
          ????????????????
          this.typeTree?=?new?TypeTree();
          ????????????????
          this.typeTree.setId(0);
          ????????????????
          this.typeTree.setName("world");
          ????????????????
          this.typeTree.setParentid(-1);
          ????????????????
          this.typeTree.setChildtype(this.getTree(0));
          ????????????????
          return?this.typeTree;
          ????????}


          ????????
          public?Type[]?getType()?{
          ????????????????
          return?type;
          ????????}


          ????????
          public?void?setType(Type[]?type)?{
          ????????????????
          this.type?=?type;
          ????????}

          }



          最后是我測試用的 類 連接數(shù)據(jù)庫寫的很簡單 別見笑啊
          這個類可以直接運(yùn)行 只要你建立了數(shù)據(jù)源 呵呵
          其中 把樹中所有元素顯示出來 也是用了一個遞歸的方法( public static void display(TypeTree typeTree) )
          這個遞歸的方法在你的JSP顯示的時候可能有用

          package?beans;

          import?java.sql.*;
          import?java.util.ArrayList;

          public?class?TreeTest?{
          ????????Connection?con?
          =?null;

          ????????Statement?sta?
          =?null;

          ????????ResultSet?res?
          =?null;

          ????????String?sql?
          =?"select?*?from?treetable";

          ????????
          public?Type[]?qiaolian()?{
          ????????????????ArrayList?arrayList?
          =?new?ArrayList();
          ????????????????
          try?{
          ????????????????????????Class.forName(
          "sun.jdbc.odbc.JdbcOdbcDriver");
          ????????????????}
          ?catch?(ClassNotFoundException?e)?{
          ????????????????????????System.out.println(e);
          ????????????????}

          ????????????????
          try?{
          ????????????????????????con?
          =?DriverManager.getConnection("jdbc:odbc:hib",?"sa",?"");
          ????????????????}
          ?catch?(SQLException?e)?{
          ????????????????????????System.out.println(e);
          ????????????????}

          ????????????????
          try?{
          ????????????????????????sta?
          =?con.createStatement();
          ????????????????????????res?
          =?sta.executeQuery(sql);
          ????????????????????????
          while?(res.next())?{
          ????????????????????????????????Type?t?
          =?new?Type();
          ????????????????????????????????t.setId(res.getInt(
          "id"));
          ????????????????????????????????t.setName(res.getString(
          "name"));
          ????????????????????????????????t.setParentid(res.getInt(
          "parentid"));
          ????????????????????????????????arrayList.add(t);
          ????????????????????????}

          ????????????????}
          ?catch?(SQLException?e)?{
          ????????????????????????System.out.println(e);
          ????????????????}

          ????????????????Type[]?type?
          =?new?Type[arrayList.size()];
          ????????????????
          for?(int?i?=?0;?i?<?arrayList.size();?i++)?{
          ????????????????????????type[i]?
          =?(Type)?arrayList.get(i);
          ????????????????}

          ????????????????
          return?type;
          ????????}


          ????????
          public?static?void?main(String[]?args)?{
          ????????????????TreeTest?treeTest?
          =?new?TreeTest();
          ????????????????Type[]?ty?
          =?treeTest.qiaolian();
          ????????????????Tree?tree?
          =?new?Tree();
          ????????????????tree.setType(ty);
          ????????????????TypeTree?typeTree?
          =?tree.createTree();
          ????????????????display(typeTree);
          ????????}


          ????????
          public?static?void?display(TypeTree?typeTree)?{
          ????????????????TypeTree?now?
          =?typeTree;
          ????????????????System.out.println(now.getId()?
          +?"\t"?+?now.getName()?+?"\t"
          ????????????????????????????????
          +?now.getParentid()?+?"\t"?+?"child?num"
          ????????????????????????????????
          +?now.getChildtype().size());
          ????????????????
          if?(now.getChildtype()?!=?null)?{
          ????????????????????????ArrayList?arrayList?
          =?now.getChildtype();
          ????????????????????????
          for?(int?i?=?0;?i?<?arrayList.size();?i++)?{
          ????????????????????????????????display((TypeTree)?arrayList.get(i));
          ????????????????????????}

          ????????????????}

          ????????}

          }

          posted on 2006-03-31 02:54 konhon 優(yōu)華 閱讀(479) 評論(0)  編輯  收藏 所屬分類: JSP/Servlet
          主站蜘蛛池模板: 盐边县| 嘉义县| 常德市| 汾西县| 铅山县| 沧州市| 若尔盖县| 白山市| 紫阳县| 略阳县| 榆中县| 巧家县| 叙永县| 林甸县| 庆安县| 台南市| 祥云县| 巴南区| 松江区| 宁南县| 惠安县| 尼勒克县| 清镇市| 九台市| 开平市| 凌源市| 田林县| 平罗县| 长丰县| 电白县| 株洲市| 赤水市| 汾西县| 哈尔滨市| 谢通门县| 邵阳县| 平度市| 鹤峰县| 三门峡市| 赤壁市| 凤台县|