Created
March 2, 2018 18:06
-
-
Save kerolloz/4ca8b49625f59cc1f48a9778cefda608 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct tnode{ | |
unsigned int data; | |
tnode *left = NULL; | |
tnode *right = NULL; | |
}; | |
tnode* create_tnode(unsigned int x){ | |
tnode *new1 = new tnode; | |
new1 -> data = x; | |
new1 -> left = NULL; | |
new1 -> right = NULL; | |
return new1; | |
} | |
void insrtTnode(tnode *& t, unsigned int x){ | |
tnode *temp1 = t, *temp2 = NULL; | |
tnode *new1 = create_tnode(x); | |
if(t == NULL){ | |
t = new1; | |
} | |
else{ | |
while(temp1 != NULL){ | |
temp2 = temp1; | |
if(x <= temp1 -> data){ | |
temp1 = temp1 -> left; | |
} | |
else{ | |
temp1 = temp1 -> right; | |
} | |
} | |
if(x <= temp2 -> data) | |
temp2 -> left = new1; | |
else | |
temp2 -> right = new1; | |
} | |
} | |
void printPreorder(tnode *t){ | |
if(t == NULL) | |
return; | |
if(t != NULL){ | |
cout << " " << t -> data; | |
printPreorder(t -> left); | |
printPreorder(t -> right); | |
} | |
} | |
void printPostorder(tnode *t){ | |
if(t == NULL) | |
return; | |
if(t != NULL){ | |
printPostorder(t -> left); | |
printPostorder(t -> right); | |
cout << " " << t -> data; | |
} | |
} | |
void printInorder(tnode *t){ | |
if(t == NULL) | |
return; | |
if(t != NULL){ | |
printInorder(t -> left); | |
cout << " " << t -> data; | |
printInorder(t -> right); | |
} | |
} | |
int main() | |
{ | |
ios_base::sync_with_stdio(false); | |
cin.tie(NULL); | |
int c, n, cnt = 1; | |
unsigned int x; | |
tnode *t = NULL; | |
cin >> c; | |
while(c){ | |
cin >> n; | |
for(int i = 0; i < n; i++){ | |
cin >> x; | |
insrtTnode(t, x); | |
} | |
cout << "Case " << cnt << ":" << endl; | |
cout <<"Pre.:"; printPreorder(t); | |
cout << "\n"; | |
cout << "In..:"; printInorder(t); | |
cout << "\n"; | |
cout << "Post:"; printPostorder(t); | |
cout << "\n\n"; | |
t = NULL; | |
c--; | |
cnt++; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment