簡體   English   中英

這個算法中可變val的目的是什么

[英]what's the purpose of variable val in this algo

這是互聯網上有關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

是的,您是對的,就像商標一樣。 我們確實可以僅使用一個變量(即key來實現此算法。 在這種算法中, val是作為一種數據類型存儲的,我們需要對其進行跟蹤。

例如考慮一下

您有一些編號的框,例如34、23、65、73,並且您想在它們上執行RB Tree操作。 因此,這些框上的數字類似於您算法中的key

現在考慮每個盒子中包含許多球。 這些球可以看作是存儲在盒子內的數據,您需要對其進行跟蹤。 因此,可以將其視為val

您甚至可以更進一步,通過將val的數據類型更改為ListMap或什至用戶定義的對象來跟蹤盒子中的幾件事。 它將仍然以相同的方式工作。

在此實現中,此數據結構的作用類似於Map ,這意味着它將鍵映射到值。 val是對value的普遍貶低,這是不言而喻的。 它可以是Java中存在的任何類型,無論是原始的還是引用的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM