Question

13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program which you can store a word


C++ From Control through Objed
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have provided the code with proper comments and screenshots for proper indentation. If you still have any doubts then please ask in the comments section. I would love to revert back to you.

Code:

------------------------------------------------------------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

// Structure for the Nodes of the tree
struct node {
    // String used for storing word and meaning
    string word, meaning;
    // Pointers to left and right tree
    struct node *left, *right;
};
//Root of the Tree
struct node *root = NULL;
// Helper Function to Create a New Node and return it's pointer
struct node * new_node(string word, string meaning) {
    struct node *temp; // Temporary Node
    temp = new node(); // Allocating the space to the pointer
    temp->word = word; // Assigning the word
    temp->meaning = meaning; // Assigning the meaning
    temp->left = temp->right = NULL; // Setting the left and right as NULL
    return temp; // Returning the pointer to the newly created node
}
// Function to insert the new word into the tree
struct node * insert(struct node* Node,string word,string meaning) {
    // Checking if the current Node on which we are is empty or not
    // If empty then insert the word here only
    if(Node == NULL) return new_node(word,meaning);
    // If the word is smaller than the word of the current node
    // Then insert on the left part of the tree
    if(Node->word > word) Node->left = insert(Node->left,word,meaning);
    // else insert on the right part of the tree
    else if (Node->word < word) Node->right = insert(Node->right,word,meaning);
    // Returning the node after inserting the word
    return Node;  
}
// Function to search a word in the tree
void search(string str) {
    // Temporary node to traverse the tree
    struct node *temp = root;
    // Run the loop until Leaves of the tree are encountered
    // Which are BTW NULL
    while (temp) {
        // Checking if the word is equal to the word of current node
        // And hence output the meaning
        if (temp->word == str) {
                cout << "Word : " << str << endl;
                cout << "Meaning: " << temp->meaning;
                return;
        }
        // Else if the word is smaller than the current node's word
        // Go for the left tree
        else if(temp->word > str) temp = temp->left;
        // Else go for the right tree
        else temp = temp->right;
    }
    // If the word is not found print the same
    cout << "Word Not Found";
    return;
}
// Inorder Traveral of the tree
void inorder_traversal(struct node *Node) {
    // If the node is NULL then return
    if(Node == NULL) return;
    // Traverse the left subtree
    inorder_traversal(Node->left);
    // print the word and meaning of the current Node
    cout << "Word : " << Node->word << " Meaning : " << Node->meaning << endl;
    // Traverse the right subtree
    inorder_traversal(Node->right);
}

//Driver Code to Test the above functions.
int main() {
    int ch;
    string str,mean;
    // Code to take input from a file
    // ifstream inFile;
    // inFile.open("data.txt");
    // if (!inFile) {
    //     cout << "Unable to open file";
    //     exit(1);
    // }
  
    // while (inFile >> str) {
    //     inFile >> mean;
    //     root = insert(root,str,mean);
    // }
    // inFile.close();
    while (1) {
            printf("\n1. Insertion\n2. Searching\n3. Traversal\n4. Exit\n");
            printf("Enter ur choice:");
            cin >> ch;
            switch (ch) {
                    case 1:
                            printf("Word to insert:");
                            cin >> str;
                            printf("Meaning:");
                            cin >> mean;
                            root = insert(root,str, mean);
                            break;
                    case 2:
                            printf("Enter the search word:");
                            cin >> str;
                            search(str);
                            break;
                    case 3:
                            inorder_traversal(root);
                            break;
                    case 4:
                            exit(0);
                    default:
                            printf("You have entered wrong option\n");
                            break;
            }
    }
    return 0;
}

------------------------------------------------------------------------------------------------------------------------------------

Screenshots of the code:

1.

1 #include<bits/stdc++.h> using namespace std; // Structure for the Nodes of the tree struct node { // String used for storin

2.

35 // Function to search a word in the tree void search(string str) { // Temporary node to traverse the tree struct node *tem

3.

//Driver Code to test the above functions. int main() { int ch; string str,mean; // Code to take input from a file // ifstrea

4.

while (1) { printf(\ni. Insertion\n2. Searching\n3. Traversal\n4. Exit\n); printf(Enter ur choice:); cin >> ch; switch (c

Sample Run :

Run 1:

1. Insertion 2. Searching 3. Traversal 4. Exit Enter ur choice: 1 Word to insert:apple Meaning: fruiti 1. Insertion 2. Search

Run 2:

1. Insertion 2. Searching 3. Traversal 4. Exit Enter ur choice:1 Word to insert: apple Meaning: fruiti 1. Insertion 2. Search

For any doubts ask in the comments section I would love to revert back to you.

Happy Learning Data Structures !

Add a comment
Know the answer?
Add Answer to:
13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program...
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
  • 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...

  • Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry...

    Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry in the dictionary is a pair: (word, meaning). Word is a one-word string, meaning can be a string of one or more words (it’s your choice of implementation, you can restrict the meaning to one-word strings). The dictionary is case-insensitive. It means “Book”, “BOOK”, “book” are all the same . Your dictionary application must provide its operations through the following menu (make sure that...

  • 1 Binary Search Trees (25 points) Consider the binary tree as shown in Figure 1. 9...

    1 Binary Search Trees (25 points) Consider the binary tree as shown in Figure 1. 9 5 15 10 17 8 Figure 1: Binary Tree: The letter next to each node (e.g., a, b) denotes the tree node, and the number inside each node is the key. 1.1 Correctness (10 points) Is this binary tree a valid binary search tree? In other words, does it satisfy the binary search tree property? If not, which node(s) violates the binary search tree...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • Read a list of names from a file. Insert the names into a binary search tree...

    Read a list of names from a file. Insert the names into a binary search tree that is a little different from what we discussed in class. For this tree, the left side will contain the larger values and the right side will contain the smaller values. In essence, it is the exact opposite of a normal binary search tree. Our tree will also be able to store duplicate names. Aside from a left and a right pointer for each...

  • Algorithms and Data Structures Let T be a binary search tree which implements a dictionary. Let...

    Algorithms and Data Structures Let T be a binary search tree which implements a dictionary. Let v be a node of T, and T_v be the subtree rooted at v. Design a recursive algorithm CountLE(v, k) which, given an input node v and a key k, returns the number of entries in T_v with key at most k.

  • Beginning with an empty binary search tree, what binary search tree is formed when you add the following letters in the order given?

    Trees-related questionsBeginning with an empty binary search tree, what binary search tree is formed when you add the following letters in the order given? J, N, B, A, W, E, TRepresent the following binary tree with an array  What is the result of adding 3 and 4 to the 2-3 tree shown below?Why does a node in a red-black tree require less memory than a node in a 2-3-4 tree?Why can’t a Red-Black Tree have a black child node with exactly...

  • Recall that in a binary search tree, at every node, all elements to the left of...

    Recall that in a binary search tree, at every node, all elements to the left of the node have a smaller key, and all elements to the right of a node have a larger key. Write a program called that takes two parameters: a pointer to a binary search tree node, and an int parameter called min which will print all the elements bigger than the specified value, min. Your program should allow the user to enter data (integer) from...

  • Write a program in C to make dictionary to add, delete and search words Using linked lists. MAKE THE SEARCHING IN THE pr...

    Write a program in C to make dictionary to add, delete and search words Using linked lists. MAKE THE SEARCHING IN THE program like the searching in the online dictionary(if we enter the 1 letter then it will show all the letters starting with the same number and if we enter 2 letters then it will show all the numbers starting with same two letters and so on up to the complete word.) make the following functions 1. insert 2....

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