简体   繁体   中英

How to find a value in a binary tree

I have a binary tree like the picture below, I want to implement a method called findNode to return the node holding the value entered as a parameter. For example: findNode(8)=8 , findNode(13)=13 . I tried to modify this code but it didn't working :

    class Node {
        Node left, right;
        int value;

        public Node findNode(int value) {
            Node focusNode = root;
            if (focusNode == null) {
                return null;
            }
            while (focusNode.value != value) {
                // If we should search to the left
                if (value < focusNode.value) {
                    // Shift the focus Node to the left child
                    focusNode = focusNode.left;
                } else {
                    // Shift the focus Node to the right child
                    focusNode = focusNode.right;
                }
                return focusNode;
            }
        }
    }

树示例

See if this helps. You can't default to left or right but need to check them explicitly. The null situation is taken care of by the while loop.

    public Node findNode(int value) {
        Node focusNode = root;
        while (focusNode != null) {
            // If we should search to the left
            if (value < focusNode.value) {
                // Shift the focus Node to the left child
                focusNode = focusNode.left;
            } else if (value > focusNode.value) {
                // Shift the focus Node to the right child
                focusNode = focusNode.right;
            } else {
                // Found!!
                return focusNode;
            }
        }
        return null;
    }

Here's a cleaner approach:

   class Node {
        Node left, right;
        int value;

        public static Node findNode(Node current, int value) {
            if (current == null)
                return null;
            if (current.value == value)
                return current;
            if (current.value < value) 
                return findNode(current.left, value);
            else 
                return findNode(current.right, value);
        }
    }

your root is either not defined or null every time. below code would work as expected.

class Node
{
    int value;
    Node left, right;

    public Node(int item)
    {
        value = item;
        left = right = null;
    }
}


public class BinaryTree {
    Node root;

    public Node findNode(int value) {
        Node focusNode = root;
        if (focusNode == null){
            return null;
        }

        while (focusNode.value!= value) {

            // If we should search to the left

            if (value< focusNode.value) {

                // Shift the focus Node to the left child

                focusNode = focusNode.left;

            } else {

// Shift the focus Node to the right child

                focusNode = focusNode.right;

            }


        }

        return focusNode;

    }

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