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

    }



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


    網站導航:
     
    主站蜘蛛池模板: 亚洲六月丁香六月婷婷蜜芽| 成人亚洲综合天堂| 亚洲AV无码AV男人的天堂| www在线观看播放免费视频日本| 国产公开免费人成视频| 国产亚洲Av综合人人澡精品| 国产在线观看免费不卡| 污污的视频在线免费观看| 亚洲国产成人久久综合野外| jizz免费观看视频| 亚洲乱码中文字幕综合| 免费无码作爱视频| 精品亚洲麻豆1区2区3区| 西西大胆无码视频免费| 亚洲乱亚洲乱妇24p| 亚洲精品成人在线| 午夜免费福利视频| 2020亚洲男人天堂精品| 国产亚洲福利一区二区免费看| 一级成人毛片免费观看| 亚洲av女电影网| 在线观看免费人成视频色| 亚洲AV永久无码精品放毛片| 亚洲精品无码久久毛片| 久久99精品国产免费观看| 久久亚洲最大成人网4438| 又爽又黄无遮挡高清免费视频 | 亚洲色图综合在线| 麻豆精品不卡国产免费看| 亚洲av永久无码精品天堂久久| 日韩毛片无码永久免费看| 抽搐一进一出gif免费视频| 亚洲精品视频在线观看免费| 国产小视频在线观看免费| 日本三级在线观看免费| 亚洲国产人成在线观看| 亚洲日韩中文字幕日韩在线| 免费能直接在线观看黄的视频| 国产精品亚洲片在线花蝴蝶| 亚洲嫩草影院久久精品| 日韩一级免费视频|