java語言實現樹

來源:互聯網
上載者:User

標籤:etl   資料   先序   wot   div   oid   return   rda   style   

首先用Node類定義一個節點,用來儲存每個節點的內容:

public class Node {    // 關鍵字    private int keyData;        // 其他資料    private int otherData;        // 左子節點    private Node leftNode;        // 右子節點    private Node rightNode;    public Node(int keyData, int otherDate) {        this.keyData = keyData;        this.otherData = otherDate;    }    public int getKeyData() {        return keyData;    }    public void setKeyData(int keyData) {        this.keyData = keyData;    }    public Node getLeftNode() {        return leftNode;    }    public void setLeftNode(Node leftNode) {        this.leftNode = leftNode;    }    public int getOtherData() {        return otherData;    }    public void setOtherData(int otherData) {        this.otherData = otherData;    }    public Node getRightNode() {        return rightNode;    }    public void setRightNode(Node rightNode) {        this.rightNode = rightNode;    }    // 顯示方法    public void display(){        System.out.println(keyData + "," + otherData);    }        }

然後定義一個Tree類,並用前序走訪、中序遍曆和後序遍曆

public class Tree {    // 根    private Node root;    // 插入方法    public void insert(int keyData, int otherData) {        Node newNode = new Node(keyData, otherData);        // 如果說沒有節點        if (root == null) {            root = newNode;        } else {            Node current = root;            Node parent;            while (true) {                parent = current;                if (keyData < current.getKeyData()) {                    current = current.getLeftNode();                    if (current == null) {                        parent.setLeftNode(newNode);                        return;                    }                } else {                    current = current.getRightNode();                    if (current == null) {                        parent.setRightNode(newNode);                        return;                    }                }            }        }    }    // 尋找方法    public Node find(int keyData) {        Node current = root;        while (current.getKeyData() != keyData) {            if (keyData < current.getKeyData()) {                current = current.getLeftNode();            } else {                current = current.getRightNode();            }            if (current == null) {                return null;            }        }        return current;    }    // 修改方法    public void change(int keyData, int newOtherData) {        Node findNode = find(keyData);        findNode.setOtherData(newOtherData);    }    // 先序遍曆    public void preOrder(Node node) {        if (node != null) {            node.display();            preOrder(node.getLeftNode());            preOrder(node.getRightNode());        }    }    // 中序遍曆    public void inOrder(Node node) {        if (node != null) {            inOrder(node.getLeftNode());            node.display();            inOrder(node.getRightNode());        }    }    // 後序遍曆    public void endOrder(Node node) {        if (node != null) {            endOrder(node.getLeftNode());            endOrder(node.getRightNode());            node.display();        }    }    public Node getRoot() {        return root;    }}

測試:

        Tree tree = new Tree();        tree.insert(80, 80);        tree.insert(49, 49);        tree.insert(42, 42);        tree.insert(30, 30);        tree.insert(45, 45);        tree.insert(90, 90);        tree.insert(150, 150);        tree.insert(130, 130);        tree.insert(82, 82);        System.out.println("前序走訪:");        tree.preOrder(tree.getRoot());        System.out.println("中序遍曆:");        tree.inOrder(tree.getRoot());        System.out.println("後序遍曆:");        tree.endOrder(tree.getRoot());

 

java語言實現樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.