C function to mirror binary tree.
C Function implementation :
1 2 3 4 5 6 7 8 9 10 11 |
/* C Function to Mirror Binary Tree */ void mirrorbst(struct Tnode *Troot){ struct Tnode *tmp; if(Troot==NULL) return; mirrorbst(Troot->left); mirrorbst(Troot->right); tmp=Troot->left; Troot->left=Troot->right; Troot->right=tmp; } |
Complete C Code implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
/* Binary Search tree code by condingstreet.com */ #include<stdio.h> #include<stdlib.h> struct Tnode { int val; struct Tnode *left,*right; }; int create_node(int val,struct Tnode **node){ struct Tnode *tmp; tmp=malloc(sizeof(struct Tnode)); if(tmp==NULL) return 0; tmp->val=val; tmp->left=NULL; tmp->right=NULL; (*node)=tmp; return 1; } void insert_node(struct Tnode **Troot,struct Tnode **node){ struct Tnode *parent,*tmp; int val; if((*node)==NULL) return; if((*Troot)==NULL){ (*Troot)=(*node); return; } tmp=(*Troot); val=(*node)->val; while(tmp!=NULL){ parent=tmp; if(val<tmp->val) tmp=tmp->left; else tmp=tmp->right; } if(val<parent->val){ parent->left=(*node); } else parent->right=(*node); return; } void inorder(struct Tnode *node){ if(node!=NULL){ inorder(node->left); printf(" %d ",node->val); inorder(node->right); } } /* C Function to Mirror Binary Tree */ void mirrorbst(struct Tnode *Troot){ struct Tnode *tmp; if(Troot==NULL) return; mirrorbst(Troot->left); mirrorbst(Troot->right); tmp=Troot->left; Troot->left=Troot->right; Troot->right=tmp; } int main(){ struct Tnode *root=NULL,*node; int key,ival; do{ printf("\n[1] Insert value"); printf("\n[2] Print inorder "); printf("\n[3] Mirror Bst "); printf("\n[4] Exit"); printf("\nEnter the input key : "); scanf("%d",&key); switch(key){ case 1: printf("\n Enter the value to be inserted :"); scanf("%d",&ival); if(create_node(ival,&node)) insert_node(&root,&node); else printf("\n Memory is Full !! "); break; case 2: inorder(root); break; case 3: mirrorbst(root); break; case 4: break; default: printf("\t Enter the Correct key !!!!! "); break; } }while(key!=4); printf("\n Thank you for using codingstreet.com 's datastructure solution "); return 0; } |