<pre name="code" class="cpp">递归遍历树很容易实现,非递归前序和中序遍历也还简单,使用栈结构即可解决问题,稍微难点的是非递归后序遍历,具体实现如下:
#include <iostream>
#include <stack>
using namespace std;
typedef struct Node{
int data;
struct Node* pLeft;
struct Node* pRight;
}*pNode,node;
typedef struct BitTree{
pNode* pRoot;
}*pBitTree;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
pNode createNode(int data)
{
pNode pNewNode = new Node();
pNewNode->data = data;
pNewNode->pLeft = NULL;
pNewNode->pRight = NULL;
return pNewNode;
}
pBitTree* createBitTree()
{
pBitTree pNewBTree = new BitTree();
pNode root = new Node();
root->data = 17;
root->pLeft = NULL;
root->pRight = NULL;
pNewBTree->pRoot = &root;
/******17********************/
pNode node1 = createNode(18); //****/ \*******/
root->pLeft = node1; //****/ \******/
pNode node2 = createNode(19); //****18 19*****/
root->pRight = node2; //*****/ \ / \****/
//*****20 21 22 23**/
pNode node3 = createNode(20); //*****/ \ /********/
node1->pLeft = node3; //*****24 25 26*****/
pNode node4 = createNode(21);
node1->pRight = node4;
pNode node5 = createNode(22);
node2->pLeft = node5;
pNode node6 = createNode(23);
node2->pRight = node6;
pNode node7 = createNode(24);
node4->pLeft = node7;
pNode node8 = createNode(25);
node5->pRight = node8;
pNode node9 = createNode(26);
node6->pLeft = node9;
return &pNewBTree;
}
//递归先序遍历
void recursivePreOrderTraversal(pNode root)
{
if(root == NULL)
return;
cout << root->data << " ";
recursivePreOrderTraversal(root->pLeft);
recursivePreOrderTraversal(root->pRight);
}
//递归中序遍历
void recursiveInOrderTraversal(pNode root)
{
if(root == NULL)
return;
recursiveInOrderTraversal(root->pLeft);
cout << root->data << " ";
recursiveInOrderTraversal(root->pRight);
}
//递归后序遍历
void recursiveAreOrderTraversal(pNode root)
{
if(root == NULL)
return;
recursiveAreOrderTraversal(root->pLeft);
recursiveAreOrderTraversal(root->pRight);
cout << root->data << " ";
}
//非递归先序遍历
void nonRecursivePreOrderTraversal(pNode root)
{
if(root == NULL)
return;
stack<pNode> nodeStack;
pNode curNode = root;
while(curNode != NULL || !nodeStack.empty())
{
while(curNode != NULL)
{
cout << curNode->data << " ";
nodeStack.push(curNode);
curNode = curNode->pLeft;
}
curNode = nodeStack.top();
nodeStack.pop();
curNode = curNode->pRight;
}
}
//删除树结点
void deleteNode(pNode theNode)
{
if(theNode->pLeft != NULL)
deleteNode(theNode->pLeft);
if(theNode->pRight != NULL)
deleteNode(theNode->pRight);
if(theNode->pLeft == NULL && theNode->pRight == NULL)
{
delete(theNode);
theNode = NULL;
}
}
//删除树结构
void deleteBitTree(pBitTree theBitTree)
{
if(theBitTree == NULL)
return;
if(*theBitTree->pRoot == NULL)
return;
deleteNode(*theBitTree->pRoot);
delete theBitTree;
}
//非递归中序遍历
void nonRecursiveInOrderTraversal(pNode root)
{
if(root == NULL)
return;
stack<pNode> nodeStack;
pNode curNode = root;
while(curNode != NULL || !nodeStack.empty())
{
while(curNode != NULL)
{
nodeStack.push(curNode);
curNode = curNode->pLeft;
}
curNode = nodeStack.top();
nodeStack.pop();
cout << curNode->data << " ";
curNode = curNode->pRight;
}
}
//非递归后序遍历
void nonRecursiveAreOrderTraversal(pNode root)
{
pNode preVisited = NULL;
if(root == NULL)
return;
stack<pNode> nodeStack;
pNode curNode = root;
while(curNode != NULL || !nodeStack.empty())
{
while(curNode != NULL)
{
nodeStack.push(curNode);
curNode = curNode->pLeft;
}
curNode = nodeStack.top();
curNode = curNode->pRight;
if(curNode == NULL || curNode == preVisited) //此时,左子树已经为空,若右子树还为空则输出该结点,或者
{ //该结点的左子树和右子树均访问过,则输出该结点
curNode = nodeStack.top();
cout << curNode->data << " ";
preVisited = curNode;
nodeStack.pop();
curNode = NULL;
}
}
}
int main(int argc, char *argv[]) {
pBitTree* theBitTree = createBitTree();
pNode root = *(*theBitTree)->pRoot;
cout << "递归前序遍历:";
recursivePreOrderTraversal(root);
cout << endl;
cout << "递归中序遍历:";
recursiveInOrderTraversal(root);
cout << endl;
cout << "递归后序遍历:";
recursiveAreOrderTraversal(root);
cout << endl;
cout << "非递归前序遍历:";
nonRecursivePreOrderTraversal(root);
cout << endl;
cout << "非递归中序遍历:";
nonRecursiveInOrderTraversal(root);
cout << endl;
cout << "非递归后序遍历:";
nonRecursiveAreOrderTraversal(root);
cout << endl;
deleteBitTree(*theBitTree);
return 0;
}