# 栈和队列在遍历二叉树中的使用

1533人阅读 评论(0)

（1）栈在前三种中的使用

Each object placed on the stack contains:–A reference to a node;–A counterthat stores how many times the item has been popped from the stack (0 or 1 or 2).

pre-order:
After a node is popped, check its counter and do:
•If counter is 0:
–Increment counter to 1 and push back the node–Push the left child (if any) onto the stack (with counter=0).
–Pop
•If counter is 1:
–Increment counter to 2 and push back the node
–Push the right child (if any) onto the stack (with counter=0).
–Pop
•If counter is 2:
–Visit the node.
–Pop if stack is non-empty
•Repeat until stack becomes empty.
in-order:
•After a node is popped, check its counter and do:
•If counter is 0:
-Increment counter to 1 and push back the node–Push the left child (if any) onto the stack (with counter=0).
–Pop if stack is non-empty.
•If counter is 1:
–Visit the node.
–Push the right child (if any) onto the stack (with counter=0).
–Pop if stack is non-empty
•Repeat until stack becomes empty.

pre-order:
•After a node is popped from the stack:
•Visit the node.
•Push the right child (if any) onto the stack.
•Push the left child (if any) onto the stack.
•Pop again if stack non-empty.
•Repeat until the stack is empty.

（2）队列实现层序遍历

Start with an empty queue; enqueue(root); dequeue;
•After a node is dequeued do
•Visit node
•Enqueue left child (if any)
•Enqueue right child (if any)
•Dequeue again if queue is non-empty
•Repeat until the queue becomes empty

（1）先序遍历

void preOrderIter(struct node *root)
{
if (root == NULL) return;
stack<struct node *> s;
s.push(root);
while (!s.empty()) {
struct node *nd = s.top();
cout << nd->data << " ";
s.pop();
if (nd->right != NULL)
s.push(nd->right);
if (nd->left != NULL)
s.push(nd->left);
}
cout << endl;
}  

void preOrderIter2(struct node *root)
{
stack<struct node *> s;
while (root != NULL || !s.empty()) {
if (root != NULL) {
cout << root->data << " "; //访问结点并入栈
s.push(root);
root = root->left;         //访问左子树
} else {
root = s.top();            //回溯至父亲结点
s.pop();
root = root->right;        //访问右子树
}
}
cout << endl;
}  

（2）中序遍历

void inOrderIter(struct node *root)
{
stack<struct node *> s;
while (root != NULL || !s.empty()) {
if (root != NULL) {
s.push(root);
root = root->left;
}
else {
root = s.top();
cout << root->data << " ";  //访问完左子树后才访问根结点
s.pop();
root = root->right;        //访问右子树
}
}
cout << endl;
}  

（3）后序遍历

Push根结点到第一个栈s中。

void postOrderIter(struct node *root)
{
if (!root) return;
stack<struct node*> s, output;
s.push(root);
while (!s.empty()) {
struct node *curr = s.top();
output.push(curr);
s.pop();
if (curr->left)
s.push(curr->left);
if (curr->right)
s.push(curr->right);
}

while (!output.empty()) {
cout << output.top()->data << " ";
output.pop();
}
cout << endl;
}  

（4）层序遍历

void levelOrderIter(struct node* root)
{
if (!root) return;
queue<struct node *> currentLevel, nextLevel;
currentLevel.push(root);
while (!currentLevel.empty()) {
struct node *currNode = currentLevel.front();
currentLevel.pop();
if (currNode) {
cout << currNode->data << " ";
nextLevel.push(currNode->left);
nextLevel.push(currNode->right);
}
if (currentLevel.empty()) {
cout << endl;
swap(currentLevel, nextLevel);
}
}
}

void swap(queue<struct node *> &curr, queue<struct node*> &next)
{
while (!next.empty()) {
struct node *nd = next.front();
next.pop();
curr.push(nd);
}
}  

void levelOrderIter2(struct node *root)
{
if (!root) return;
queue<struct node*> nodesQueue;
int nodesInCurrentLevel = 1;
int nodesInNextLevel = 0;
nodesQueue.push(root);
while (!nodesQueue.empty()) {
struct node *currNode = nodesQueue.front();
nodesQueue.pop();
nodesInCurrentLevel--;
if (currNode) {
cout << currNode->data << " ";
nodesQueue.push(currNode->left);
nodesQueue.push(currNode->right);
nodesInNextLevel += 2;
}
if (nodesInCurrentLevel == 0) {
cout << endl;
nodesInCurrentLevel = nodesInNextLevel;
nodesInNextLevel = 0;
}
}
}  

个人资料
等级：
访问量： 3万+
积分： 1819
排名： 2万+
文章分类
最新评论