Question

I am in the process of creating a binary tree for a project I've been working on where I insert people into a binary tree by name (the tree iterates through each character to determine which is bigger when inserting). Is there a way to make my tree search through the tree to find a person who match the name given to the program. This is my code so far

lass Node {
private String person;
private Node left;
private Node right;

public Node(String person) {
    this.person = person;
    left = null;
    right = null;
}

//setters
protected void setLeft(Node left) {
    this.left = left;
}

protected void setRight(Node right) {
    this.right = right;
}

//getters
protected String getPerson() {
    return person;
}

protected Node getLeft() {
    return left;
}

protected Node getRight() {
    return right;
}
}

public class BinaryTree {
private static Node root; 

public BinaryTree() {
    root = null;
}

public void insert(String person) {
    root = insert(person, root);
}

//Check if node is leaf
public static boolean isLeaf() {
    if(root.getLeft() == null && root.getRight() == null)
        return false;
    else
        return true;
}

// Search tree for entered value

public static void searchTree(String search, Node tNode) {
// Not sure what to put into the part to make the method search through people in the tree
    }

    private Node insert(String person, Node tree) {
    if(tree == null)
        tree = new Node(person);
    else {
        int count = 1;
        int x = 0;
        while(person.toLowerCase().charAt(x) == tree.getPerson().toLowerCase().charAt(x) && count != tree.getPerson().length()) {
            count = count + 1;
            x = x + 1;
        }
        if(person.toLowerCase().charAt(x) != tree.getPerson().toLowerCase().charAt(x)) {
            if(person.toLowerCase().charAt(x) < tree.getPerson().toLowerCase().charAt(x))
                tree.setLeft(insert(person, tree.getLeft()));
            else
                tree.setRight(insert(person, tree.getRight()));
        } else {
            tree.setRight(insert(person, tree.getRight()));
        }
    }
    return tree;

}

Can you please suggest how I should create a method to search through the tree

Was it helpful?

Solution

I Would suggest you to do these steps. These steps will give you a start.

  1. Start from root and compare the Name to be searched with root using compareToIgnoreCase().
  2. Depending upon the result move left or right.
  3. Continue till either node becomes null or you find a match.

OTHER TIPS

If you're trying to implement a binary search tree, you need to change the code in your setters to determine whether to add a person to the left or right node (by comparing the strings lexicographically) every time those methods are called. If the tree is not ordered, you'd have to search every node. When it's ordered, you'll be able to search in log n time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top