Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7819541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:59:21+00:00 2026-06-02T06:59:21+00:00

I am creating a binary search tree using Nodes with MovieInfo objects as keys.

  • 0

I am creating a binary search tree using Nodes with MovieInfo objects as keys. MovieInfo objects are objects with three fields: ID, fullName, and shortName. The binary search tree will store information on an input text file containing every movie listed on IMDB. The insert would be based on an ID randomly associated with each movie. The search features would have a user input a String and pull up the other data of the object (shortName, ID, etc).
Now — the errors I am having are with the findExact method.
First, no matter what my input is — I get the message “Current Node is null” which means my starting currentNode is ALWAYS going null. The other issue is, I am not sure how to make sure my root node (the first currentNode searched for in the tree) is initialized properly. I have a feeling this might have something to do with my problem.
The other problem might lie in how I am inserting the nodes in the first place.
And for reference, the way I am testing this code/running in the text input file is IndexTester.java.

UPDATE: Okay. Everything works now. The only issue I am having now is that my findExact method seems to change the ID field of the MovieInfo class. So my search does not end up working. For example, if I input:

1568482 2 White Kids with Problems  2 White Kids with Problems
1568487 Disorient   Disorient
1568488 DIY Playgrounds DIY Playgrounds

and search “disorient,” the return is “1568488 Disorient Disorient” (with the ID for the “2 White Kids with Problems” object). Additionally, since the ID is taken…DIY Playgrounds can’t be searched for succesfully. The insertion method works, but the findExact method is giving me issues. Any ideas as to what might be causing this change in the ID?

The MovieInfo class (separate .java file) — can’t be edited

public class MovieInfo {
    public String shortName;
    public String fullName;
    static int ID;
    public String key;

    public MovieInfo(int id, String s, String f) {
        ID = id;
        shortName = s;
        fullName = f;
    }
}

BSTIndex.java — the class in which I create the internal BST

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

public class BSTIndex extends MovieInfo {

    public Node root;
    public class Node{
        public MovieInfo movie;
        public Node left, right;

    public Node(MovieInfo movie)
    {
        this.movie = movie;
        //this.val = val;
        //this.N = N;
        }
    }

    public BSTIndex()
    /**
     * constructor to initialize the internal binary search tree. 
     * The data element should be an object of the type MovieInfo, described above.
     */
    {   
        super(0, "", "");

    }


    public MovieInfo findExact(String key) {
    return findExact(root, key);
}

private MovieInfo findExact(Node x, String key) {
    if (x == null) return null;
    int cmp = key.compareToIgnoreCase(x.movie.fullName);
    if      (cmp < 0) return findExact(x.left, key);
    else if (cmp > 0) return findExact(x.right, key);
    else              return x.movie;
}

    public void insert(MovieInfo data)
    {
        if (data == null) {return; }
        root = insert(root, data);
    }

    public Node insert(Node x, MovieInfo data)
    {   
        if (x == null) return new Node(data);
        int cmp = data.ID - x.movie.ID;
        if (cmp > 0 )       x.right = insert(x.right, data);
        else if (cmp < 0)   x.left = insert(x.left, data);
        else if (cmp == 0)  x.movie = data;
        return x;
    }
}

IndexTester.java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class IndexTester {

    // Test program
    public static void main( String [ ] args ) throws FileNotFoundException
    {
        BSTIndex t = new BSTIndex( );
        Scanner scan = new Scanner(new FileInputStream(args[0]));
        long start = System.currentTimeMillis();
        int i=0;

        while(scan.hasNext()){
            String line = scan.nextLine();
            String [] fields = line.split("\t");
            int id = Integer.parseInt(fields[0].trim());
            String shortName = fields[1].trim();
            String fullName = fields[2].trim();
            MovieInfo info = new MovieInfo(id, shortName, fullName);

            t.insert(info);
            i++;
            if(i % 10000 == 0){
                System.out.println("Inserted " + i + " records.");
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("Index building complete. Inserted " + i + 
            " records.Elapsed time = " + (end - start )/1000 + " seconds.");

        Scanner input = new Scanner(System.in);

        System.out.println("Enter search string, end in a '*' for 
            prefix search. q to quit");
        while(input.hasNext()){
            String search = input.nextLine().trim();
            if(search.equals("q")) break;
            if(search.indexOf('*')>0){
                //call prefix search. 

                MovieInfo item = t.findPrefix(search);
                if(item==null) System.out.println("Not Found"); 
                else System.out.println(item.ID + " " + item.shortName + " 
                            " + item.fullName);

            }
            else{
                //call exact search, modify to return MovieInfo
                MovieInfo item = t.findExact(search); 
                if(item==null) System.out.println("Not Found");
                else System.out.println(item.ID + " " + item.shortName + " 
                            " + item.fullName);
            }
        }


    }   
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:59:24+00:00Added an answer on June 2, 2026 at 6:59 am

    public class BSTIndex should not extend MovieInfo, preferably MovieInfo should extend Node.

    Ok, so MovieInfo cannot be modified so I would populate the MovieInfo class with the data and set it as the nodevalue of an extended Node class.

    public class BSTNode extends Node {
        private BSTNode left,right;
        private MovieInfo val;
    
        public void setVal(MovieInfo val){
            this.val=val;
        }
    
        public void setLeft(MovieInfo m){
            left=new BSTNode(m);
        }
        public void setRight(MovieInfo m){
            right=new BSTNode(m);
        }
        //override some of the Node methods
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

well i was creating a binary search tree using this code..as far as i
Using a very large raw binary data file, I will be creating a large
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=
In a binary search tree that takes a simple object.....when creating the getter and
Let's say I'm creating a class for a binary tree, BT , and I
I am creating a utility that will store data on flat file in a
I've been coding up a bunch of different binary search tree implementations recently (AVL,
I'm working on creating a binary search algorithm in C that searches for a
I am working with Delphi Prism and creating and writing into binary file using
In binary search tree we have the node strucure as class Node { public:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.