Question

Write a program to use a binary search tree to store a list of computer games....

Write a program to use a binary search tree to store a list of computer games. Each node in the tree stores the title (string) of a computer game. Different games have different titles.
Your program should display the following menu repeatedly:
1. Insert new game
2. Search for games         
3. List games
4. quit
Option 1 should read a game (title) and add the game into the tree. 
Option 2 allows the user to enter a partial key (a prefix of key) and displays all the games whose titles match the partial key.
Option 3 list games using the breadth-first traversal

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA CODE:

import java.util.Queue;
import java.util.LinkedList;
import java.io.*;
class GameTree {
  
/* Class containing left and right child of current node and key value*/
class Node {
String key;
Node left, right;
  
public Node(String item) {
key = item;
left = right = null;
}
}
  
// Root of BST
Node root;
  
// Constructor
GameTree() {
root = null;
}
  
// This method mainly calls insertRec()
void insert(String key) {
root = insertRec(root, key);
}
  
/* A recursive function to insert a new key in BST */
Node insertRec(Node root, String key) {
  
/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(key);
return root;
}
  
/* Otherwise, recur down the tree */
if (key.compareTo(root.key)<0)
root.left = insertRec(root.left, key);
else if (key.compareTo(root.key)>0)
root.right = insertRec(root.right, key);
  
/* return the (unchanged) node pointer */
return root;
}

void GameSearch(String search){
SearchUtil(root,search);
}
  
void SearchUtil(Node root,String search) {
if (root != null) {
SearchUtil(root.left,search);
int length = search.length();
if(search.compareToIgnoreCase(root.key.substring(0,length))==0)
System.out.println(root.key);
SearchUtil(root.right,search);
}
}
void BFS()
{
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty())
{
Node tempNode = queue.poll();
System.out.println(tempNode.key + " ");
  
/*Enqueue left child */
if (tempNode.left != null) {
queue.add(tempNode.left);
}
  
/*Enqueue right child */
if (tempNode.right != null) {
queue.add(tempNode.right);
}
}
}
// Driver Program to test above functions
public static void main(String[] args)throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
GameTree tree = new GameTree();   
  
int choice;String name,search;
while(true){
System.out.println("1. Insert New Game");
System.out.println("2. Search For Games");
System.out.println("3. List Games");
System.out.println("4. Quit");
System.out.println("Enter Choice: ");
choice = Integer.parseInt(in.readLine());
if(choice == 4){
break;
}
switch(choice){
case 1:
System.out.println("Enter Name of the Game: ");
name = in.readLine();
tree.insert(name);
break;
case 2:
System.out.println("Enter Search String: ");
search = in.readLine();
System.out.println("Search Results:");
tree.GameSearch(search);
break;
case 3:
System.out.println("List of Games:");
tree.BFS();
System.out.println();
break;
}
  
}
}
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a program to use a binary search tree to store a list of computer games....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A Binary Search Tree is a binary tree where nodes are ordered in the following way:...

    A Binary Search Tree is a binary tree where nodes are ordered in the following way: each node contains one key (also known as data) the keys in the left subtree are less than the key in its parent node the keys in the right subtree are greater than the key in its parent node duplicate keys are not allowed Create a Binary Search Tree inserting the following list of numbers in order from left to right. 10,          6,           4,            8,            18,          15,          21 Please type...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • Write a C++ program to store and update students' academic records in a binary search tree....

    Write a C++ program to store and update students' academic records in a binary search tree. Each record (node) in the binary search tree should contain the following information fields:               1) Student name - the key field (string);               2) Credits attempted (integer);               3) Credits earned (integer);               4) Grade point average - GPA (real).                             All student information is to be read from a text file. Each record in the file represents the grade information on...

  • This is binary search tree problem. The program reads the text file, and creates a binary...

    This is binary search tree problem. The program reads the text file, and creates a binary search tree based on the words in the file. I can create the tree but I also have to store 'the order of insertion' in each node.   For example, if text includes "the" 3 times and it is the 1st, 5th, and 9th word in the file, in binary search tree, one node should have string value "the" and array list{1, 5, 9}. How...

  • e Construct a binary search tree (BST) using the following array elements 60,63,15, 81,30,74,35,38,93,41,53,45,86,90). e For...

    e Construct a binary search tree (BST) using the following array elements 60,63,15, 81,30,74,35,38,93,41,53,45,86,90). e For the above BST, show the use of post-order traversal to delete node 53. . For the above BST, show the path to search the node 86 and to insert a node with key

  • You have to use Binary Tree ADT. Problem Use Java as Programming Language Define an ESports...

    You have to use Binary Tree ADT. Problem Use Java as Programming Language Define an ESports World Cup class that implements the E-Games World Cup using a Binary Tree ADT. The external nodes of the tree correspond to each competing player. The internal nodes correspond to games played at different rounds of the cup (i.e. root of the tree is the final game, its children are the semifinals, etc.). Two players compete against each other and the winner goes to...

  • Q8 - Construct a Binary Search Tree by inserting the following sequence of numbers. 5,6,3,2,10,4,1,7,9,8. Write...

    Q8 - Construct a Binary Search Tree by inserting the following sequence of numbers. 5,6,3,2,10,4,1,7,9,8. Write down the level ordered traversal sequence of the final tree after insert. Delete node 10, 8, and 6 step by step using in-order successor. Write down the level ordered traversal sequence after every delete. I want you to write down (1 level ordered traversal for the insert and 3 level-ordered traversals for the deletes). In total there should be 4 level-ordered traversal sequences in...

  • 13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program...

    13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program which you can store a word with its definition. Each node of the tree will contain the word and definition. The word is what will be used as the key to sort our data. The dictionary should allow you to search for a word. If the word exist then the definition will display. You can also add words to the dictionary. For testing you...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT