<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
    package tree;

    /**
     * 結(jié)點(diǎn)數(shù)據(jù)
     * @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;
        
        /**
         * 查找某個(gè)值
         * @param key   要查找的值
         * @return Node 查找到的結(jié)點(diǎn)
         */
        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;
        }
        
        /**
         * 插入某個(gè)值
         * @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;     //作為插入結(jié)點(diǎn)的父結(jié)點(diǎn)
                
                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();
        }

    }



    只有注冊用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲天堂福利视频| 亚洲一区二区三区乱码在线欧洲| 国产精品99精品久久免费| 亚洲黄色在线观看| 爽爽日本在线视频免费| 99re6在线视频精品免费| 亚洲乱码在线视频| 亚洲AV无码乱码精品国产| 久久大香香蕉国产免费网站| 亚洲AV无码一区二区三区网址| 亚洲日韩欧洲乱码AV夜夜摸| 最新中文字幕免费视频| 成年免费a级毛片免费看无码| 亚洲一区无码中文字幕乱码| 91麻豆精品国产自产在线观看亚洲 | 91精品免费高清在线| 爱情岛论坛亚洲品质自拍视频网站| 亚洲人成伊人成综合网久久久| 动漫黄网站免费永久在线观看| jizz免费观看视频| 亚洲欧美日本韩国| 亚洲网址在线观看你懂的| 国产男女猛烈无遮挡免费视频| 小草在线看片免费人成视久网| 特级毛片爽www免费版| 亚洲免费闲人蜜桃| 久久久久亚洲Av片无码v| 免费女人18毛片a级毛片视频| 69av免费视频| 日本视频免费高清一本18| 猫咪免费人成在线网站| 一本色道久久88—综合亚洲精品 | 亚洲一线产品二线产品| 国产V片在线播放免费无码| 国内免费高清在线观看| 亚洲人成网站免费播放| 亚洲AV无码专区在线电影成人 | 亚洲色大18成人网站WWW在线播放| 亚洲国产a∨无码中文777| 亚洲国产成人精品久久久国产成人一区二区三区综 | a级片在线免费看|