<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();
        }

    }



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


    網站導航:
     
    主站蜘蛛池模板: 亚洲中文字幕不卡无码| 影音先锋在线免费观看| 人人狠狠综合久久亚洲婷婷| 人禽伦免费交视频播放| 亚洲一级特黄大片在线观看| 特黄特色大片免费| 亚洲午夜福利精品久久| 成人无码区免费A∨直播| 国产AV无码专区亚洲AV毛网站| 中文字幕免费观看全部电影| 国产亚洲精品a在线观看app| 久久国产免费一区二区三区| 亚洲AV日韩AV永久无码久久| 1000部啪啪未满十八勿入免费| 亚洲国产成人手机在线电影bd| 少妇高潮太爽了在线观看免费| 亚洲日韩精品无码专区加勒比| 国产色爽女小说免费看| v片免费在线观看| 亚洲AV无码一区二区三区系列| 男女午夜24式免费视频| 亚洲人成在线精品| 亚洲精品国产高清不卡在线| 免费在线观看一级片| 亚洲一卡二卡三卡| 亚洲精品老司机在线观看| 污污网站免费观看| 亚洲无人区码一二三码区别图片| 四虎亚洲国产成人久久精品| a毛片全部免费播放| 亚洲一区免费在线观看| 亚洲日韩国产精品乱| 57pao国产成视频免费播放| 亚洲AV噜噜一区二区三区| 亚洲日韩激情无码一区| 日韩精品无码区免费专区| 黄色网页在线免费观看| 激情亚洲一区国产精品| 中文字幕亚洲综合久久男男| 无码国产精品一区二区免费式影视| 国产成人综合亚洲|