binary search tree - java, plz use simple java language, netbeans 8.1 or 8.2 PLZ
ID: 3823240 • Letter: B
Question
binary search tree - java, plz use simple java language, netbeans 8.1 or 8.2
PLZ do the main method too
THANK U
Create a new Java Application that has the following methods:
1. A method that generate a binary search tree (BST) where the number of nodes and the range of the values are
from a file.
2. A recursive method to print the BST in preorder traversal
3. A recursive method to print the BST in post-order traversal
4. A recursive method to print the BST in in-order traversal
5. A recursive method to count the number of all nodes in BST
6. A recursive method to find the largest value in a BST
7. A method to find and return the smallest value in a BST.
8. A method to search for a given value V in a BST.
9. A method to count the number of ancestors of a given value V
10. A method to count the number of comparisons to decide whether a given value V is in a BST.
11. A method to count the number of leaf nodes in BST
12. A method to count how many nodes containing a given value V
13. A method to count the number of nodes that contain even numbers in a BST
14. A method to count the number of nodes with 2 children (child may be a single node or a sub-tree)
15. A method to print a BST sorted in reverse order
16. A method to print a BST in breadth first order
17. A method reconstruct a BST given its preorder traversal
18. A method to check if two BSTs are identical
19. A method mirror() to create a mirror image of BST
20. A method to check if a BST T1 is a mirror of T2
21. A method to find the common elements between two BSTs, and insert them in 3rd BST
Write the main method to test these methods described in 1 to 19
Explanation / Answer
auto placeNode(std::shared_ptr<BinaryNode<ItemType>> subTreePtr, std::shared_ptr<BinaryNode<ItemType>> newNode);
{
if (subTreePtr->data < newNode->data)
{
placeNode(subTreePtr->right, newNode);
}
else
{
placeNode(subtree->left, newNode);
}
}
auto findNode(std::shared_ptr<BinaryNode<ItemType>> treePtr,
const ItemType& target) const
{
if (treePtr == target)
return treePtr;
else if (target->data < treePtr->data)
return findNode(treePtr->left, target);
else
return findNode(treePtr->right, target);
}
bool isEmpty()
{
if rootPtr == NULL)
return true;
else
return false;
}
int findHeight(std::shared_ptr<BinaryNode<ItemType>> treePtr)
{
if (treePtr == NULL)
return 0;
int l = findHeight(treePtr->left);
int r = findHeight(treePtr->right);
return max (l, r) + 1;
}
int getHeight()
{
return findHeight(rootPtr);
}
void visit(ItemType &root)
{
if (root == NULL) return;
cout<<root->data<<" ";
}
void preorderTraverse(void visit(ItemType& ptr)) const
{
if (ptr == NULL)
return;
visit(ptr);
preorderTraverse(visit(ptr->left));
preorderTraverse(visit(ptr->right));
}
void inorderTraverse(void visit(ItemType& ptr)) const
{
if (ptr == NULL)
return;
inorderTraverse(visit(ptr->left));
visit(ptr);
inorderTraverse(visit(ptr->right));
}
void postTraverse(void visit(ItemType& ptr)) const
{
if (ptr == NULL)
return;
postTraverse(visit(tree->left));
postTraverse(visit(tree->right));
visit(tree);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.