夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          public class MyBinaryTree
          {
              
          private MyNode root;// 根節點
              private MyBinaryTree left;// 左子樹
              private MyBinaryTree right;// 右子樹
              
              
          /**
               * 添加一個數 將數值插入到二叉樹中,比當前結點小或等于當前結點的插在當前結點的左側,比當前結點大的數插在當前結點的右側,每次從根結點開始遞歸比較
               
          */

              
          public void addData(int n)
              
          {
                  
          if (root == null)
                  
          {
                      root 
          = new MyNode();
                      root.setData(n);
                  }

                  
          else
                  
          {
                      
          int data = root.getData();
                      
          if (n <= data)
                      
          {
                          
          if (this.left == null)
                          
          {
                              
          this.left = new MyBinaryTree();
                          }

                          
          this.left.addData(n);
                      }

                      
          else
                      
          {
                          
          if (this.right == null)
                          
          {
                              
          this.right = new MyBinaryTree();
                          }

                          
          this.right.addData(n);
                      }

                  }

              }

              
              
          /**
               * 先序排序
               
          */

              
          public void preorder()
              
          {
                  
          if (this.root != null)
                  
          {
                      System.out.print(root.getData() 
          + ",");
                  }

                  
          if (this.left != null)
                  
          {
                      
          this.left.preorder();
                  }

                  
          if (this.right != null)
                  
          {
                      
          this.right.preorder();
                  }

              }

              
              
          /**
               * 中序排序
               
          */

              
          public void inorder()
              
          {
                  
          if (this.left != null)
                  
          {
                      
          this.left.inorder();
                  }

                  
          if (this.root != null)
                  
          {
                      System.out.print(root.getData() 
          + ",");
                  }

                  
          if (this.right != null)
                  
          {
                      
          this.right.inorder();
                  }

              }

              
              
          /**
               * 后序排序
               
          */

              
          public void postorder()
              
          {
                  
          if (this.left != null)
                  
          {
                      
          this.left.postorder();
                  }

                  
          if (this.right != null)
                  
          {
                      
          this.right.postorder();
                  }

                  
          if (this.root != null)
                  
          {
                      System.out.print(root.getData() 
          + ",");
                  }

              }

              
              
          public static void main(String[] args)
              
          {
                  
          int[] arr = 28749316,0,5 };
                  MyBinaryTree bt 
          = new MyBinaryTree();
                  
          for (int i = 0; i < arr.length; i++)
                  
          {
                      bt.addData(arr[i]);
                  }

                  System.out.println(
          "先序:");
                  bt.preorder();
                  System.out.println(
          "\n中序:");
                  bt.inorder();
                  System.out.println(
          "\n后序:");
                  bt.postorder();
              }

              
          }


          /**
           * 節點對象
           
          */

          class MyNode
          {
              
          /** 存儲的數據 */
              
          private int data;
              
              
          public int getData()
              
          {
                  
          return data;
              }

              
              
          public void setData(int data)
              
          {
                  
          this.data = data;
              }

          }
          posted on 2014-07-12 20:49 HUIKK 閱讀(675) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 辽宁省| 马山县| 沙河市| 定结县| 固安县| 塘沽区| 临潭县| 化州市| 阜平县| 长乐市| 柳江县| 衡南县| 道孚县| 武清区| 民乐县| 甘德县| 和林格尔县| 伊宁市| 台湾省| 临沧市| 远安县| 太仆寺旗| 珲春市| 岳西县| 玉田县| 阿克陶县| 珠海市| 文化| 罗定市| 大埔区| 额济纳旗| 普宁市| 伊通| 宁武县| 济源市| 紫金县| 武定县| 报价| 敖汉旗| 大同县| 准格尔旗|