java中的二叉樹排序問題

來源:互聯網
上載者:User

標籤:turn   fat   public   sys   class   div   重載方法   tput   out   

原創:轉載請註明出處

目的:想用java實現二叉樹排序演算法

思想:利用java中物件導向的思想,即:

  Tree:類

  樹根Tree:root    //static所屬於每一個Tree

  左節點Tree:leftSon

  右節點Tree:rightSon

  父親節點Tree:father

上代碼 Tree類:

package com.cissst.dom1;/** * 二叉樹 * @author phoebe */public class Tree {    public Integer data;//每一個節點的值    public static Tree root;//根節點(有且僅有一個)    public Tree father;//父節點    public Tree leftSon;//左子節點    public Tree rightSon;//右子節點        //左樹是否為空白    public boolean hasLeftSon(){        return leftSon!=null;    }    //右樹是否為空白    public boolean hasRightSon(){        return rightSon!=null;    }        //插入節點    public void insert(Integer data,Tree father){        /**         * 思想:先讓data和root中的值進行比較,大於0插入右邊,小於0插入左邊,計劃使用遞迴思想         */        //等於root.data        if(data.compareTo(father.data)==0){            return;        }        //大於root.data        if(data.compareTo(father.data)>0){            //父節點沒有右節點            if(!father.hasRightSon()){                father.rightSon = new Tree();//產生一個右節點                father.rightSon.data=data;//給右節點賦值                father.rightSon.father=father;//指定右節點的父親是誰            }else{                insert(data,father.rightSon);            }        }                //小於同上        if(data.compareTo(father.data)<0){            //父節點沒有左節點            if(!father.hasLeftSon()){                father.leftSon = new Tree();//產生一個右節點                father.leftSon.data=data;//給右節點賦值                father.leftSon.father=father;//指定右節點的父親是誰            }else{                insert(data,father.leftSon);            }        }    }        /**     * 總體插入操作     * 1.判斷是否有樹根,沒有的話將資料添加到樹根裡     * 2.有樹根調用insert的重載方法,判斷插入到左son還是右son     * @param data     */    public void insert(Integer data){        if(root==null){            root = new Tree();            root.data=data;            return;        }else{            insert(data,root);        }    }        }

測試代碼:

package com.cissst.dom1;/** * 樹測試代碼 * @author phoebe */public class TreeTest {    public static void main(String[] args) {        TreeTest tt = new TreeTest();        Tree tree = new Tree();        tree.insert(5);        tree.insert(2);        tree.insert(3);        tree.insert(1);        tree.insert(8);        tree.insert(3);        tt.outPutTree(tree.root);        tt.getMinValue(tree.root);        tt.getMaxValue(tree.root);    }    //遍曆樹中的集合    public void outPutTree(Tree tree){        System.out.print(tree.data+" ");        if(tree.hasLeftSon()){            outPutTree(tree.leftSon);        }        if(tree.hasRightSon()){            outPutTree(tree.rightSon);        }    }    //找出樹中最小的值    public void getMinValue(Tree tree){        if(tree.hasLeftSon()){            getMinValue(tree.leftSon);        }else{            System.out.println("最小值"+tree.data);        }    }    //找出樹中最大的值    public void getMaxValue(Tree tree){        if(tree.hasRightSon()){            getMaxValue(tree.rightSon);        }else{            System.out.println("最大值"+tree.data);        }    }        }

 

java中的二叉樹排序問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.