I need help to write this code in C++, I am struggling to find max node's parent's right child and max node's left child.
Node* maxValueNode(Node* node)
{
Node* current = node;
while (current && current->right !=
NULL)
current = current->right;
return current;
}
void deleteNode(BST* tree, Node* node, Node* parent)
{
//TODO - follow the lecture pseudocode to write the
deleteNode function
// - returns true if the value was deleted, false if
not
// - don't forget to free the memory for the node
you're deleting
if (node->left == NULL && node->right ==
NULL)
{
if (parent == NULL)
tree->root =
NULL;
else
parent->left
= NULL;
}
else if (node->left == NULL)
parent->left =
node->right;
else if (node->right == NULL)
parent->right =
node->left;
else
{
Node* max =
maxValueNode(node->left);
node->value = max->value;
Here is my current work.
Program that follows the pseudocode and answer your question accordingly to the program-
#include <iostream>
using namespace std;
// Data structure to store a Binary Search Tree node
struct Node {
int data;
Node *left, *right;
};
// Function to create a new binary tree node having given
key
Node* newNode(int key)
{
Node* node = new Node;
node->data = key;
node->left = node->right = nullptr;
return node;
}
// Function to perform inorder traversal of the BST
void inorder(Node *root)
{
if (root == nullptr)
return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
// Helper function to find minimum value node in subtree rooted
at curr
Node* maxValueNode(Node* node)
{
Node* current = node;
while (current && current->right != NULL)
current = current->right;
return current;
}
// Recursive function to insert an key into BST
Node* insert(Node* root, int key)
{
// if the root is null, create a new node an return
it
if (root == nullptr)
return newNode(key);
// if given key is less than the root node, recur
for left subtree
if (key < root->data)
root->left =
insert(root->left, key);
// if given key is more than the root node, recur
for right subtree
else
root->right =
insert(root->right, key);
return root;
}
// Iterative function to search in subtree rooted at curr &
set its parent
// Note that curr & parent are passed by reference
void searchKey(Node* &curr, int key, Node* &parent)
{
// traverse the tree and search for the key
while (curr != nullptr && curr->data !=
key)
{
// update parent node as current
node
parent = curr;
// if given key is less than the
current node, go to left subtree
// else go to right subtree
if (key < curr->data)
curr =
curr->left;
else
curr =
curr->right;
}
}
// Function to delete node from a BST
bool deleteNode(Node*& root, int key)
{
// pointer to store parent node of current node
Node* parent = nullptr;
// start with root node
Node* curr = root;
// search key in BST and set its parent
pointer
searchKey(curr, key, parent);
// return if key is not found in the
tree
if (curr == nullptr)
return false;
// Case 1: node to be deleted has no
children i.e. it is a leaf node
if (curr->left == nullptr && curr->right
== nullptr)
{
// if node to be deleted is not a
root node, then set its
// parent left/right child to
null
if (curr != root)
{
if
(parent->left == curr)
parent->left = nullptr;
else
parent->right = nullptr;
}
// if tree has only root node,
delete it and set root to null
else
root =
nullptr;
// deallocate the
memory
free(curr); // or
delete curr;
return true;
}
// Case 2: node to be deleted has two
children
else if (curr->left &&
curr->right)
{
// find its in-order successor
node
Node* successor =
maxValueNode(curr->left);
// store successor
value
int val =
successor->data;
// recursively delete
the successor. Note that the successor
// will have at-most one child
(right child)
deleteNode(root,
successor->data);
// Copy the value of
successor to current node
curr->data = val;
}
// Case 3: node to be deleted has only one
child
else
{
// find child node
Node* child = (curr->left)?
curr->left: curr->right;
// if node to be deleted
is not a root node, then set its parent
// to its child
if (curr != root)
{
if (curr ==
parent->left)
parent->left = child;
else
parent->right = child;
}
// if node to be deleted
is root node, then set the root to child
else
root =
child;
// deallocate the
memory
free(curr);
return true;
}
}
// main function
int main()
{
Node* root = nullptr;
bool x;
int keys[] = { 15, 10, 20, 8, 12, 16 };
for (int key : keys)
root = insert(root, key);
x = deleteNode(root, 1);
if(x == true)
cout<<"Deleted Successfully"<<endl;
else
cout<<"Not deleted key not found"<<endl;
inorder(root);
return 0;
}
I need help to write this code in C++, I am struggling to find max node's parent's right child and max node'...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....
using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...
C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node containing value ss. If there is no such node, it returns false. Otherwise, it deletes the node, check the balance of the tree, rebalance the tree if it is necessary. When you delete a node, consider three different scenarios: -The node is a leaf -The node has only ONE child subtree -The node has two child subtrees Important: When you implement this project, do...
Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) { p = new node; p->key=x; p->left=NULL; p->right=NULL; } else { //Cheak if the element to be inserted is smaller than the root. if (x < p->key) { //call the function itself with new parameters. insert(x,p->left); } //cheak if the alement to be inserted...
Write a C++ program to validate computer user-ids and passwords. A list of valid ids and passwords (unsorted) is read from a file and stored in a Binary Search Tree (BST) of UserInfo objects. When user-ids and passwords are entered during execution, this BST is searched to determine whether they are legal. Input (file): UserInfo records for valid users Input (keyboard): Ids and passwords of users logging in Output (screen): Messages indicating whether user-ids and passwords are valid, as well...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
1. Write a function in Tree class which returns true if and only if the tree satisfies the binary search tree property. The function’s header line is public boolean isValidBST() And in the attached code, you just need to finish the function after the comment: “//Instructor hint: please write your code here:” Make sure you execute your code, and the result in the main function after calling your function should be same as the prompt message I write. Clearly you...
Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...
Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...