Insert 100 in all Leaf nodes and 10 in all non leaf nodes using BST

Insert 100 in all Leaf nodes and 10 in all non leaf nodes using BST in C++

#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right,*q,*p;
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->left==NULL && r->right==NULL){
r->data=100;}
else r->data=10;
cout << r->data <<"  ";
inorder(r->right);
} }

void _inorder(Node *r){
if (r == NULL) return;
else {
      _inorder(r->left);
cout << r->data <<"  ";
if(r->left==NULL && r->right==NULL){
r->data=100;}
_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<<"\n\n\nSimple Tree: BST inorder:\n \n";
b._inorder(b.root);
cout<<"\n\n\nAfter Puting 100 in all leaf Node:\n \n";
b._inorder(b.root);
cout<<"\n\n\nAfter Puting 10 in all Non leaf Node:\n\n ";
b.inorder(b.root);
getch();

}



No comments

Powered by Blogger.