层序遍历二叉树,使用队列
分行打印二叉树,使用队列
之字形打印二叉树,使用两个栈
#include<iostream>
#include<deque>
#include<stack>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
//层序遍历,用到了队列
void PrintFromTopToBottom(BinaryTreeNode* pTreeRoot)
{
if (!pTreeRoot)
return;
deque<BinaryTreeNode*> dequeTreeNode;
dequeTreeNode.push_back(pTreeRoot);
while (!dequeTreeNode.empty())
{
BinaryTreeNode* pNode = dequeTreeNode.front();
dequeTreeNode.pop_front();
cout << pNode->m_nValue << " ";
if (pNode->m_pLeft)
dequeTreeNode.push_back(pNode->m_pLeft);
if (pNode->m_pRight)
dequeTreeNode.push_back(pNode->m_pRight);
}
}
//分行从上到下打印二叉树
void Print(BinaryTreeNode* pRoot)
{
if (pRoot == nullptr)
return;
deque<BinaryTreeNode*> nodes;
nodes.push_back(pRoot);
int nextLevel = 0;
int toBePrint = 1;
while (!nodes.empty())
{
BinaryTreeNode* pNode = nodes.front();
cout << pNode->m_nValue << " ";
if (pNode->m_pLeft != nullptr)
{
nodes.push_back(pNode->m_pLeft);
++nextLevel;
}
if (pNode->m_pRight != nullptr)
{
nodes.push_back(pNode->m_pRight);
++nextLevel;
}
nodes.pop_front();
--toBePrint;
if (toBePrint == 0)
{
toBePrint = nextLevel;
nextLevel = 0;
cout << endl;
}
}
}
//之字形打印二叉树
void Print2(BinaryTreeNode* pRoot)
{
if (pRoot == nullptr)
return;
stack<BinaryTreeNode*> leves[2];
int current = 0;
int next = 1;
leves[current].push(pRoot);
while (!leves[0].empty() || !leves[1].empty())
{
BinaryTreeNode* pNode = leves[current].top();
leves[current].pop();
cout << pNode->m_nValue << " ";
if (current == 0)
{
if (pNode->m_pLeft != nullptr)
leves[next].push(pNode->m_pLeft);
if (pNode->m_pRight != nullptr)
leves[next].push(pNode->m_pRight);
}
else
{
if (pNode->m_pRight != nullptr)
leves[next].push(pNode->m_pRight);
if (pNode->m_pLeft != nullptr)
leves[next].push(pNode->m_pLeft);
}
if (leves[current].empty())
{
cout << endl;
current = 1 - current;
next = 1 - next;
}
}
}
void creatTree(BinaryTreeNode* root, BinaryTreeNode* left, BinaryTreeNode* right)
{
root->m_pLeft = left;
root->m_pRight = right;
}
int main()
{
/* 树
root1(1)
a(2) b(3)
c(4) d(5) e(6) f(7)
*/
BinaryTreeNode* root1 = new BinaryTreeNode();
root1->m_nValue = 1;
root1->m_pLeft = nullptr;
root1->m_pRight = nullptr;
BinaryTreeNode* a = new BinaryTreeNode();
a->m_nValue = 2;
a->m_pLeft = nullptr;
a->m_pRight = nullptr;
BinaryTreeNode* b = new BinaryTreeNode();
b->m_nValue = 3;
b->m_pLeft = nullptr;
b->m_pRight = nullptr;
BinaryTreeNode* c = new BinaryTreeNode();
c->m_nValue = 4;
c->m_pLeft = nullptr;
c->m_pRight = nullptr;
BinaryTreeNode* d = new BinaryTreeNode();
d->m_nValue = 5;
d->m_pLeft = nullptr;
d->m_pRight = nullptr;
BinaryTreeNode* e = new BinaryTreeNode();
e->m_nValue = 6;
e->m_pLeft = nullptr;
e->m_pRight = nullptr;
BinaryTreeNode* f = new BinaryTreeNode();
f->m_nValue = 7;
f->m_pLeft = nullptr;
f->m_pRight = nullptr;
creatTree(root1, a, b);
creatTree(a, c, d);
creatTree(b, e, f);
PrintFromTopToBottom(root1); // 层序遍历
cout << endl;
Print(root1); // 分行从上到下打印二叉树
cout << endl;
Print2(root1);//之字形打印二叉树
cin.get();
return 0;
}