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

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

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

    posts - 13, comments - 7, trackbacks - 0, articles - 0

    數據結構中的樹

    Posted on 2008-10-24 00:06 eyejava 閱讀(283) 評論(0)  編輯  收藏
    package tree;

    /**
     * 結點數據
     * @author jiaqiang
     *
     */
    public class Node {
        private int key;
        private int data;
        private Node leftChild;
        private Node rightChild;
        
        public Node() {}
        
        public Node(int key, int data) {
            this.key = key;
            this.data = data;
        }

        public int getKey() {
            return key;
        }

        public int getData() {
            return data;
        }

        public Node getLeftChild() {
            return leftChild;
        }

        public void setLeftChild(Node leftChild) {
            this.leftChild = leftChild;
        }

        public Node getRightChild() {
            return rightChild;
        }

        public void setRightChild(Node rightChild) {
            this.rightChild = rightChild;
        }
        
        public void display() {
            System.out.print(data + " ");
        }
    }

    -------------------
    package tree;

    /**
     * 二叉樹
     * @author jiaqiang
     *
     */
    public class Tree {
        private Node root;
        private int size;
        
        /**
         * 查找某個值
         * @param key   要查找的值
         * @return Node 查找到的結點
         */
        public Node find(int key) {
            Node current = root;
        
            while ((current!=null) && (current.getKey()!=key)) {
                if (key < current.getKey()) {           //go left
                    current = current.getLeftChild();
                } else {
                    current = current.getRightChild();  //go right
                }
                
                if (current == null) {
                    return null;
                }
            }
            
            return current;
        }
        
        /**
         * 插入某個值
         * @param key
         * @param data
         */
        public void insert(int key, int data) {
            Node newNode = new Node(key, data);
            
            if (root == null) {  //空樹
                root = newNode;
                size++;
            } else {             //not empty tree
                Node current = root;
                Node parent;     //作為插入結點的父結點
                
                while (true) {
                    parent = current;
                    if (key < current.getKey()) {
                        current = current.getLeftChild();
                        if (current == null) {
                            parent.setLeftChild(newNode);
                            size++;
                            return;
                        }
                    } //end if go left
                    else {
                        current = current.getRightChild();
                        if (current == null) {
                            parent.setRightChild(newNode);
                            size++;
                            return;
                        }
                    } //end if go right
                } // end while (true)
            }
        } //end insert
        
        /**
         * 中序遍歷,該方法為輔助方法
         * @param Node localRoot
         */
        private void inOrder(Node localRoot) {
            if (localRoot != null) {
                inOrder(localRoot.getLeftChild());
                localRoot.display();
                inOrder(localRoot.getRightChild());
            } else
                return;
        }
        
        /**
         * 中序遍歷
         */
        public void inOrder() {
            inOrder(root);
        }
        
        public int getSize() {
            return size;
        }

        public static void main(String[] args) {
            Tree tree = new Tree();
            tree.insert(23, 23);
            tree.insert(20, 20);
            tree.insert(24, 24);
         
            
            System.out.println(tree.getSize());
            tree.inOrder();
        }

    }



    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲一区二区三区香蕉| 亚洲综合久久一本伊伊区| 51在线视频免费观看视频| 亚洲乱码一二三四区麻豆| 亚洲精品456人成在线| 国产精品视_精品国产免费| 亚洲最大av资源站无码av网址| 最近中文字幕免费2019| 亚洲AV无码国产精品永久一区| 国内自产拍自a免费毛片| 国产午夜精品理论片免费观看| 亚洲色偷偷偷鲁综合| 久久久久久免费视频| EEUSS影院WWW在线观看免费| 成人午夜亚洲精品无码网站| 91精品免费国产高清在线| 农村寡妇一级毛片免费看视频| 亚洲欧洲精品一区二区三区| 国产一区二区免费| 亚洲精华国产精华精华液好用| 亚洲第一AAAAA片| 四虎最新永久免费视频| 亚洲一卡2卡3卡4卡国产网站| va亚洲va日韩不卡在线观看| 二个人看的www免费视频| 亚洲a∨无码男人的天堂| 亚洲精品色午夜无码专区日韩| 日美韩电影免费看| 一级午夜免费视频| 久久亚洲国产精品| 日韩精品亚洲专区在线观看| 青青青国产在线观看免费网站| 精品无码一级毛片免费视频观看| 久久亚洲精品国产精品| 成人au免费视频影院| 99精品视频在线观看免费专区| 亚洲色大成网站www久久九| 久久久无码精品亚洲日韩蜜臀浪潮| 久久影院亚洲一区| 无码区日韩特区永久免费系列| 免费人成网站在线观看不卡|