#include <iostream>
using namespace std;
class Node
{
public:
char m_value; //数据
Node* m_left; //指向左孩子
Node* m_right; //指向右孩子
public:
Node(const char v = '*') :m_value{ v }, m_left{ nullptr }, m_right{ nullptr } {}
};
class BTree
{
using root = Node*;
private:
root m_root; //根结点
char m_flag; //标记位
public:
BTree() :m_root{ nullptr }, m_flag{ '*' } {}
root Create(const char*& str)
{
if (*str == '*')
{
return nullptr;
}
else
{
Node* root = new Node{ *str };
root->m_left = Create(++str);
root->m_right = Create(++str);
return root;
}
}
root& GetRoot()
{
return m_root;
}
void PreOrder(const root const bt) const
{
if (bt == nullptr)
{
return;
}
else
{
cout << bt->m_value << " ";
PreOrder(bt->m_left);
PreOrder(bt->m_right);
}
}
void InOrder(const root const bt) const
{
if (bt == nullptr)
{
return;
}
else
{
InOrder(bt->m_left);
cout << bt->m_value << " ";
InOrder(bt->m_right);
}
}
void PostOrder(const root const bt) const
{
if (bt == nullptr)
{
return;
}
else
{
PostOrder(bt->m_left);
PostOrder(bt->m_right);
cout << bt->m_value << " ";
}
}
int Size(const root const bt)const
{
if (bt == nullptr)
{
return 0;
}
else
{
return Size(bt->m_left) + Size(bt->m_right) + 1;
}
}
int Height(const root const bt)const
{
if (bt == nullptr)
{
return 0;
}
else
{
int lh = Height(bt->m_left);
int rh = Height(bt->m_right);
return lh > rh ? lh + 1 : rh + 1;
}
}
};
int main()
{
BTree bt;
const char* str = "abd**eh***cf*i**g**";
bt.GetRoot() = bt.Create(str);
bt.PreOrder(bt.GetRoot());
cout << "\n";
bt.InOrder(bt.GetRoot());
cout << "\n";
bt.PostOrder(bt.GetRoot());
cout << endl;
cout << bt.Height(bt.GetRoot()) << endl;
cout << bt.Size(bt.GetRoot()) << endl;
return 0;
}