zoukankan      html  css  js  c++  java
  • java语言实现树

    首先用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());
  • 相关阅读:
    计算广告(1):计算广告基础
    shell定时任务
    shell脚本运行报错:-bash: xxx: /bin/sh^M: bad interpreter: No such file or directory
    spark文件读取与保存(scala实现)
    spark中的共享变量(广播变量和累加器)
    spark基本概念与运行架构
    用二维数组输出螺旋方阵(JAVA实现)
    InputStream、InputStreamReader和BufferedReader
    Arrays.sort()自定义排序的实现
    BufferedReader和Scanner
  • 原文地址:https://www.cnblogs.com/y3596597/p/6880484.html
Copyright © 2011-2022 走看看