Question

Write a C++ program that implements a binary search tree (BST) to manage a number of...

Write a C++ program that implements a binary search tree (BST) to manage a number of integer items with different priorities. In order to do so, you will need to maintain a queue as an item in the tree. Each queue item is an integer. All items in a given queue will have the same priority. As a new item with a user-specified priority comes in, it should be enqueued in the queue with items of that priority. A new queue may need to created in case a queue with that priority does not exist. Use dynamic memory to manage nodes in the queue and BST.

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

#include <iostream>
#include <stdlib.h>


using namespace std;

// Queue Operations
struct queue_node
{
   int key;
   struct queue_node* next;
};

void enqueue(struct queue_node* n, int key)
{
   struct queue_node* t = (struct queue_node*) malloc(sizeof(struct queue_node));
   t->key = key;
   t->next = NULL;
   while (n != NULL)
   {
       n = n->next;
   }
   n = t;

}

// Use this utility program just to show the data
int dequeue(struct queue_node* node)
{
   if (node != NULL)
   {
       int data = node->key;
       return data;
   }
}

bool isEmpty(struct queue_node* node)
{
   if (node == NULL)
   {
       return true;
   }
   else
       return false;
}

// Creating a struct node to use in the bst
typedef struct node
{
   struct queue_node* Q; // Queue to hold the values of the same priority
   int priority; // to determine the priority of the node
   struct node* left; // left children of this node
   struct node* right; // right children of this node
} Node;


// utility function to insert in the BST
void insert(Node* node, int key, int priority)
{
   if (node == NULL)
   {
       node = (Node*)malloc(sizeof(Node));
       node->Q = (struct queue_node*) malloc(sizeof(struct queue_node));
       node->Q->next = NULL;
       enqueue(node->Q, key);
       node->priority = priority;
       node->left = NULL;
       node->right = NULL;
       cout << "At priority " << priority << " data " << key << " has been inserted" << endl;
       return;
   }
   if (priority == -1)
   {
       enqueue(node->Q, key);
       node->priority = priority;
       cout << "At priority " << priority << " data " << key << " has been inserted" << endl;
       return;
   }

   if (node->priority == priority) // If we get the right priority we just enqueue the number
   {
       enqueue(node->Q, key);
       cout << "At priority " << priority << " data " << key << " has been inserted" << endl;
       return;
   }
   //If it is not null we search for the right place to insert the data on the basis of priority
   if (priority < node->priority) // if the priority is lesser we search in the left subtree
   {
       insert(node->left, key, priority);
   }
   else if (priority > node->priority) // if the priority is greater we search in the right subtree
   {
       insert(node->right, key, priority);
   }
   return;
}


// Driver Program
int main()
{
   Node* root = (Node*)malloc(sizeof(Node));
   root->left = NULL;
   root->right = NULL;
   root->priority = -1;
   root->Q = (struct queue_node*)malloc(sizeof(struct queue_node));
   root->Q->next = NULL;
   insert(root, 23, 1);
   insert(root, 324, 1);
   insert(root, 21, 2);
   insert(root, 435, 2);
   insert(root, 332, 2);
   insert(root, 212, 0);
   insert(root, 32, 3);
   insert(root, 31, 3);

   return 0;
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that implements a binary search tree (BST) to manage a number of...
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
  • How do I implement overload operator == C++ No late submission Submission instructions: Name your file...

    How do I implement overload operator == C++ No late submission Submission instructions: Name your file as (Last NameFirst Name.cpp). Up- load to Blackboard. 1. (Total 100 Points) Write a C++ program that implements a binary search tree (BST) to manage a number of integer items with different priorities. In order to do so, you will need to maintain a queue as an item in the tree. Each queue item is an integer. All items in a given queue will...

  • Construct a Binary Search Tree (BST) program in C++. The program is required to: 1) load...

    Construct a Binary Search Tree (BST) program in C++. The program is required to: 1) load the BST from a dataset (I will provide the datasets-see below) in the order they exist in the dataset. 2) after the BST is built analyze the BST and display the following values: 2.1) the smallest branch height 2.2) the highest branch height 2.3) the number of nodes in the tree 2.4) the determination if the tree is balanced 2.5) the determination if the...

  • help 2. Do the following problems: Create a binary search tree (BST), with the following words...

    help 2. Do the following problems: Create a binary search tree (BST), with the following words inserted: Int, Char, Return, Break, Float, While, Short, Sort, Double, For, Continue. a. b. Insert the following words into the BST built in (a): Tree, Table, Binary, Network, Visit, Seekk, Traversal c. Where is the minimum key value in a BST? (Give a concrete example) d. Where is the maximum key value in a BST? (Give a concrete example) e. How many comparisons are...

  • C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N...

    C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...

  • Write a program in C fro Binary Search tree using following functions 1. Insertion operation using...

    Write a program in C fro Binary Search tree using following functions 1. Insertion operation using recursion 2. Deletion operation 3. Minimum/Maximum of a BST 6. Reorganize the tree so that the tree height is minimum 7. Print all the nodes from the node to the path to the root 8. Find the lowest common shared node between two given nodes

  • 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...

  • Programming in C++ Implement a BST (Binary Search Tree) and test your program. (Linked List based.)...

    Programming in C++ Implement a BST (Binary Search Tree) and test your program. (Linked List based.) You should test at least the three major functions. (Insert, Retrieve, and Delete).

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • I need this ASAP please Choose the data structure from the list at right that best...

    I need this ASAP please Choose the data structure from the list at right that best represents the description at left. Last item added is the first to be removed java.util collections calls this a Map [Choose ] Priority Queue Bag SortedArrayList ArrayList Stack Expression Tree Queue Iterator Binary Search Tree Heap Hash Table Dictionary First item added is the first to be removed Your computer operating system uses one manage processes and job requests when resources are tight. Combines...

  • Write a C++ program to manage a Point of Sale System for a Supermarket. The main...

    Write a C++ program to manage a Point of Sale System for a Supermarket. The main user is an employee at the Supermarket. Build Specifications (36 points) 1. The system should load a catalog of all items that the store sells. 2. A user can search the inventory: The user of the system can search the inventory by using the name of the item or by category. 3. A user can sell items once they are found. Or can sell...

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