Programming Language: Java Pruning BSTs : write a method/function that prunes a
ID: 3778416 • Letter: P
Question
Programming Language: Java
Pruning BSTs: write a method/function that prunes a BST by “deleting” nodes with key values out of a given range [min, max]. Since actual deletions of nodes from BST is expensive (O(h) time for BSTs of height h) and also some applications might require fast recovery of deleted nodes, we will just put “deletion marks” on those nodes instead of actually deleting them. There are two types of marks we can use to mark nodes: (1) type-I marks indicating that the marked nodes are unavailable (“deleted”); and (2) type-II marks indicating that all nodes in the subtree rooted at the marked nodes are unavailable (“deleted”). There is an O(h)-time algorithm that places no more than O(h) marks (type-I and type-II combined) in a BST of height h in order to indicate that all nodes with key values outside a given range [min, max] are unavailable. The algorithm entailed by your program should has the same properties, i.e., runs in O(h) time and marks no more than O(h) nodes. One possible application of such an algorithm is that in implementing a shopping website, we might need to use BSTs to store results of a search and facilitate fast refinement of the search results, i.e., tablets within a price range.
Explanation / Answer
#ifndef H_binaryTree
#define H_binaryTree
#include <iostream>
using namespace std;
//Definition of the Node
template <class elemType>
struct nodeType
;
//Definition of the category
template <class elemType>
class binaryTreeType
to work out whether or not the binary tree is empty.
//Postcondition: Returns true if the binary tree is empty;
// otherwise, returns false.
void inorderTraversal() const;
//Function associated do} an inorder traversal of the binary tree.
//Postcondition: Nodes square measure written in inorder sequence.
void preorderTraversal() const;
//Function to try to to a preorder traversal of the binary tree.
//Postcondition: Nodes square measure written in preorder sequence.
void postorderTraversal() const;
//Function to try to to a postorder traversal of the binary tree.
//Postcondition: Nodes square measure written in postorder sequence.
int treeHeight() const;
//Function to see the peak of a binary tree.
//Postcondition: Returns the peak of the binary tree.
void destroyTree();
//Function to destroy the binary tree.
//Postcondition: Memory area occupied by every node
// is deallocated.
// root = NULL;
virtual bool search(const elemType& searchItem) const = 0;
//Function to see if searchItem is within the binary
//tree.
//Postcondition: Returns true if searchItem is found in
// the binary tree; otherwise, returns
// false.
virtual void insert(const elemType& insertItem) = 0;
//Function to insert insertItem within the binary tree.
//Postcondition: If there's no node within the binary tree
// that has identical information as insertItem, a
// node with the information insertItem is made
// and inserted within the binary search tree.
virtual void deleteNode(const elemType& deleteItem) = 0;
//Function to delete deleteItem from the binary tree
//Postcondition: If a node with identical information as
// deleteItem is found, it's deleted from
// the binary tree.
// If the binary tree is empty or
// deleteItem isn't within the binary tree,
// Associate in Nursing applicable message is written.
binaryTreeType(const binaryTreeType<elemType>& otherTree);
//Copy creator
binaryTreeType();
//Default creator
~binaryTreeType();
//Destructor
protected:
nodeType<elemType> *root;
private:
void copyTree(nodeType<elemType>* &copiedTreeRoot,
nodeType<elemType>* otherTreeRoot);
//Makes a replica of the binary tree to that
//otherTreeRoot points.
//Postcondition: The pointer copiedTreeRoot points to
// the basis of the derived binary tree.
void destroy(nodeType<elemType>* &p);
//Function to destroy the binary tree to that p points.
//Postcondition: Memory area occupied by every node, in
// the binary tree to that p points, is
// deallocated.
// p = NULL;
void inorder(nodeType<elemType> *p) const;
//Function associated do} an inorder traversal of the binary
//tree to that p points.
//Postcondition: Nodes of the binary tree, to which p
// points, square measure written in inorder sequence.
void preorder(nodeType<elemType> *p) const;
//Function to try to to a preorder traversal of the binary
//tree to that p points.
//Postcondition: Nodes of the binary tree, to which p
// points, square measure written in preorder
// sequence.
void postorder(nodeType<elemType> *p) const;
//Function to try to to a postorder traversal of the binary
//tree to that p points.
//Postcondition: Nodes of the binary tree, to which p
// points, square measure written in postorder
// sequence.
int height(nodeType<elemType> *p) const;
//Function to see the peak of the binary tree
//to that p points.
//Postcondition: Height of the binary tree to that
// p points is came back.
int max(int x, int y) const;
//Function to see the larger of x and y.
//Postcondition: Returns the larger of x and y.
};
//Definition of member functions
template <class elemType>
binaryTreeType<elemType>::binaryTreeType()
template <class elemType>
bool binaryTreeType<elemType>::isEmpty() const
come (root == NULL);
}
template <class elemType>
void binaryTreeType<elemType>::inorderTraversal() const
template <class elemType>
void binaryTreeType<elemType>::preorderTraversal() const
template <class elemType>
void binaryTreeType<elemType>::postorderTraversal() const
template <class elemType>
int binaryTreeType<elemType>::treeHeight() const
come height(root);
}
template <class elemType>
int binaryTreeType<elemType>::treeNodeCount() const
come nodeCount(root);
}
template <class elemType>
int binaryTreeType<elemType>::treeLeavesCount() const
come leavesCount(root);
}
template <class elemType>
void binaryTreeType<elemType>::copyTree
(nodeType<elemType>* &copiedTreeRoot,
nodeType<elemType>* otherTreeRoot)
} //end copyTree
template <class elemType>
void binaryTreeType<elemType>::inorder
(nodeType<elemType> *p) const
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.