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

    }



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


    網站導航:
     
    主站蜘蛛池模板: 国产高清对白在线观看免费91 | 国产免费av片在线无码免费看| 亚洲人成综合网站7777香蕉| 免费观看国产精品| 日本视频免费高清一本18| 亚洲av一本岛在线播放| 免费人成在线观看视频播放| 久久免费公开视频| 久久无码av亚洲精品色午夜| 亚洲春色在线视频| 日本高清免费网站| 十九岁在线观看免费完整版电影| 亚洲欧美日韩自偷自拍| 亚洲男同帅GAY片在线观看| 毛片免费视频观看| 国产一精品一av一免费爽爽| 亚洲一本到无码av中文字幕| 亚洲国产精品成人久久| 国产免费av片在线播放| 日本高清在线免费| 香蕉免费在线视频| 亚洲Aⅴ在线无码播放毛片一线天 亚洲avav天堂av在线网毛片 | 国内外成人免费视频| 一个人看的www免费视频在线观看| 亚洲AV日韩综合一区尤物| 亚洲AV永久无码区成人网站| 无码国模国产在线观看免费| 1000部夫妻午夜免费| 久久不见久久见免费影院www日本| 亚洲最大的成人网| 久久久久亚洲AV无码专区首JN| 亚洲午夜精品一级在线播放放| 丁香花在线观看免费观看 | 女人18毛片水真多免费播放| 久久精品无码精品免费专区| 免费高清A级毛片在线播放| 亚洲最大成人网色香蕉| 18gay台湾男同亚洲男同| 中文字幕亚洲图片| 国产男女猛烈无遮挡免费网站| 日韩亚洲国产高清免费视频|