#include<bits/stdc++.h>
using namespace std;
typedef struct Node {
int date=0;
Node* left=NULL, *right=NULL;
}BTNode;
BTNode* createBTree(int a[],int n) {
BTNode* root=NULL,*c=NULL, * pa = NULL, *p=NULL;
root = new BTNode;
root->date = a[0];
root->left = root->right = NULL;
for (int i=1; i < n;i++){
p = new BTNode;
p->left = p->right = NULL;
p->date = a[i];
c = root;
while (c) {
pa = c;
if (c->date < p->date) c = c->right;
else c = c->left;
}
if (pa->date < p->date)pa->right = p;
else pa->left = p;
}
return root;
}
void Inorder(BTNode *root) {
if (root) {
Inorder(root->left);
cout << root->date << " ";
Inorder(root->right);
}
}
void Forder(BTNode *root) {
if (root){
cout << root->date << " ";
Forder(root->left);
Forder(root->right);
}
}
void Porder(BTNode *root) {
if (root) {
Porder(root->left);
Porder(root->right);
cout << root->date << " ";
}
}
int BTtree(BTNode *root) {
int c1, c2, c;
if (!root) {
return 0;
}
else {
c1 = BTtree(root->left);
c2 = BTtree(root->right);
c = c1 > c2 ? c1 : c2;
return c + 1;
}
}
BTNode* serach(BTNode *root,int key ) {
if (!root)return NULL;
else if (root->date == key)return root;
if (root->date < key)return serach(root->right, key);
else return serach(root->left,key);
}
int main() {
int a[] = {1,2,9,3,41,5,26,7};
BTNode* root = createBTree(a,8);
Inorder(root);
cout << endl<< BTtree(root);
cout << serach(root, 26)->date;
return 0;
}