有人知道如何迭代搜索AVL树中的特定节点吗?我的意思是,随着树高的复杂性。我写了一些代码,但实际上我并不知道如何迭代地搜索节点(递归地不会成为问题):
public class AuDTree<E extends Comparable<? super E>> implements AuDTreeInterface<E> {
private class Node{
public Node left;
public Node right;
public Node parent;
public E value;
public Node(E value){
left = null;
right = null;
parent = null;
this.value = value;
}
}
public Node root;
public AuDTree(){
this.root = null;
}
@Override
public boolean isEmpty() {
if(root == null){
return true;
}
else{
return false;
}
}
@Override
public int count() {
return count(root);
}
private int count(Node n) {
if (n == null) {
return 0;
}
if (n.left == null && n.right == null) {
return 1;
} else {
return count(n.left) + count(n.right);
}
}
@Override
public int getHeight() {
return getHeight(root);
}
private int getHeight(Node n) {
if (n == null) {
return -1;
}
return Math.max(getHeight(n.left), getHeight(n.right)) + 1;
}
@Override
public boolean isAVLTree() {
return isAVLTree(root);
}
public boolean isAVLTree(Node n){
Node prev = null;
if(root == null)
{
if(!isAVLTree(root.left))
return false;
if(prev != null && root.value.compareTo(prev.value) <= 0)
return false;
prev = root;
return isAVLTree(root.right);
}
return true;
}
@Override
public boolean contains(E value) {
if(isEmpty()) return false;
else if(value == root) return true;
else{
事先感谢:)
答案 0 :(得分:2)
intended pattern只是一种特殊的二叉搜索树,你可以AVL tree以同样的方式:
我们首先检查根节点。如果树为null,则我们搜索的密钥在树中不存在。否则,如果密钥等于根的密钥,则搜索成功,我们返回节点。如果密钥小于根密钥,我们搜索左子树。同样,如果密钥大于根密钥,我们搜索正确的子树。重复此过程,直到找到密钥或剩余子树为空。如果在到达空子树之前未找到搜索到的键,则该树中不存在该键。
对于您的实现,看起来像这样:
$string = 'hey how are you <img src="example.com/image.png"> test test <img src="example.com/image2.png">';
preg_match_all('/(<img .*?>)/', $string, $img_tag);
print_r($img_tag[1]);