Make all values of tree negative -ive

#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 makeneg(Node *r){
  if (r == NULL) return;
  else {
   makeneg(r->left);
   r->data=r->data*(-1);
   makeneg(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\n";
 b.inorder(b.root);
 b.makeneg(b.root);
 cout<<"\n\nAfter Making all  values Nagitive -ive: \n\n ";
 b.inorder(b.root);
 getch();

}


No comments

Powered by Blogger.