简体   繁体   中英

BST String Traversal

I'm trying to implement an inorder, preorder, and postorder traversal algorithm for my BST. It seems that the inorder algorithm is working so far, but it's only returning the first character of the word. I realize that I'm returning char c , but I'm confused on how I would make it return the entire word. Any help would be highly appreciated!

package main;

import java.io.*;
import java.util.*;

// Node class
class Node
{
    char c;
    boolean end; 
    Node left, right;

    public Node(char c)
    {
        this.c = c;
        this.end = false;
        this.left = null;
        this.right = null;

    }          
}


class BinarySearchTree
{
    private Node root;

    public BinarySearchTree()
    {
        root = null;
    }

    public void addValue(String s)
    { 
            root = addValue(root, s, 0); 
    }

    private Node addValue(Node x, String s, int d)
    {
            char c = s.charAt(d);
            if (x == null) 
        x = new Node(c);
            if (c < x.c) 
        x.left = addValue(x.left, s, d);
            else if (c > x.c) 
        x.right = addValue(x.right, s, d);
            else x.end = true;
                return x;
    }

    public boolean search(String s)
    { 
            return search(root, s, 0); 
    }

    private boolean search(Node x, String s, int d)
    {
            if (x == null) 
        return false;

            char c = s.charAt(d);

            if (c < x.c) 
                return search(x.left, s, d);   
            else if (c > x.c) 
                return search(x.right, s, d);
            else 
                return x.end;

    }

        public void inorder(){
            inorder(root);
        }

        private void inorder(Node r){
            if (r != null){
                inorder(r.left);
                System.out.print(r.c);
                inorder(r.right);
            }
        }
} 

public class Project2
{
    public static void main(String[] args) throws Exception
    {
        BinarySearchTree tree  = new BinarySearchTree(); // Make new BST object

        // Variable for scanned word
        String token = "";

        // Use scanner for input file
        Scanner scan = new Scanner(new File("10words.txt")).useDelimiter("\\s+"); 

        //Check for next line in text file
        while (scan.hasNext()) 
        {
            token = scan.next();
            tree.addValue(token);
        }
        scan.close();

        tree.inorder();

        Scanner inputWord = new Scanner(System.in);
        System.out.print("\nEnter a word to search: ");
    String word = inputWord.next().toLowerCase();

    if(tree.search(word) == false){
            System.out.println("Word NOT Found!");
        } else{
            System.out.println("Word Found!");
        }
        inputWord.close();
    }
}

I did not look at your inorder code but you said it works so I'll believe you.

However I did take a look at your addValue code and you only save the first character here. No wonder you can't get the entire word back, you just don't save it :P

Instead, you should change your Node class to accept a String instead of a character. You won't need the d parameter in your addValue method then either.

The Java String class provides the .compareTo () method in order to lexicographically compare Strings. You can use it with string.compareTo(otherString)

This method will return a value < 0, equal to 0 or > 0.

< 0 means string is lexicographically smaller than otherString (for example: "Apple" would be smaller than "Banana")

= 0 means it's the exact same word

> 0 means string is bigger than otherString (for example: "Banana" would be bigger than "Apple")

your addValue method looks like it is incorrect. it only modifies the root, then character will be equal, so it returns. In particular d is never modified.

On a more fondamental level, a BST would be appropriate to look for a character in a tree, not for looking for a String. If you want to look for a word, you can use a Trie instead (which is not a binary tree).

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