Display only even Values from 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 inorder(Node *r){
  if (r == NULL) return;
  else {
   inorder(r->left);
   if(r->data % 2 == 0){
   cout << r->data <<"  ";             }
   inorder(r->right);
   }
 }



};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
 Node b;
 clrscr();
 for(int i=0;i<=10;i++){
 b.insert(A[i]); }
 cout<<"\nIn-Order display :\nDisplaing only Even values:\n\n";
 b.inorder(b.root);
 getch();

}

0 comments:

Post a Comment

My Instagram