Question: Write a C++ program to create a Binary Search Tree and search a user given value in that tree. The output should be “The is a child of ”
My Answer:
#include
using namespace std;
//Compiler version g++ 6.3.0
struct node {
int data;
node *left;
node *right;
node *parent;
};
node *createNode(int data) {
node *n = new node;
n->data = data;
n->left = NULL;
n->right = NULL;
n->parent = NULL;
return n;
}
void inorder(node *root){
if(root != NULL){
inorder(root->left);
if(root->parent == NULL)
cout parent->data;
inorder(root->right);
}
}
node *insert(node *root, int data) {
node *temp1 = createNode(data);
node *temp2 = new node;
temp2 = root;
if(root == NULL){
root = temp1;
} else {
while(temp2 != NULL) {
if(temp2->data right == NULL) {
temp2->right = temp1;
//node *rchild = insert(root->right, data);
//temp2->right = rchild;
//rchild->parent = temp2;
break;
}
temp2 = temp2->right;
} else if(temp2->data > data) {
if(temp2->left == NULL) {
temp2->left = temp1;
//node *lchild = insert(root->left, data);
//temp2->left = lchild;
//lchild->parent = temp2;
break;
}
temp2 = temp2->left;
}
}
}
return root;
}
void search(node *root, int data) {
node *temp = new node;
temp = root;
while(temp != NULL) {
if(temp->data == data) {
cout data > data)
temp = temp->left;
else
temp = temp->right;
}
cout >n;
search(root, n);
cout >c;
if(c == 'y' || c == 'Y')
goto up;
}
How Many Marks Out Of 30 Can I Get?
Thank You.
Answers
Answered by
1
Explanation:
I not understand this all are out of coverage area shop in 110 this lesson show me
Similar questions