这个算法中变量val的目的是什么

时间:2017-01-13 06:10:41

标签: java algorithm binary-search-tree red-black-tree

它是互联网上关于Red-Black BST Java实现的算法。我对这个程序中名为val的变量感到困惑。这是代码:

package tools;
public class redBlack2 {
    private static final boolean RED   = true;
    private static final boolean BLACK = false;
    private Node root;
    public redBlack2() {}
    private class Node {
        private int key;
        private int val;
        private Node left, right;
        private boolean color;
        public Node(int key, int val, boolean color) {
            this.key = key;
            this.val = val;
            this.color = color;
        }
    }
    private boolean isRed(Node x) {
        if (x == null) return false;
        return x.color == RED;
    }
    public int get(int key) {
        return get(root, key);
    }
    private int get(Node x, int key) {
        while (x != null) {
            if      (key < x.key) x = x.left;
            else if (key > x.key) x = x.right;
            else              return x.val;
        }
        System.out.println("There is no such key.");
        return 0;
    }
    public boolean contains(int key) {
        return get(key) != 0;
    }
    public void put(int key, int val) {
        root = put(root, key, val);
        root.color = BLACK;
    }
    private Node put(Node h, int key, int val) {
        if (h == null) return new Node(key, val, RED);
        if      (key<h.key) h.left  = put(h.left,  key, val);
        else if (key>h.key) h.right = put(h.right, key, val);
        else if (key == h.key)  h.val   = val;
        if (isRed(h.right) && !isRed(h.left))      h = rotateLeft(h);
        if (isRed(h.left)  &&  isRed(h.left.left)) h = rotateRight(h);
        if (isRed(h.left)  &&  isRed(h.right))     flipColors(h);
        return h;
    }
    private Node rotateRight(Node h) {
        Node x = h.left;
        h.left = x.right;
        x.right = h;
        x.color = x.right.color;
        x.right.color = RED;
        return x;
    }
    private Node rotateLeft(Node h) {
        Node x = h.right;
        h.right = x.left;
        x.left = h;
        x.color = x.left.color;
        x.left.color = RED;
        return x;
    }
    private void flipColors(Node h) {
        h.color = !h.color;
        h.left.color = !h.left.color;
        h.right.color = !h.right.color;
    }
    public static void main(String[] args) {
        redBlack2 r = new redBlack2();
        r.put(34,1);
        r.put(23,2);
        r.put(65,3);
        r.put(73, 4);
        System.out.print(r.get(73));
    }
}

这只是我们给树内数字的标记吗?那么我们还没有钥匙作为标记吗?为什么我们仍然需要变量val

2 个答案:

答案 0 :(得分:1)

是的,你是对的,它就像一个标记。我们确实可以用一个变量实现这个算法,即key。在这个算法中,val被存储为一种我们需要跟踪的数据类型。

例如考虑这个

  

你有几个编号的盒子,如34,23,65,73,你想要   对它们实施RB Tree操作。所以这些数字放在盒子上   类似于算法中的key

     

现在考虑每个盒子里面都包含许多球。这些球   可以看作存储在盒子里面的数据,你需要   跟踪它。因此,这可以被视为val

您甚至可以更进一步,通过将val的数据类型更改为ListMap甚至用户 - 来跟踪框内的一些内容定义的对象。它仍然会以同样的方式工作。

答案 1 :(得分:0)

在此实现中,此数据结构的行为类似于val,这意味着它将键映射到值。 value$ sudo service mysql start Job for mysql.service failed. See 'systemctl status mysql.service' and 'journalctl -xn' for details. $ sudo cat /var/log/messages ... Jan 13 04:19:25 localhost mysqld_safe: 2017-01-12T19:19:25.588436Z mysqld_safe Skipping wsrep-recover for empty datadir: /var/lib/mysql Jan 13 04:19:25 localhost mysqld_safe: 2017-01-12T19:19:25.590385Z mysqld_safe Assigning 00000000-0000-0000-0000-000000000000:-1 to wsrep_start_position Jan 13 04:19:27 localhost mysql-systemd: State transfer in progress, setting sleep higher Jan 13 04:19:40 localhost mysqld_safe: 2017-01-12T19:19:40.723030Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended Jan 13 04:19:47 localhost mysql-systemd: /usr/bin/mysql-systemd: 137 行: kill: (19791) - そのようなプロセスはありません Jan 13 04:19:47 localhost mysql-systemd: ERROR! mysqld_safe with PID 19791 has already exited: FAILURE Jan 13 04:19:47 localhost systemd: mysql.service: control process exited, code=exited status=1 Jan 13 04:19:47 localhost mysql-systemd: WARNING: mysql pid file /var/run/mysqld/mysqld.pid empty or not readable Jan 13 04:19:47 localhost mysql-systemd: ERROR! mysql already dead Jan 13 04:19:47 localhost systemd: mysql.service: control process exited, code=exited status=2 ... 的常见缩写,这是非常自我解释的。它可以是Java中存在的任何类型,无论是原始的还是引用的。