基本结构及其工具
typedef int DataBype;
#define PRINTDIVIDE cout << endl << "##########################################" << endl;
class Node
{
public:
DataBype v;
Node * l;
Node * r;
Node()
{
v = 0;
l = NULL;
r = NULL;
}
private:
};
typedef Node * PN;
template<class T>
void exchange(T & f, T & s)
{
T t = f;
f = s;
s = t;
};
使用vector初始化二叉树
void BS::initwithivsbylevel( vector<DataBype> vs )
{
queue<PN> tqn;
if (!vs.empty())
{
m_nroot = new Node;
m_nroot->v = vs[0];
tqn.push(m_nroot);
for (int i = 1; i < vs.size();)
{
PN cn = tqn.front();
tqn.pop();
PN tn = new Node;
tn->v = vs[i ++];
cn->l = tn;
tqn.push(tn);
if (i < vs.size())
{
tn = new Node;
tn->v = vs[i ++];
cn->r = tn;
tqn.push(tn);
}
}
}
}
打印第level 层
void BS::printlevel(PN root, int level )
{
if (NULL == root)
return;
if (0 == level)
cout << root->v << " ";
printlevel(root->l, level - 1);
printlevel(root->r, level - 1);
}
得到树的深度
int BS::getlevel(const PN root )
{
if (NULL == root)
return 0;
return max(getlevel(root->l), getlevel(root->r)) + 1;
}
层遍历打印二叉树
void BS::printself()
{
int alllevel = getlevel(m_nroot);
for (int i = 0; i < alllevel; ++ i)
{
printlevel(m_nroot, i);
cout << endl;
}
}
递归先序、中序、后序遍历二叉树
void BS::preorder_recursion( const PN root )
{
if (NULL != root)
{
cout << root->v << " ";
preorder_recursion(root->l);
preorder_recursion(root->r);
}
}
void BS::inorder_recursion( const PN root )
{
if (NULL != root)
{
inorder_recursion(root->l);
cout << root->v << " ";
inorder_recursion(root->r);
}
}
void BS::postorder_recursion( const PN root )
{
if (NULL != root)
{
postorder_recursion(root->l);
postorder_recursion(root->r);
cout << root->v << " ";
}
}
非递归先序遍历
void BS::preorder_no_recursion( const PN root )
{
stack<PN> spn;
PN tpn = root;
while (NULL != tpn || !spn.empty())
{
while (NULL != tpn)
{
cout << tpn->v << " ";
spn.push(tpn);
tpn = tpn->l;
}
if (!spn.empty())
{
tpn = spn.top();
spn.pop();
tpn = tpn->r;
}
}
}
非递归中序遍历
void BS::inorder_no_recursion( const PN root )
{
stack<PN> spn;
PN tpn = root;
while (NULL != tpn || !spn.empty())
{
while (NULL != tpn)
{
spn.push(tpn);
tpn = tpn->l;
}
if (!spn.empty())
{
tpn = spn.top();
cout << tpn->v << " ";
spn.pop();
tpn = tpn->r;
}
}
}
非递归后序遍历
void BS::postorder_no_recursion( const PN root )
{
stack<PN> spn;
PN tpn = NULL;
PN pre = NULL;
spn.push(root);
while (!spn.empty())
{
tpn = spn.top();
if ((NULL == tpn->l && NULL == tpn->r) || ( pre != NULL && (pre == tpn->l || pre == tpn->r) ))
{
cout << tpn->v << " ";
pre = tpn;
spn.pop();
}
else
{
if (NULL != tpn->r)
spn.push(tpn->r);
if (NULL != tpn->l)
spn.push(tpn->l);
}
}
}
二叉树反转(递归和非递归)
void BS::reverse_recursion( PN root )
{
if (NULL != root)
{
exchange(root->l, root->r);
reverse_recursion(root->l);
reverse_recursion(root->r);
}
}
void BS::reverse_no_recursion( PN root )
{
queue<PN> qpn;
PN tpn = root;
qpn.push(root);
while (!qpn.empty())
{
tpn = qpn.front();
qpn.pop();
exchange(tpn->l, tpn->r);
if (NULL != tpn->l)
qpn.push(tpn->l);
if (NULL != tpn->r)
qpn.push(tpn->r);
}
}
得到第k层节点个数
int BS::countklevel( const PN root, int k )
{
if (NULL == root || k < 0)
return 0;
if (0 == k)
return 1;
return countklevel(root->l, k - 1) + countklevel(root->r, k - 1);
}
是否包含节点
bool BS::find_node( const PN root, const DataBype & o)
{
if (NULL == root || (o != root->v && !find_node(root->l, o) && !find_node(root->r, o)))
return false;
return true;
}
最近公共祖先
PN BS::getLCP(const PN root, const DataBype & f, const DataBype & s)
{
if (NULL == root || f == root->v || s == root->v)
return root;
if (find_node(root->l, f))
{
if (find_node(root->r, s))
return root;
return getLCP(root->l, f, s);
}
if (find_node(root->r, f))
{
if (find_node(root->l, s))
return root;
return getLCP(root->r, f, s);
}
return NULL;
}
二叉树转线性表
PN BS::to_linklist_recursion( PN root)
{
if (NULL == root)
return NULL;
PN tl = to_linklist_recursion(root->l);
PN head = root;
if (NULL != tl)
{
head = tl;
while (tl->r)
tl = tl->r;
tl->r = root;
root->l = tl;
}
PN tr = to_linklist_recursion(root->r);
root->r = tr;
if (NULL != tr)
tr->l = root;
return head;
}