Elementary Symbol Tables
Java autoboxing and equals(). Consider two ?????? values ? and ? and their corresponding ?????? values ? and ?.
- Find values such that (?==?) is ???? but ?.??????(?) is ?????.
- Find values such that (?==?) is ????? but ?.??????(?) is ????.
Hint: IEEE floating point arithmetic has some peculiar rules for ?.?, −?.?, and ???. Java requires that ??????() implements an equivalence relation.
根据提示,再查看Double的源码:
/* <p>also has the value {@code true}. However, there are two
* exceptions:
* <ul>
* <li>If {@code d1} and {@code d2} both represent
* {@code Double.NaN}, then the {@code equals} method
* returns {@code true}, even though
* {@code Double.NaN==Double.NaN} has the value
* {@code false}.
* <li>If {@code d1} represents {@code +0.0} while
* {@code d2} represents {@code -0.0}, or vice versa,
* the {@code equal} test has the value {@code false},
* even though {@code +0.0==-0.0} has the value {@code true}.
* </ul>
*/
public boolean equals(Object obj) {
return (obj instanceof Double)
&& (doubleToLongBits(((Double)obj).value) ==
doubleToLongBits(value));
}
public static long doubleToLongBits(double value) {
if (!isNaN(value)) {
return doubleToRawLongBits(value);
}
return 0x7ff8000000000000L;
}
满足第一种情况的是 a = +0.0 b = -0.0 (两者虽然值相等,但在equals()时会将符号也转化)
满足第二种情况的是 a = b = NaN (NaN不可直接比较,但doubleToLongBits()返回相同的值)
Check if a binary tree is a BST. Given a binary tree where each ???? contains a key, determine whether it is a binary search tree. Use extra space proportional to the height of the tree.
第一时间想到递归判断左结点小于根结点、右结点大于根结点,但这种做法是错误的,因为可能出现以下这种情况(因为只考虑了左子树根结点小于根节点,实际上要保证左子树所有结点都小于根节点,右子树同理):
[外链图片转存失败(img-9mfwwzEX-1564728147953)(assets/tree_bst.gif)]
又由BST的性质-中序遍历必然得到非递减序列,想到可以通过中序遍历给定二叉树,判断是否存在逆序对来判断是否是BST,但空间复杂度为O(N)不符合要求。
空间占用更小的方法:对第一种方法进行改进,加上最小值、最大值的限定,使得当前结点都必须在[min, max]的范围里,空间复杂度为O(logN)。
private Node root;
public boolean isBST() {
return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private boolean isBST(Node x, int min, int max) {
if (x == null) {
return true;
}
if (x.key < min || x.key > max) {
return false;
}
return isBST(x.left, min, x.key) && isBST(x.right, x.key, max);
}
private class Node {
private int key;
private Node left, right;
public Node(int key) {
this.key = key;
}
}
Inorder traversal with constant extra space. Design an algorithm to perform an inorder traversal of a binary search tree using only a constant amount of extra space.
参考 Inorder Tree Traversal without recursion and without stack!
public void inOrder(Node root) {
Node cur, pre;
if (root == null) {
return;
}
cur = root;
while (cur != null) {
if (cur.left == null) {
System.out.println(cur.key);
cur = cur.right;
} else {
pre = cur.left;
while (pre.right != null && pre.right != cur) {
pre = pre.right;
}
if (pre.right == null) {
pre.right = cur;
cur = cur.left;
} else {
pre.right = null;
System.out.println(cur.key);
cur = cur.right;
}
}
}
}
Web tracking. Suppose that you are tracking nn web sites and mm users and you want to support the following API:
- User visits a website.
- How many times has a given user visited a given site?
What data structure or data structures would you use?
用BST实现的Symbol Table。
参考
A program to check if a binary tree is BST or not
[Inorder Tree Traversal without recursion and without stack!](