<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 閱讀(285) 評論(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();
        }

    }



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


    網站導航:
     
    主站蜘蛛池模板: 亚洲一级免费视频| 亚洲一区二区三区在线网站 | 免费va在线观看| 色多多免费视频观看区一区| 国产高清免费观看| 国产亚洲精品成人久久网站| 国产一区二区三区在线免费 | 亚洲国产欧美一区二区三区| 日本三级2019在线观看免费| 亚洲国产精品久久久久秋霞影院| 6080午夜一级毛片免费看| 亚洲经典在线中文字幕| 精品久久久久成人码免费动漫| 中国亚洲呦女专区| 国产精品免费小视频| 日韩免费在线中文字幕| 亚洲人成网77777亚洲色| 日韩av无码久久精品免费| 亚洲一级毛片免观看| 免费国产在线观看不卡| 三上悠亚在线观看免费| 亚洲熟妇无码爱v在线观看| 午夜成年女人毛片免费观看 | 久久午夜伦鲁片免费无码| 亚洲国产精品综合福利专区| 永久黄网站色视频免费| 男人天堂免费视频| 亚洲国产成人精品电影| 免费在线观看中文字幕| 久久久久久夜精品精品免费啦 | 国产精品亚洲综合一区| 99精品视频在线免费观看| 亚洲香蕉久久一区二区| 亚洲人成无码久久电影网站| 最近中文字幕大全免费视频| 亚洲变态另类一区二区三区| 亚洲va无码手机在线电影| 97人伦色伦成人免费视频| 久久一区二区三区免费| 亚洲香蕉久久一区二区三区四区| 国产精品亚洲αv天堂无码|