Modify the code on BST and implement the Delete Node algorithms. Consider all the three deleting algorithms. - a leaf node - a node with one child and - a node with two children

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Modify the code on BST and implement the Delete Node algorithms. Consider all the three deleting algorithms.
- a leaf node
- a node with one child and
- a node with two children

Here is the code so far:

#include <iostream>
using namespace std;

struct Tree
{
   char data;
   Tree *left;
   Tree *right;
   Tree *parent;
};
const int size=10;

struct Tree *newTreeNode(int data)
{
   Tree *node = new Tree;
   node->data = data;
   node->left = NULL;
   node->right = NULL;
   node->parent = NULL;
   return node;
}

struct Tree* insertTreeNode(struct Tree *node, int data)
{
   static Tree *p; //retains its value between calls to this function
   Tree *retNode;
   if(node == NULL) {
       retNode = newTreeNode(data);
       retNode->parent = p;
       return retNode;
   }
   if(data <= node->data ) {
       p = node;
       node->left = insertTreeNode(node->left,data);
   }
   else {
       p = node;
       node->right = insertTreeNode(node->right,data);
   }
   return node;
}

Tree* lookUp(struct Tree *node, char key)
{
   if(node == NULL) return node;

   if(node->data == key) return node;
   else {
       if(node->data < key)
           return lookUp(node->right, key) ;
       else
           return lookUp(node->left, key);
   }
}

int treeSize(struct Tree *node)
{
   if(node == NULL)
       return 0;
   else
       return treeSize(node->left) + 1 + treeSize(node->right);
}

int maxDepth(struct Tree *node)
{
   if(node == NULL || (node->left == NULL && node->right == NULL))
       return 0;
   int leftDepth = maxDepth(node->left);
   int rightDepth = maxDepth(node->right);
   if (leftDepth > rightDepth )
       return leftDepth + 1;
   else
       return rightDepth + 1;
}

int minDepth(struct Tree *node)
{
   if(node == NULL || (node->left == NULL && node->right == NULL))
       return 0;
   int leftDepth = minDepth(node->left);
   int rightDepth = minDepth(node->right);

   if (leftDepth < rightDepth )
       return leftDepth + 1;
   else
       return rightDepth + 1;
}

bool isBalanced(struct Tree *node)
{
   if(maxDepth(node)-minDepth(node) <= 1)
       return true;
   else
       return false;
}

/* Tree Minimum */
Tree* minTree(struct Tree *node)
{
   if(node == NULL) return NULL;
   while(node->left)
           node = node -> left;
   return node;
}

/* Tree Maximum */
Tree* maxTree(struct Tree *node)
{
   while(node->right)
           node = node -> right;
   return node;
}

int main(int argc, char **argv)
{
   char ch;
   Tree *found;

   /* Data for building the Tree */
   char charArr[] = {'F','B','A','D','C','E','H','I','G','K'};
  
   Tree *root = newTreeNode(charArr[0]);
   for (int i=1; i < size; i++)
   {
       insertTreeNode(root,charArr[i]);
   }

   /* size of tree */
   cout << "Data size in the tree = " << treeSize(root) << endl;

   /* max depth */
   cout << "Maximum depth = " << maxDepth(root) << endl;

   /* min depth */
   cout << "Minimum depth = " << minDepth(root) << endl;

   /* balanced tree? */
   if(isBalanced(root))
       cout << "This tree is balanced!\n";
   else
       cout << "This tree is not balanced!\n";

   /* min value of the tree*/
   if(root)
           cout << "Min value = " << minTree(root)->data << endl;

   /* max value of the tree*/
   if(root)
           cout << "Max value = " << maxTree(root)->data << endl;

   /* Example of Min and Max value in a sub tree */
   ch = 'H';
   found = lookUp(root,ch);
   if(found) {
       cout << "Min value of subtree " << ch << " as a root is "
       << minTree(found)->data << endl;
       cout << "Max value of subtree " << ch << " as a root is "
       << maxTree(found)->data << endl;
   }
return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY