Created
August 8, 2015 20:15
-
-
Save bragboy/b5f3c307431d0ddf216e to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
#include <queue> | |
using namespace std; | |
struct node | |
{ | |
int data; | |
struct node* left; | |
struct node* right; | |
}; | |
typedef struct node* nodeptr; | |
void display_preorder(nodeptr root) | |
{ | |
if(!root) return; | |
cout << root->data << " "; | |
display_preorder(root->left); | |
display_preorder(root->right); | |
} | |
void display_inorder(nodeptr root) | |
{ | |
if(!root) return; | |
display_inorder(root->left); | |
cout << root->data << " "; | |
display_inorder(root->right); | |
} | |
void display_postorder(nodeptr root) | |
{ | |
if(!root) return; | |
display_postorder(root->left); | |
display_postorder(root->right); | |
cout << root->data << " "; | |
} | |
void display_levelorder(nodeptr root) | |
{ | |
if(!root) return; | |
queue<nodeptr> myq; | |
myq.push(root); | |
while(!myq.empty()) | |
{ | |
nodeptr tmp = myq.front(); | |
myq.pop(); | |
cout << tmp->data << " "; | |
if(tmp->left) myq.push(tmp->left); | |
if(tmp->right) myq.push(tmp->right); | |
} | |
cout << endl; | |
} | |
int main() | |
{ | |
node g = {7, NULL, NULL}; | |
node f = {5, NULL, NULL}; | |
node e = {3, NULL, NULL}; | |
node d = {1, NULL, NULL}; | |
node c = {6, &f, &g}; | |
node b = {2, &d, &e}; | |
node a = {4, &b, &c}; | |
cout << "pre order :: "; display_preorder(&a); cout << endl; | |
cout << "in order :: "; display_inorder(&a); cout << endl; | |
cout << "post order :: "; display_postorder(&a); cout << endl; | |
cout << "level order :: "; display_levelorder(&a); | |
return 0; | |
} | |
//pre order :: 4 2 1 3 6 5 7 | |
//in order :: 1 2 3 4 5 6 7 | |
//post order :: 1 3 2 5 7 6 4 | |
//level order :: 4 2 6 1 3 5 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment