简体   繁体   中英

Find a node in an AVL tree iteratively

do somebody have a clue how I can search for a specific node in an AVL tree iteratively? I mean, with the complexity of the height of the tree. I wrote a bit of code, but I actually don't know how to search for the node iteratively (recursively wouldn't be a problem):

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{

thanks in beforehand :)

An AVL tree is just a special kind of binary search tree, and you can search it the same way:

We begin by examining the root node. If the tree is null, the key we are searching for does not exist in the tree. Otherwise, if the key equals that of the root, the search is successful and we return the node. If the key is less than that of the root, we search the left subtree. Similarly, if the key is greater than that of the root, we search the right subtree. This process is repeated until the key is found or the remaining subtree is null. If the searched key is not found before a null subtree is reached, then the key is not present in the tree.

For your implementation, that would look something like this:

public boolean contains(E value) {
    Node current = root;

    while (current != null) {
        int comparison = value.compareTo(current.value);
        if (comparison == 0) {
            return true;
        } else if (comparison < 0) {
            current = current.left;
        } else { //comparison > 0
            current = current.right;
        }
    }

    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM