#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include <string.h>
using namespace std;
//Structure definition
struct BinaryTreeNode
{
//Structure member
string value;
BinaryTreeNode *left;
BinaryTreeNode *right;
};//End of structure
//Returns the node having minimum value
BinaryTreeNode* FindMin(BinaryTreeNode *tnode)
{
//Checks if tree is empty
if(tnode == NULL)
{
//There is no element in the tree
return NULL;
}
//Go to the left sub tree to find the min element
if(tnode -> left)
return FindMin(tnode -> left);
else
return tnode;
}//End of function
//Insert a node
BinaryTreeNode *Insert(BinaryTreeNode *tnode, string value)
{
//Checks if tree is empty
if(tnode == NULL)
{
//Creates a temporary pointer
BinaryTreeNode *temp;
temp = new BinaryTreeNode;
temp -> value = value;
//Initializes left and right node to null
temp -> left = temp -> right = NULL;
return temp;
}//End of if
//Checks if the entered value is greater than the current node
value
if(value.compare(tnode -> value) > 0)
{
//Insert the node at right
tnode -> right = Insert(tnode -> right, value);
}//End of if
//Checks if the entered value is less than the current node
value
else if(value.compare(tnode -> value) < 0)
{
//Insert the node at left
tnode -> left = Insert(tnode -> left, value);
}//End of else if
//Else there is nothing to do as the data is already in the
tree.
return tnode;
}//End of function
//Function to delete a node from tree
BinaryTreeNode * Delete(BinaryTreeNode *tnode, string value)
{
//Creates a temporary pointer
BinaryTreeNode *temp;
//Checks if tree is empty
if(tnode == NULL)
{
cout<<"Element Not Found";
}//End of if
//Checks if the entered value is less than the current node
value
else if(value.compare(tnode -> value) < 0)
{
tnode -> left = Delete(tnode -> left, value);
}// End of else if condition
//Checks if the entered value is greater than the current node
value
else if(value.compare(tnode -> value) > 0)
{
tnode -> right = Delete(tnode -> right, value);
}// End of else
else
{
//Now We can delete this node and replace with either minimum
element in the right sub tree or maximum element in the left
subtree
if(tnode -> right && tnode -> left)
{
//Here we will replace with minimum element in the right sub
tree
temp = FindMin(tnode -> right);
tnode -> value = temp -> value;
//As we replaced it with some other node, we have to delete that
node
tnode -> right = Delete(tnode -> right, temp ->
value);
}// End of if condition
else
{
//If there is only one or zero children then we can directly remove
it from the tree and connect its parent to its child
temp = tnode;
if(tnode -> left == NULL)
tnode = tnode -> right;
else if(tnode -> right == NULL)
tnode = tnode -> left;
//Delete temp
free(temp);
}//End of inner else
}//End of outer else
// Returns the node
return tnode;
}//End of function
//Inorder traversal
void Inorder(BinaryTreeNode *tnode)
{
//Checks if tree is empty
if(tnode == NULL)
return;
//Recursively calls the left child node
Inorder(tnode -> left);
//Displays data
cout<<tnode -> value<<" ";
//Recursively calls the right child node
Inorder(tnode -> right);
}//End of function
//Pre order traversal
void Preorder(BinaryTreeNode *tnode)
{
//Checks if tree is empty
if(tnode == NULL)
return;
//Displays data
cout<<tnode -> value<<" ";
//Recursively calls the left child node
Preorder(tnode -> left);
//Recursively calls the right child node
Preorder(tnode -> right);
}//End of function
//Post order traversal
void Postorder(BinaryTreeNode *tnode)
{
//Checks if tree is empty
if(tnode == NULL)
return;
//Recursively calls the left child node
Postorder(tnode -> left);
//Recursively calls the right child node
Postorder(tnode -> right);
//Displays data
cout<<tnode -> value<<" ";
}//End of function
//Returns depth of the tree
int depth(BinaryTreeNode *tnode)
{
//Checks if tree is empty
if (tnode == NULL)
return 0;
//Recursively calls the function to calculate depth
return max( depth(tnode->left), depth(tnode->right) ) +
1;
}// End of function
// Returns number of nodes in the tree
int numberOfNodes(BinaryTreeNode *tnode, int &counter)
{
//Checks if tree is empty
if(tnode == NULL)
{
return 0;
}// End of if condition
//Recursively calls the left child node
numberOfNodes(tnode -> left, counter);
//Recursively calls the right child node
numberOfNodes(tnode -> right, counter);
// Increase the node counter by one
counter++;
}// End of function
// Function to search a node in tree
void Find(BinaryTreeNode *tnode, string value, int
&index)
{
//Checks if tree is empty
if(tnode == NULL)
{
return;
}// End of if condition
//Recursively calls the left child node
Find(tnode -> left, value, index);
//Recursively calls the right child node
Find(tnode -> right, value, index);
// Checks if the current node value is equals to the value passed
as a parameter to search is equal
if(tnode -> value.compare(value) == 0)
{
// Display the node
cout<<"\n Found: "<<tnode -> value<<" ";
// Set the index to one
index = 1;
// return from the function
return;
}// End of if condition
}// End of function
//Main function
int main()
{
//Creates root and temporary node
BinaryTreeNode *root = NULL,*temp;
// To store the string value entered by the use for find, or
delete, or insert
string val;
// To store found status or number of nodes
int counter;
// To store user choice
int ch;
//Loops till user choice is 8
while(1)
{
// Initializes counter to zero
counter = 0;
//Displays the menu
cout<<"\n 1) Insert \n 2) Find \n 3) Depth and Number of
Nodes \n 4) In-order \n 5) Pre-order \n 6) Post-order \n 7) Delete
\n 8) Exit\n";
//Accepts user choice
cout<<"Enter your choice: ";
cin>>ch;
//Checks the choice and calls appropriate function
switch(ch)
{
case 1:
cout<<"\n Enter the value to insert: ";
cin>>val;
root = Insert(root, val);
break;
case 2:
cout<<"\n Enter the value to find: ";
cin>>val;
Find(root, val, counter);
if(counter != 1)
cout<<"\n Not Found: "<<val;
break;
case 3:
numberOfNodes(root, counter);
cout<<"\n Number of nodes: "<<counter;
cout<<"\n Depth of the tree: "<<depth(root);
break;
case 4:
cout<<"\n Inorder Traversal: ";
Inorder(root);
break;
case 5:
cout<<"\n Preorder Traversal:: ";
Preorder(root);
break;
case 6:
cout<<"\n Postorder Traversal:: ";
Postorder(root);
break;
case 7:
cout<<"\n Enter the value to delete: ";
cin>>val;
root = Delete(root,val);
break;
case 8:
exit(0);
default:
cout<<"\n Invalid choice:";
}//End of switch
}//End of while
return 0;
}//End of main
Sample Run:
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 1
Enter the value to insert: check
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 1
Enter the value to insert: this
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 4
Inorder Traversal: check this
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 5
Preorder Traversal:: check this
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 6
Postorder Traversal:: this check
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 1
Enter the value to insert: again
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 4
Inorder Traversal: again check this
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 5
Preorder Traversal:: check again this
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 6
Postorder Traversal:: again this check
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 3
Number of nodes: 3
Depth of the tree: 2
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 2
Enter the value to find: hello
Not Found: hello
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 2
Enter the value to find: check
Found: check
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 1
Enter the value to insert: how
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 4
Inorder Traversal: again check how this
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 3
Number of nodes: 4
Depth of the tree: 3
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 7
Enter the value to delete: how
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 5
Preorder Traversal:: check again this
1) Insert
2) Find
3) Depth and Number of Nodes
4) In-order
5) Pre-order
6) Post-order
7) Delete
8) Exit
Enter your choice: 8
uhayDownloads/ab%2010.pdf Lab 10 Fall 2017 Eric May struct TreeNode string word TreeNode left, right Note: A...
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...
I am struggling with a program in C++. it involves using a struct and a class to create meal arrays from a user. I've written my main okay. but the functions in the class are giving me issues with constructors deconstructors. Instructions A restaurant in the Milky way galaxy called “Green Alien” has several meals available on their electronic menu. For each food item in each meal, the menu lists the calories per gram and the number of grams per...
Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end. Here is what is missing : Here is my code: public class GSL { private String arr[]; private int size; public GSL() { arr = new String[10]; size = 0; } public int size() { return size; } public void add(String value) { ...
MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access a vector of structures, use sort with different compare functions. Assignment: Your program will use the same input file, but you will print the whole book information, not just the title. And, most importantly, this time you will take an object oriented approach to this problem. Detailed specifications: Define a class named Collection, in which you will define a structure that can hold book...
MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access a vector of structures, use sort with different compare functions. Assignment: Your program will use the same input file, but you will print the whole book information, not just the title. And, most importantly, this time you will take an object oriented approach to this problem. Detailed specifications: Define a class named Collection, in which you will define a structure that can hold book...
Recursion and Trees Application – Building a Word Index Make sure you have read and understood · lesson modules week 10 and 11 · chapters 9 and 10 of our text · module - Lab Homework Requirements before submitting this assignment. Hand in only one program, please. Background: In many applications, the composition of a collection of data items changes over time. Not only are new data items added and existing ones removed, but data items may be duplicated. A list data structure...
Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic singly linked list that can hold any data type. The interface has been specified and provided to you in a header file called mylist.h. So your job is to write mylist.c that implements each function whose prototype is included in mylist.h. Specifically, you are asked to write the following functions: struct Node *addFront(struct List *list, void *data) void traverseList(struct List *list, void (*f)(void *))...
linked list operation
/***************************************************************************************
This function creates a new node with the information give as a parameter and looks
for the right place to insert it in order to keep the list organized
****************************************************************************************/
void insertNode(string first_name, string last_name, string phoneNumber)
{
ContactNode *newNode;
ContactNode *nodePtr;
ContactNode *previousNode = nullptr;
newNode = new ContactNode;
/***** assign new contact info to the new node here *****/
if (!head) // head points to nullptr meaning list is empty
{
head = newNode;...
I
need help writing this code in C++
Proj11.cpp is provided as well as the randomdata.txt
thank you in advance!
Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...
10.3 Name_year in C - phase 3 This lab is part 2 of a series of 3 labs that will walk you through the process of creating a C struct and related functions that are the equivalent of the Name_year class you created in chapter 9. For this phase, we will Create an init_name_year() function that initializes a Name_year object. Move the object initialization logic from create_name_year() to init_name_year(). Create a function called compare_name_year() to compare two Name_year objects. The...