#include <iostream>
#include <stack>
#include <queue>
using namespace std;
template<typename Type>
struct Node
{
Node* right;
Node* left;
Type data;
Node(Type tp = Type()) :data(tp),right(NULL),left(NULL){}
};
template<typename Type>
class MT
{
public:
MT(const char *s,Type tp)
{
root = NULL;
flags = tp;
Insert(root,s);
}
void Insert(Node<Type> *&t,const char *& s )
{
if (*s == flags)
{
t = NULL;
return ;
}
else
{
t = new Node<Type>(*s);
Insert(t->left,++s);
Insert(t->right, ++s);
}
}
void A()
{
Node<Type> *t = root;
if (t == NULL)return;
queue<Node<Type> *> st;
st.push(t);
while (st.empty() == false)
{
Node<Type> *t = st.front();
st.pop();
cout << t->data << " ";
if (t->left != NULL)
{
st.push(t->left);
}
if (t->right != NULL)
{
st.push(t->right);
}
}
}
int B()
{
return B(root);
}
int C(int x)
{
return C(root,x);
}
void Printf()
{
Printf(root);
}
void D()
{
}
bool IsHere(char ch)
{
return IsHere(root,ch);
}
char Parent(char ch1,char ch2)
{
return Parent(root,ch1,ch2);
}
bool IsBanlance()
{
return IsBanlance(root);
}
int GetLengthMax()
{
return GetLengthMax(root);
}
int GetLg()
{
return GetLg(root);
}
private:
int GetLg(Node<Type> *t)
{
if (t == NULL)return 0;
else
{
return GetLg(t->left) > GetLg(t->right) ? GetLg(t->left) + 1 : GetLg(t->right) + 1;
}
}
bool IsBanlance(Node<Type> *t)
{
if (t == NULL)return true;
int len = GetLengthMax(root);
if (len >= 0 && len <= 1)return true;
else return false;
}
int GetLengthMax(Node<Type>* t)
{
int count = 0;
int mincount = 0x7fffffff;
int maxcount = 0xffffffff;
Getlow(root, count, mincount);
count = 0;
GetHigh(root, count, maxcount);
return maxcount - mincount;
return 0;
}
int Getlow(Node<Type> *t,int count,int &mincount)
{
if (t == NULL)return 0;
if (root->left == NULL || root->right == NULL)
{
mincount = 0;
return 0;
}
if (t == root)
{
count += 1;
}
if ((t->left == NULL && t->right == NULL))
{
mincount = count > mincount ? mincount : count;
return 0;
}
else
{
count++;
Getlow(t->left, count, mincount);
Getlow(t->right, count, mincount);
}
return 0;
}
int GetHigh(Node<Type> *t,int count,int &maxcount)
{
if (t == NULL)return 0;
else
{
count++;
GetHigh(t->left,count,maxcount);
GetHigh(t->right, count, maxcount);
maxcount = count > maxcount ?
count : maxcount;
}
return 0;
}
char Parent(Node<Type> *t,char ch1,char ch2)
{
if (t != NULL)
{
stack<Node<Type> *> st;
st.push(t);
while (st.empty() == false)
{
if (IsHere(t->left, ch1) == 1 && IsHere(t->left,ch2) == 1)
{
st.push(t->left);
t = t->left;
continue;
}
if (IsHere(t->right, ch1) == 1 && IsHere(t->right, ch2) == 1)
{
st.push(t->right);
t = t->right;
continue;
}
break;
}
while (st.empty() == false)
{
Node<Type> *p = st.top();
st.pop();
cout << p->data << endl;
}
}
return 'a';
}
bool IsHere(Node<Type> *t,char ch)
{
if (t == NULL)return false;
if (t->data == ch)return true;
else
{
if (IsHere(t->left, ch))return true;
return IsHere(t->right,ch);
}
}
int C(Node<Type> *t,int x)
{
if (x<1 || t == NULL)return 0;
if (x == 1)
{
return 1;
}
else
{
x--;
return C(t->left, x) + C(t->right, x);
}
}
int B(Node<Type>* t)
{
if (t == NULL)return 0;
if (t->right == NULL && t->left == NULL)
{
return 1;
}
else
{
return B(t->left)+B(t->right);
}
}
void Printf(Node<Type> *t)
{
if (t == NULL)
{
return;
}
else
{
cout << t->data << " ";
Printf(t->left);
Printf(t->right);
}
}
private:
Type flags;
Node<Type> *root;
};
int main()
{
char s[] = "ab##c#d#e#f##";
//char s[] = "abcd####e#f#g#h##";
MT<char> mt(s,'#');
//mt.A();
//cout << mt.C(4) << endl;
//cout <<mt.IsHere('e') << endl;
//cout <<mt.Parent('c','d')<<endl;
//mt.D();
cout << mt.GetLg() << endl;
//cout << mt.GetLengthMax() << endl;
//cout<<mt.IsBanlance()<<endl;
//mt.Printf();
return 0;
}