Question

In: Computer Science

c++ Binary Search Tree: find parent of a node please insert comment to explain what the...

c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};

Solutions

Expert Solution

BST: -

     1
   /   \
  2     3
 / \
4   5
#include<bits/stdc++.h>
using namespace std;
 
/*Binary Search Tree having data with two pointers, i.e., left and right*/
struct Node{
    int data;
    struct Node *left, *right;
    
    Node(int data) {    //constructor to initialize a node
        this->data = data;
        left = right = NULL;
    }
};
 
//function to find  parent of a given node recursively
void findParentNode(struct Node* node, int value, int parent){
    if (node == NULL)           //if tree is empty, the return
        return;
    
 
    //If found the given node
    if (node->data == value){
        if(parent == -1){
                cout<<"Given node has no parent."<<endl;    //if given node has no parent
                }
                else
                cout<<"Parent of given node "<<value<<" is "<<parent<<endl;               //display the parent node
    }
    
    else{
        //Recursively look for the node in left subtree and right subtree
        findParentNode(node->left, value, node->data);
        findParentNode(node->right, value, node->data);
    }
}
 

int main(){
    struct Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->left->right = new Node(5);
    
    
    int node;           //user input
    cout<<"Enter the node: ";
    cin>>node;
    cout<<endl;
 
    findParentNode(root, node, -1);
    return 0;
}

Sample Run: -

The program finds recursively finds the given node in the binary search tree. Each time, the head pointer(Node* node) and parent node data is re-assigned to keep track. To check for the given node, there's an if condition. When it's true, it displays the current node's parent stored by variable parent.


Related Solutions

Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode* find(int i) { // implement } If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right;...
in C++, given a binary search tree of ints, in which each node contains a size...
in C++, given a binary search tree of ints, in which each node contains a size parameter (also an int), explain, in English, not code, how you would find the median element of the tree in theta(log N) time.
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
In C++: Write a binary search tree and include the functions to find the number of...
In C++: Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal.
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
write a method in java for a binary search tree that receives a node as input...
write a method in java for a binary search tree that receives a node as input and returns the successor node.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT