Make all Odd values Even in a tree
#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;
void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}
void makeeven(Node *r){
if (r == NULL) return;
else {
makeeven(r->left);
if(r->data % 2 != 0){
r->data=r->data+1; }
makeeven(r->right);
}
}
void inorder(Node *r){
if (r == NULL) return;
else {
inorder(r->left);
cout << r->data <<" ";
inorder(r->right);
}
}
};
void main()
{
int c,A[]={19,54,63,22,75,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
cout<<"\nIn-Order display :\n";
b.inorder(b.root);
b.makeeven(b.root);
cout<<"\nAfter Making all Odd values even: \n ";
b.inorder(b.root);
getch();
}
#include<conio.h>
class Node{
private:
int data;
Node* right;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;
void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}
void makeeven(Node *r){
if (r == NULL) return;
else {
makeeven(r->left);
if(r->data % 2 != 0){
r->data=r->data+1; }
makeeven(r->right);
}
}
void inorder(Node *r){
if (r == NULL) return;
else {
inorder(r->left);
cout << r->data <<" ";
inorder(r->right);
}
}
};
void main()
{
int c,A[]={19,54,63,22,75,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
cout<<"\nIn-Order display :\n";
b.inorder(b.root);
b.makeeven(b.root);
cout<<"\nAfter Making all Odd values even: \n ";
b.inorder(b.root);
getch();
}
Post a Comment