Question

C++, data structure using :binarySearchTree Deleting an element from a binary search tree is far more complicated than i...

C++, data structure

using :binarySearchTree

Deleting an element from a binary search tree is far more complicated than inserting an element in a binary search tree. An analysis of binary search tree deletion finds 4 cases:

Case 1: The node to be deleted has no left and right subtrees

Case 2: The node to be deleted has no left subtree but does have a right subtree.

Case 3: The node to be deleted has no right subtee but does have a left subtree

Case 4: The node to be deleted has non empty left and right subtrees.

The implementation of case 4 - see the file binarySearchTree.h in the zipped file - swaps the info in the largest node in the node to be deleted's left subtree with the info in the node to be deleted. The choice of the largest node in the node to be deleted's left subtree is arbitrary. It is just as easy to swap with the node in the node to be deleted's right subtree. Rewrite the function to swap info with the smallest element in the node to be deleted's right subtree.

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

PROGRAM:

#include<iostream>
#include<stdlib.h>
using namespace std;  

class BST{
private:
   struct node
   {
int key;
struct node *left, *right;
   };
  
   struct node*root;

   struct node* insert(struct node* Node , struct node*newNode)
   {
if (Node == NULL) return newNode;

if (newNode->key < Node->key)
Node->left = insert(Node->left, newNode);
else if (newNode->key > Node->key)
Node->right = insert(Node->right, newNode);   

return Node;
   }

struct node * minValueNode(struct node* node)
{
struct node* current = node;
  
/* loop down to find the leftmost leaf */
while (current->left != NULL)
current = current->left;
  
return current;
}

struct node* deleteNode(struct node* root, int key)
{
// base case
if (root == NULL) return root;
  
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
if (key < root->key)
root->left = deleteNode(root->left, key);
  
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if (key > root->key)
root->right = deleteNode(root->right, key);
  
// if key is same as root's key, then This is the node
// to be deleted
else
{
// node with only one child or no child
if (root->left == NULL)
{
struct node *temp = root->right;
delete(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
delete(root);
return temp;
}
  
// node with two children: Get the inorder successor (smallest
// in the right subtree)
struct node* temp = minValueNode(root->right);
  
// Copy the inorder successor's content to this node
root->key = temp->key;
  
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
return root;
}  

int size(struct node*Node){
   if(!Node)return 0;
   return 1+size(Node->left)+size(Node->right);
}

void inorder(struct node*Node )

{
   if(!Node)return;
   inorder(Node->left );
   cout<<Node->key<<" ";
   inorder(Node->right );
}

void Postorder(struct node*Node )

{
   if(!Node)return;
   Postorder(Node->left );
  
   Postorder(Node->right );
   cout<<Node->key<<" ";
}
public:
   BST(){
   root=NULL;
}
  void Insert(int item)
   {
struct node *temp = new struct node;
temp->key = item;
temp->left =NULL;
   temp->right = NULL;
root= insert(root,temp);
   }
  
   void Delete(int item){
       root= deleteNode(root,item);
   }
int Size(){
       return size(root);
   }
void PrintInorder(){
   inorder(root);
   }
void PrintPostorder()

{
   Postorder(root);
   }

};

int main(){
   int arr[]={20,5,8,3,12,9,2,16};
   BST bst;
   int n = sizeof(arr)/sizeof(int);
   cout<<"Inserting nodes with ";
   for(int i=0;i<n;i++){
       cout<<arr[i]<<" ";
       bst.Insert(arr[i]);
   }
   cout<<"\n\n";
   cout<<"The number of nodes in the tree is now "<<bst.Size()<<"\n\n";
   cout<<"Here are the values in the tree in order:\n";
   bst.PrintInorder();
   cout<<"\n\n";
   cout<<"Now deleting 8 from tree.......\n";
   bst.Delete(8);
   cout<<"Now deleting 12 from tree.......\n";
   bst.Delete(12);
   cout<<"The number of nodes in the tree is now "<<bst.Size()<<"\n\n";
   cout<<"Here are the values in the tree in order:\n";
   bst.PrintInorder();
   cout<<"\n\n";
   cout<<"Here are the values in the tree POST order:\n";
   bst.PrintPostorder();
   cout<<"\n\n";

}

Add a comment
Know the answer?
Add Answer to:
C++, data structure using :binarySearchTree Deleting an element from a binary search tree is far more complicated than i...
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
  • In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write...

    In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...

  • Draw the tree resulting from inserting the following values into a binary search tree in order...

    Draw the tree resulting from inserting the following values into a binary search tree in order without re-balancing: 40, 10, 60, 30, 20, 90, 70, 50 Null pointers can be omitted as long as it is clear whether a single child is a left or right child. THEN For every node in the tree, the values that can be in the subtree rooted at that node are constrained by ancestors to be in some range of integers. The root (the...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • please write the code in C++ Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as...

    please write the code in C++ Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C & findMax( ) const...

  • C++ Question 5 5 pts In a min-heap of N elements, if we want to find...

    C++ Question 5 5 pts In a min-heap of N elements, if we want to find the max element, we have to search all the leaves. What is the big-o running time of findMax? O(N^2) Oſlog N) O(N) OIN log N) Question 6 5 pts An AVL tree is a Binary Search Tree that has the following additional property for every node in the tree, the height of the left and right subtrees is the same none of the above...

  • In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a...

    In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C...

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • 3. (8 points) Using the implementation of binary search tree operations we discussed in class, draw...

    3. (8 points) Using the implementation of binary search tree operations we discussed in class, draw the trees that result from the following operations: (a) Inserting 142, 400, 205, 127, 100, 320, 160, 141, and 110 into an initially-empty tree (in that order). (b) Deleting 142 from the tree you drew for part (a). 4. (8 points) Draw the unique binary tree that has a preorder traversal of 4, 1, 6, 3, 7, 5, 9, 2, 8 and an inorder...

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

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