Java 什么';在这个算法中变量val的作用是什么
这是一个来自互联网的关于红黑BST Java实现的算法。我对这个程序中名为Java 什么';在这个算法中变量val的作用是什么,java,algorithm,binary-search-tree,red-black-tree,Java,Algorithm,Binary Search Tree,Red Black Tree,这是一个来自互联网的关于红黑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
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));
}
}
打包工具;
公共类红黑2{
私有静态最终布尔红=真;
私有静态最终布尔黑=假;
私有节点根;
公共redBlack2(){}
私有类节点{
私钥;
私人国际旅行社;
私有节点左、右;
私有布尔色;
公共节点(int键、int值、布尔颜色){
this.key=key;
this.val=val;
这个颜色=颜色;
}
}
专用布尔isRed(节点x){
如果(x==null)返回false;
返回x.color==红色;
}
公共整数获取(整数密钥){
返回get(root,key);
}
私有int-get(节点x,int-key){
while(x!=null){
如果(键x.key)x=x.right;
否则返回x.val;
}
System.out.println(“没有这样的键”);
返回0;
}
公共布尔包含(int键){
返回get(键)!=0;
}
公共作废put(整数键,整数值){
root=put(root,key,val);
根颜色=黑色;
}
专用节点put(节点h,int-key,int-val){
如果(h==null)返回新节点(key,val,RED);
如果(键h.key)h.right=put(键h.right,key,val);
如果(key==h.key)h.val=val,则为else;
如果(isRed(h.right)和&!isRed(h.left))h=rotateLeft(h);
if(isRed(h.left)和&isRed(h.left.left))h=旋转右(h);
if(isRed(左)和isRed(右)翻转颜色(h);
返回h;
}
专用节点rotateRight(节点h){
节点x=h.左;
h、 左=右;
x、 右=h;
x、 颜色=x.right.color;
x、 右。颜色=红色;
返回x;
}
私有节点rotateLeft(节点h){
节点x=h.右;
h、 右=x.左;
x、 左=h;
x、 颜色=x.left.color;
x、 左。颜色=红色;
返回x;
}
私有void FlipColor(节点h){
h、 颜色=!h.color;
h、 left.color=!h.left.color;
h、 right.color=!h.right.color;
}
公共静态void main(字符串[]args){
redBlack2 r=新的redBlack2();
r、 put(34,1);
r、 put(23,2);
r、 put(65,3);
r、 put(73,4);
系统输出打印(r.get(73));
}
}
这只是我们放在树上的数字的标记吗?那我们不是已经有钥匙做记号了吗?为什么我们仍然需要变量
val
?在这个实现中,这个数据结构就像一个映射
,这意味着它将键映射到值。val
是value
的一个常见缩写,这是非常自我解释的。它可以是Java中存在的任何类型,无论是原语还是引用。是的,你说得对,它就像一个标记。实际上,我们只需一个变量即可实现该算法,即key
。在这个算法中,val
是一种存储为我们需要跟踪的数据类型的东西
例如,考虑这个
您有几个编号的框,如34、23、65、73,您希望
对它们执行RB树操作。盒子上的数字是多少
类似于算法中的键
现在考虑每个盒子里都有一些球。这些球
可以看作是存储在盒子内的数据,您需要
跟踪它。因此,这可以被视为val
您甚至可以更进一步,通过将
val
的数据类型更改为List
或Map
甚至用户定义的对象,来跟踪一个框中的一些内容。它仍然会以同样的方式工作。假设有人给你一本你从未见过的字典。你决定查一个词。当你找到这个词时,字典不会告诉你任何关于它的信息——没有发音,没有定义,没有任何东西,只有这个词。你知道的并不比以前多(除了你知道这个词在字典里,有一点,但不多)。如果有一本词典能提供更多的信息,这不是更有用吗?这有助于解释val
的用途吗?val
存储调用put(int-key,int-val)
中给出的值,然后由get(int-key)
返回。为什么您认为键
的值与val
的值相同?如果您只需按照代码查看值的来源和使用位置,答案就是这样。类似地,Java提供了Set
和Map
集合。集合
可以查找钥匙并告诉您钥匙是否在集合中,但它不能告诉您更多信息。Map
允许您提供与密钥相关的附加数据。这有助于解释吗?