#include <iostream>
#include <limits>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
struct Node{
int val;
struct Node* left;
struct Node* right;
};
typedef struct Node Node;
Node* CreatTree(){
Node* Head = new Node;
Node->left = NULL, Node->right = NULL;
Node->val = INT_MAX;
int n;
cin>>n;
for(int i = 1; i <= n; i++){
Node* newele = new Node;
Node* cur = Head, *parent = Head;
cin>>newele->val;
newele->left = 0;
newele->right = 0;
while(cur){
if(newele->val < cur->val){
parent = cur;
cur = cur->left;
}
else if(newele->val > cur->val){
parent = cur;
cur = cur->right;
}
}
if(!cur){
if(parent->val > newele->val){
parent->left = newele;
}
else if(parent->val < newele->val){
parent->right = newele;
}
}
}
return Head;
}
void PreOrderTraverse(Node* Head){
if(Head){
cout<<Head->val<<" ";
PreOrderTraverse(Head->left);
PreOrderTraverse(Head->right);
}
}
void InOrderTraverse(Node* Head){
if(Head){
InOrderTraverse(Head->left);
cout<<Head->val<<" ";
InOrderTraverse(Head->right);
}
}
void AfterOrderTraverse(Node* Head){
if(Head){
AfterOrderTraverse(Head->left);
AfterOrderTraverse(Head->right);
cout<<Head->val<<" ";
}
}
void SequenceTraverse(Node* Head){
queue<Node*>Q;
Q.push(Head->left);
while(!Q.empty()){
Node* cur = Q.front();
Q.pop();
cout<<cur->val<<" ";
if(cur->left){
Q.push(cur->left);
}
if(cur->right){
Q.push(cur->right);
}
}
}
int SearchInBiTree(Node* Head){
int key;
cin>>key;
Node* cur = Head->left;
while(cur){
if(cur->val < key){
cur = cur->right;
}
else if(cur->val > key){
cur = cur->left;
}
else if(cur->val == key){
return 1;
}
}
return 0;
}
void ExchangeTree(Node* Head){
if(Head){
swap(Head->left,Head->right);
ExchangeTree(Head->left);
ExchangeTree(Head->right);
}
}
int DepthOfTree(Node* Head){
if(Head == NULL)
return 0;
return max(DepthOfTree(Head->left),DepthOfTree(Head->right)) + 1;
}
int LeavesOfTree(Node* Head){
if(Head == NULL)
return 0;
if(Head->left == NULL && Head->right == NULL)
return 1;
return LeavesOfTree(Head->left)+LeavesOfTree(Head->right);
}
void InsertNewNodeInBiTree(Node* Head){
int key;
cin>>key;
Node* cur = Head->left *parent;
while(cur){
if(key < cur->val){
parent = cur;
cur = cur->left;
}
else if(key > cur->val){
parent = cur;
cur = cur->right;
}
else{
cout<<"the value already exist!"<<endl;
return;
}
}
Node* newele = new Node;
newele->val = key;
newele->left = NULL, newele->right = NULL;
if(parent->val < newele->val){
parent->right = newele;
}
else{
parent->left = newele;
}
}
int main(){
Node* Head = CreatTree();
PreOrderTraverse(Head->left);
cout<<endl;
InOrderTraverse(Head->left);
cout<<endl;
AfterOrderTraverse(Head->left);
cout<<endl;
cout<<SearchInBiTree(Head)<<endl;
cout<<SearchInBiTree(Head)<<endl;
InsertNewNodeInBiTree(Head);
PreOrderTraverse(Head->left);
cout<<endl;
InOrderTraverse(Head->left);
cout<<endl;
AfterOrderTraverse(Head->left);
cout<<endl;
InOrderTraverse(Head->left);
cout<<endl;
SequenceTraverse(Head->left);
cout<<endl;
ExchangeTree(Head->left);
PreOrderTraverse(Head->left);
cout<<endl;
InOrderTraverse(Head->left);
cout<<endl;
AfterOrderTraverse(Head->left);
cout<<endl;
ExchangeTree(Head->left);
PreOrderTraverse(Head->left);
cout<<endl;
InOrderTraverse(Head->left);
cout<<endl;
AfterOrderTraverse(Head->left);
cout<<endl;
cout<<DepthOfTree(Head->left)<<endl;
cout<<LeavesOfTree(Head->left)<<endl;
return 0;
}