<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    夢幻之旅

    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 閱讀(671) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: a级毛片高清免费视频就| 动漫黄网站免费永久在线观看 | 国产免费私拍一区二区三区| 成年网站免费入口在线观看| 久久亚洲伊人中字综合精品| 国产免费av片在线看| 亚欧国产一级在线免费| 91亚洲精品麻豆| 亚洲国产天堂久久综合| 69免费视频大片| 免费毛片毛片网址| 亚洲大片免费观看| 亚洲综合区小说区激情区| 国产男女爽爽爽爽爽免费视频| 羞羞视频免费网站入口| 亚洲福利电影在线观看| 亚洲精品动漫人成3d在线| 免费福利网站在线观看| 国产一级一毛免费黄片| 99亚洲乱人伦aⅴ精品| 99亚洲精品高清一二区| 亚洲精品无码99在线观看 | 亚洲AV无码专区亚洲AV伊甸园 | 亚洲自偷自拍另类图片二区| 亚洲喷奶水中文字幕电影| 免费大黄网站在线观| 大片免费观看92在线视频线视频| 亚洲激情黄色小说| 亚洲精品中文字幕乱码三区 | 最近中文字幕完整免费视频ww | 免费无遮挡无码视频在线观看 | 99爱视频99爱在线观看免费| 免费无码国产在线观国内自拍中文字幕| 亚洲男人天堂影院| 日韩亚洲一区二区三区| 亚洲精品国产va在线观看蜜芽| 成人特黄a级毛片免费视频| 久久久久久AV无码免费网站下载 | 亚洲理论片在线观看| 国产亚洲AV无码AV男人的天堂| 亚洲成a人片在线观看国产|