URI 1466-Level Order Tree Traversal
/**
* @Author: Pranta Sarker
*
**/
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 1002;
// ***************** BST START ***************
struct Node
{
int key;
Node *left, *right;
};
Node *newNode(int data)
{
Node *tmp = new Node;
tmp->key = data;
tmp->left = tmp->right = NULL;
return tmp;
}
Node *Insert(Node *root, int data)
{
if(root == NULL) return newNode(data);
else if(root->key < data)
{
root->right = Insert(root->right , data);
}
else if(root->key > data)
{
root->left = Insert(root->left , data);
}
return root;
}
// **************** BST END **************************
//void preorder(Node *root)
//{
// if(root)
// {
// int x = root->key;
//
// cout << x << "; ";
//
// preorder(root->left);
// preorder(root->right);
// }
//}
int BST_Height(Node *root)
{
if(root == NULL) return 0;
int lft = BST_Height(root->left);
int rgt = BST_Height(root->right);
return max(lft, rgt) + 1;
}
bool space = false;
void PrintGivenOrder(Node *root, int level)
{
if(root == NULL) return;
else if(level == 1)
{
if(!space)
{
cout << root->key;
space = true;
}
else
{
cout << " " << root->key;
}
}
else if(level > 1)
{
PrintGivenOrder(root->left, level - 1);
PrintGivenOrder(root->right , level - 1);
}
}
void PrintLevelOrder(Node *root)
{
int height = BST_Height(root);
for(int i=1; i<=height; i++)
{
PrintGivenOrder(root, i);
}
}
int main()
{
fast;
int test , tc=0;
cin >> test;
while(test--)
{
Node *root = NULL;
space = false;
int i , x , n;
cin >> n;
cin >> x;
root = Insert(root, x);
cout << "Case " << ++tc << ":\n";
for(i=1; i<n; i++)
{
cin >> x;
Insert(root , x);
}
//preorder(root);
PrintLevelOrder(root);
cout << "\n\n";
}
return 0;
}
* @Author: Pranta Sarker
*
**/
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 1002;
// ***************** BST START ***************
struct Node
{
int key;
Node *left, *right;
};
Node *newNode(int data)
{
Node *tmp = new Node;
tmp->key = data;
tmp->left = tmp->right = NULL;
return tmp;
}
Node *Insert(Node *root, int data)
{
if(root == NULL) return newNode(data);
else if(root->key < data)
{
root->right = Insert(root->right , data);
}
else if(root->key > data)
{
root->left = Insert(root->left , data);
}
return root;
}
// **************** BST END **************************
//void preorder(Node *root)
//{
// if(root)
// {
// int x = root->key;
//
// cout << x << "; ";
//
// preorder(root->left);
// preorder(root->right);
// }
//}
int BST_Height(Node *root)
{
if(root == NULL) return 0;
int lft = BST_Height(root->left);
int rgt = BST_Height(root->right);
return max(lft, rgt) + 1;
}
bool space = false;
void PrintGivenOrder(Node *root, int level)
{
if(root == NULL) return;
else if(level == 1)
{
if(!space)
{
cout << root->key;
space = true;
}
else
{
cout << " " << root->key;
}
}
else if(level > 1)
{
PrintGivenOrder(root->left, level - 1);
PrintGivenOrder(root->right , level - 1);
}
}
void PrintLevelOrder(Node *root)
{
int height = BST_Height(root);
for(int i=1; i<=height; i++)
{
PrintGivenOrder(root, i);
}
}
int main()
{
fast;
int test , tc=0;
cin >> test;
while(test--)
{
Node *root = NULL;
space = false;
int i , x , n;
cin >> n;
cin >> x;
root = Insert(root, x);
cout << "Case " << ++tc << ":\n";
for(i=1; i<n; i++)
{
cin >> x;
Insert(root , x);
}
//preorder(root);
PrintLevelOrder(root);
cout << "\n\n";
}
return 0;
}
Comments
Post a Comment