【发布时间】:2021-05-02 15:02:35
【问题描述】:
我必须用一些给定的接口函数来实现一个 AVL 树。到目前为止,一切似乎都很好,除非我在主函数“tree.search(3)”中调用,例如,它根本不起作用,只返回一个值或 null。有谁知道为什么会这样?
AVLTree:
public class AVLTree implements SearchTree{
public class Node {
int key;
int height;
Node left;
Node right;
String string;
Node(int key, String _string) {
this.key = key;
this.string = _string;
}
}
public static void main(String[] args) {
AVLTree tree = new AVLTree();
tree.insert(5, "gambo");
tree.insert(9, "3");
tree.insert(3, "3");
System.out.println(tree.search(9));
}
private Node root;
public Node find(int key) {
Node current = root;
while (current != null) {
if (current.key == key) {
break;
}
current = current.key < key ? current.right : current.left;
}
return current;
}
// public void insert(int key) {
// root = insert(root, key);
// }
public void delete(int key) {
root = delete(root, key);
}
public Node getRoot() {
return root;
}
public int height() {
return root == null ? -1 : root.height;
}
private Node insert(Node node, int key, String string) {
if (node == null) {
System.out.println("yep new node");
return new Node(key, string);
} else if (node.key > key) {
node.left = insert(node.left, key, node.string);
} else if (node.key < key) {
node.right = insert(node.right, key, node.string);
} else {
throw new RuntimeException("duplicate Key!");
}
return rebalance(node);
}
private Node delete(Node node, int key) {
if (node == null) {
return node;
} else if (node.key > key) {
node.left = delete(node.left, key);
} else if (node.key < key) {
node.right = delete(node.right, key);
} else {
if (node.left == null || node.right == null) {
node = (node.left == null) ? node.right : node.left;
} else {
Node mostLeftChild = mostLeftChild(node.right);
node.key = mostLeftChild.key;
node.right = delete(node.right, node.key);
}
}
if (node != null) {
node = rebalance(node);
}
return node;
}
private Node mostLeftChild(Node node) {
Node current = node;
while (current.left != null) {
current = current.left;
}
return current;
}
private Node rebalance(Node z) {
updateHeight(z);
int balance = getBalance(z);
if (balance > 1) {
if (height(z.right.right) > height(z.right.left)) {
z = rotateLeft(z);
} else {
z.right = rotateRight(z.right);
z = rotateLeft(z);
}
} else if (balance < -1) {
if (height(z.left.left) > height(z.left.right)) {
z = rotateRight(z);
} else {
z.left = rotateLeft(z.left);
z = rotateRight(z);
}
}
return z;
}
private Node rotateRight(Node y) {
Node x = y.left;
Node z = x.right;
x.right = y;
y.left = z;
updateHeight(y);
updateHeight(x);
return x;
}
private Node rotateLeft(Node y) {
Node x = y.right;
Node z = x.left;
x.left = y;
y.right = z;
updateHeight(y);
updateHeight(x);
return x;
}
private void updateHeight(Node n) {
n.height = 1 + Math.max(height(n.left), height(n.right));
}
private int height(Node n) {
return n == null ? -1 : n.height;
}
public int getBalance(Node n) {
return (n == null) ? 0 : height(n.right) - height(n.left);
}
@Override
public void insert(int key, String string) {
root = insert(root, key, string);
}
@Override
public String search(int key) {
Node p = find(key);
return p.string.toString();
}
@Override
public long getCumulativeLengthOfSearchPaths() {
return 0;
}
}
界面:
/**
* Organizes strings with keys in a search tree and collects some statistics
*/
public interface SearchTree {
/**
* Inserts a string with a given key into the tree. The position in the tree
* depends on the rank of the key with respect to all existing keys in the tree.
* Each key is to appear in the tree at most once. Hence, this method will not
* alter the tree if the key already exists in the tree.
*
* @param key the key of the string to be inserted
* @param string the string to be inserted
*/
public void insert(int key, String string);
/**
* Deletes a string with a given key from the tree.
*
* @param key the key of the string to be deleted
*/
public void delete(int key);
/**
* Searches for a string with a given key in the tree and returns the string if
* it exists.
*
* @param key the key of the string to be searched for
* @return the string to be searched for, null if the key does not exist in the
* tree
*/
public String search(int key);
public long getCumulativeLengthOfSearchPaths();
}
【问题讨论】: