Question

In: Computer Science

Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...

Using the following definitions for a binary tree,

typedef struct bintreenode

{

    int data;

    struct bintreenode* left;

    struct bintreenode* right;

} btreenode;

// Used for a node in the queue.

typedef struct node

{

    btreenode* nodePtr;

    struct node* next;

} node;

// Used to represent the queue efficiently.

typedef struct queue

{

    node* front;

    node* back;

} queue;

Implement the following functions:

void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root.

void insert(btreenode* root, int value) //using level order

void deletion(btreenode* root, int value) // find the node to be deleted and also the deepest node, copy the value of the deepest node to the node to be deleted

void deleteDeepest(btreenode* root, btreenode* d_node) // delete the deepest node  

Solutions

Expert Solution

CODE:-

void bfs(btreenode* root)
{
   if (root == NULL) return;
queue<btreenode *> q;
q.push(root);
  
while (q.empty() == false)
{
btreenode *temp = q.front();
cout << temp->data << " ";
q.pop();
if (temp->left != NULL)
q.push(temp->left);
if (temp->right != NULL)
q.push(temp->right);
}
}
void insert(btreenode* root, int value)
{
// If the tree is empty, assign new node address to root
btreenode* newNode = new btreenode();
if (root == NULL) {
       newNode->data = value;
newNode->left = newNode->right = NULL;
root = newNode;
return root;
}

queue<btreenode*> q;
q.push(root);

while (!q.empty()) {
btreenode* temp = q.front();
q.pop();

if (temp->left != NULL)
q.push(temp->left);
else {
           newNode->data = value;
newNode->left = newNode->right = NULL;
temp->left = newNode;
return ;
}

if (temp->right != NULL)
q.push(temp->right);
else {
       newNode->data = value;
newNode->left = newNode->right = NULL;
temp->right = newNode;
return ;
}
}
}
void deletion(btreenode* root, int value)
{
if (root == NULL)
return NULL;
  
if (root->left == NULL && root->right == NULL) {
if (root->data == key)
return NULL;
else
return root;
}
  
queue<btreenode*> q;
q.push(root);
  
btreenode* temp;
btreenode* key_node = NULL;
while (!q.empty()) {
temp = q.front();
q.pop();
  
if (temp->data == value)
key_node = temp;
  
if (temp->left)
q.push(temp->left);
  
if (temp->right)
q.push(temp->right);
}
  
if (key_node != NULL) {
int x = temp->key;
deletDeepest(root, temp);
key_node->key = x;
}
}
void deleteDeepest(btreenode* root,
btreenode* d_node)
{
queue <btreenode*>q;
q.push(root);
  
// Do level order traversal until last node
bintreenode* temp;
while (!q.empty()) {
temp = q.front();
q.pop();
if (temp == d_node) {
temp = NULL;
delete (d_node);
return;
}
if (temp->right) {
if (temp->right == d_node) {
temp->right = NULL;
delete (d_node);
return;
}
else
q.push(temp->right);
}
  
if (temp->left) {
if (temp->left == d_node) {
temp->left = NULL;
delete (d_node);
return;
}
else
q.push(temp->left);
}
}
}

Explanation:-

1)BFS on a tree is simply level order traversal the idea is to push the root in the queue then it’s child nodes are also put in the First In First Out queue then we’re printing the nodes in level order traversal.
2) In this approach we traverse the tree in level order using a queue and then check if any node whose left child is empty then we insert our new node here and like this we also check the right child of any node if it is empty then we insert our new node.
3) Here the algorithm is to find the deepest rightmost element and we replace it’s data with the data of the node to be deleted and then we delete the deepest rightmost element.


Related Solutions

Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates { int x; int y; } uCoordinate; // TODO - Populate 4 different Coordinates with any numbers between 1 and 99 for x & y values // using coordinate1, coordinate2, coordinate3, & coordinate4 as Coordinate names // TODO - Print to screen the x & y values of each coordinate // TODO - Replace the following with your name: Ethin Svoboda int main() { //...
Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit:...
Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit: name (string, up to 50 characters long) color (string, up to 10 characters long) fat (integer) sugar (integer) carbohydrate (integer) Write a void function called printFruit that takes a fruitType parameter and prints the data (as shown in the example below). Declare a variable of type fruitType to store the following data: name: banana color: yellow fat: 1 sugar: 15 carbohydrate: 22 then use...
Consider the following declaration: typedef struct{                                  &nbsp
Consider the following declaration: typedef struct{                                          int A;                                          char B[10];                                          float C;                                          char D;                                  }rectype;                                   typedef rectype matrix[121][4][5];                                   matrix A1; Compute the address of element A1[120][3][3] given the base address at 2000.
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT