Question

In: Computer Science

Write this code in full c++ and in separate files in visual studio. Binary Search Tree...

Write this code in full c++ and in separate files in visual studio.

Binary Search Tree Using a binary search tree, you are tasked with building a dictionary program that stores 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 exists in the dictionary then the definition will display. You can also add words to the dictionary. For testing you should be able to display the current word list. The dictionary will populate from a data file and new words will be saved to the file as well.

Solutions

Expert Solution

For taking file input use fstream and sstream header file.

This problem illustrates the binary search tree for dictionary.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct BSTnode {
char word[128], meaning[256];
struct BSTnode *left, *right;
};

struct BSTnode *root = NULL;

struct BSTnode * createNode(char *word, char *meaning) {
struct BSTnode *newnode;
newnode = (struct BSTnode *)malloc(sizeof(struct BSTnode));
strcpy(newnode->word, word);
strcpy(newnode->meaning, meaning);
newnode->left = newnode->right = NULL;
return newnode;
}

void insert(char *word, char *meaning) {

struct BSTnode *parent = NULL, *current = NULL, *newnode = NULL;

int res = 0;

if (!root) {

root = createNode(word, meaning);

return;

}

for (current = root; current !=NULL;

current = (res > 0) ? current->right : current->left) {

res = strcasecmp(word, current->word);

if (res == 0) {

printf("Duplicate entry!!\n");

return;

}

parent = current;

}

newnode = createNode(word, meaning);

res > 0 ? (parent->right = newnode) : (parent->left = newnode);

return;

}

void deleteNode(char *str) {

struct BSTnode *parent = NULL, *current = NULL, *temp = NULL;

int flag = 0, res = 0;

if (!root) {

printf("BST is not present!!\n");

return;

}

current = root;

while (1) {

res = strcasecmp(current->word, str);

if (res == 0)

break;

flag = res;

parent = current;

current = (res > 0) ? current->left : current->right;

if (current == NULL)

return;

}

/* deleting leaf node */

if (current->right == NULL) {

if (current == root && current->left == NULL) {

free(current);

root = NULL;

return;

} else if (current == root) {

root = current->left;

free (current);

return;

}

flag > 0 ? (parent->left = current->left) :

(parent->right = current->left);

} else {

/* delete node with single child */

temp = current->right;

if (!temp->left) {

temp->left = current->left;

if (current == root) {

root = temp;

free(current);

return;

}

flag > 0 ? (parent->left = temp) :

(parent->right = temp);

} else {

/* delete node with two children */

struct BSTnode *successor = NULL;

while (1) {

successor = temp->left;

if (!successor->left)

break;

temp = successor;

}

temp->left = successor->right;

successor->left = current->left;

successor->right = current->right;

if (current == root) {

root = successor;

free(current);

return;

}

(flag > 0) ? (parent->left = successor) :

(parent->right = successor);

}

}

free (current);

return;

}

void findElement(char *str) {

struct BSTnode *temp = NULL;

int flag = 0, res = 0;

if (root == NULL) {

printf("Binary Search Tree is out of station!!\n");

return;

}

temp = root;

while (temp) {

if ((res = strcasecmp(temp->word, str)) == 0) {

printf("Word : %s", str);

printf("Meaning: %s", temp->meaning);

flag = 1;

break;

}

temp = (res > 0) ? temp->left : temp->right;

}

if (!flag)

printf("Search Element not found in Binary Search Tree\n");

return;

}

void inorderTraversal(struct BSTnode *myNode) {

if (myNode) {

inorderTraversal(myNode->left);

printf("Word : %s", myNode->word);

printf("Meaning : %s", myNode->meaning);

printf("\n");

inorderTraversal(myNode->right);

}

return;

}

int main() {

int ch;

char str[128], meaning[256];

while (1) {

printf("\n1. Insertion\t2. Deletion\n");

printf("3. Searching\t4. Traversal\n");

printf("5. Exit\nEnter ur choice:");

scanf("%d", &ch);

getchar();

switch (ch) {

case 1:

printf("Word to insert:");

fgets(str, 100, stdin);

printf("Meaning:");

fgets(meaning, 256, stdin);

insert(str, meaning);

break;

case 2:

printf("Enter the word to delete:");

fgets(str, 100, stdin);

deleteNode(str);

break;

case 3:

printf("Enter the search word:");

fgets(str, 100, stdin);

findElement(str);

break;

case 4:

inorderTraversal(root);

break;

case 5:

exit(0);

default:

printf("You have entered wrong option\n");

break;

}

}

return 0;

}


Related Solutions

(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
In C++: Write a binary search tree and include the functions to find the number of...
In C++: Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal.
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print out the data value for each node it encounters in the insert operation. Remember that the module AVL tree code gets part of its functionality from the BST type, since an AVL tree is a binary search tree, but adds some additional functionality. 2. Add code to the main method to meet the following requirements: (a) Create an AVL tree to hold names stored...
Code using C++ A binary search tree is a data structure designed for efficient item insertion,...
Code using C++ A binary search tree is a data structure designed for efficient item insertion, deletion, and retrieval. These 3 operations share an average run time complexity of O(log(n)). Such time complexities are guaranteed whenever you are working with a balanced binary search tree. However, if you have a tree that begins leaning heavily to either its left or right side, then you can expect the performances of the insertion, deletion, and retrieval operations to degrade. As an example,...
Here is a picture of a Binary Search Tree. First, construct the Binary Search Tree using...
Here is a picture of a Binary Search Tree. First, construct the Binary Search Tree using the following BinaryNode as we discussed in class. public class BinaryNode { private int value; private BinaryNode leftChild; private BinaryNode rightChild; public BinaryNode(int value) { this.value = value; leftChild = null; rightChild = null; } public BinaryNode(int value, BinaryNode leftChild, BinaryNode rightChild) { this.value = value; this.leftChild = leftChild; this.rightChild = rightChild; } public int getValue() { return value; } public void setValue(int value)...
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
Code: please use in visual studio 2019 c++ (not java) In total you will write 6...
Code: please use in visual studio 2019 c++ (not java) In total you will write 6 functions: Functions : A ) addInts, returns int; input is two ints; Add the values of the two parameters and return the sum B) subInts , returns int; input is two ints; Subtract the values of the two parameters and return the difference C) multInts, returns int; input is two ints; multiple the values of the two parameters and return the product D) divInts,...
Prove that a binary tree that is not full cannot correspond to an optimal prefix code....
Prove that a binary tree that is not full cannot correspond to an optimal prefix code. The proof should first consider a prefix-free code C, whose corresponding binary tree T has some node with only one child; show that one can transform T into another binary tree T', whose corresponding code C' has smaller average length and so is better than C. In your proof you need to indicate the transformation from T into T' and explain why the code...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT