• 递归 和 非递归 遍历二叉树


    1 二叉树结点

    2 先序遍历二叉树

    3 中序遍历二叉树

    4 后序遍历二叉树

    5 测试样例

    1 二叉树结点

    struct BinaryTreeNode

    {

    int m_nValue;

    BinaryTreeNode *m_pLeft;

    BinaryTreeNode *m_pRight;

    };

    2 先序遍历二叉树 

    先序遍历二叉树递归算法定义为:

    若二叉树为空,则空操作;否则(1)访问根节点;(2)先序遍历左子树;(3)后序遍历右子树;

    代码如下:

    /* 递归先序遍历 */
    void PreOrderTraverseTree(BinaryTreeNode *pRoot)
    {
    	if (pRoot != NULL)
    	{
    		cout << pRoot->m_nValue << " ";
    		PreOrderTraverseTree(pRoot->m_pLeft);
    		PreOrderTraverseTree(pRoot->m_pRight);
    	}
    }


    先序遍历二叉树的非递归算法定义为:

    我们首先创建一个堆栈stack<BinaryTreeNode *>nodes,首先让根节点入栈。

    然后进入一个while循环,即 while(!nodes.empty()),每次弹出一个节点。遍历该节点的值,然后将其节点的右孩子入栈,再次将其节点的左孩子入栈。

    直至while循环结束。

    代码如下:

    /* 非递归先序遍历 */
    void PreOrderTraverseTreeNoRecursive(BinaryTreeNode *pRoot)
    {
    	if (pRoot == NULL)
    	{
    		return;
    	}
    
    	stack<BinaryTreeNode *> nodes;
    	nodes.push(pRoot);
    	while (!nodes.empty())
    	{
    		BinaryTreeNode *pNode = nodes.top();
    		nodes.pop();
    		cout << pNode->m_nValue << " ";
    
    		if (pNode->m_pRight != NULL)
    		{
    			nodes.push(pNode->m_pRight);
    		}
    		if (pNode->m_pLeft != NULL)
    		{
    			nodes.push(pNode->m_pLeft);
    		}
    	}
    }
    


    3 中序遍历二叉树

    中序遍历二叉树的递归算法如下:

    若二叉树为空,则空操作;否则(1)中序遍历左子树;(2)访问根节点;(3)中序遍历右子树;

    代码如下:

    /* 递归中序遍历 */
    void InOrderTraverseTree(BinaryTreeNode *pRoot)
    {
    	if (pRoot != NULL)
    	{
    		InOrderTraverseTree(pRoot->m_pLeft);
    		cout << pRoot->m_nValue << " ";
    		InOrderTraverseTree(pRoot->m_pRight);
    	}
    }


    中序遍历二叉树的非递归算法如下:

    /* 非递归中序遍历 */
    void InOrderTraverseTreeNoRecursive(BinaryTreeNode *pRoot)
    {
    	if (pRoot == NULL)
    	{
    		return;
    	}
    
    	stack<BinaryTreeNode *> nodes;
    	BinaryTreeNode *pCurrent = pRoot;
    	while (!nodes.empty() || pCurrent != NULL)
    	{
    		if (pCurrent != NULL)
    		{
    			nodes.push(pCurrent);
    			pCurrent = pCurrent->m_pLeft;
    		}
    		else
    		{
    			pCurrent = nodes.top();
    			nodes.pop();
    			cout << pCurrent->m_nValue << " ";
    			pCurrent = pCurrent->m_pRight;
    		}
    	}
    }


    4 后序遍历二叉树

    后序遍历二叉树的递归算法如下:

    若二叉树为空,则空操作;否则(1)后序遍历左子树;(2)后序遍历右子树;(3)访问根节点;

    /* 递归后序遍历 */
    void PostOrderTraverseTree(BinaryTreeNode *pRoot)
    {
    	if (pRoot != NULL)
    	{
    		PostOrderTraverseTree(pRoot->m_pLeft);
    		PostOrderTraverseTree(pRoot->m_pRight);
    		cout << pRoot->m_nValue << " ";
    	}
    }


    后序遍历二叉树的非递归算法如下:

    /* 非递归后序遍历 */
    void PostOrderTraverseTreeNoRecursive(BinaryTreeNode *pRoot)
    {
    	if (pRoot == NULL)
    	{
    		return;
    	}
    
    	stack<BinaryTreeNode *> traverseNodes;
    	stack<BinaryTreeNode *> nodes;
    	traverseNodes.push(pRoot);
    	while (!traverseNodes.empty())
    	{
    		BinaryTreeNode *pNode = traverseNodes.top();
    		traverseNodes.pop();
    		if (pNode->m_pLeft != NULL)
    		{
    			traverseNodes.push(pNode->m_pLeft);
    		}
    		if (pNode->m_pRight != NULL)
    		{
    			traverseNodes.push(pNode->m_pRight);
    		}
    		nodes.push(pNode);
    	}
    
    	while (!nodes.empty())
    	{
    		BinaryTreeNode *pNode = nodes.top();
    		nodes.pop();
    		cout << pNode->m_nValue << " ";
    	}
    }


    5 测试样例

    文件 BinaryTree.h

    struct BinaryTreeNode 
    {
    	int                    m_nValue; 
    	BinaryTreeNode*        m_pLeft;  
    	BinaryTreeNode*        m_pRight; 
    };
    
    BinaryTreeNode* CreateBinaryTreeNode(int value);
    void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight);
    void PrintTreeNode(BinaryTreeNode* pNode);
    void PrintTree(BinaryTreeNode* pRoot);
    void DestroyTree(BinaryTreeNode* pRoot);
    


    文件 BinaryTree.cpp

    #include "BinaryTree.h"
    #include <cstdio>
    
    BinaryTreeNode* CreateBinaryTreeNode(int value)
    {
    	BinaryTreeNode* pNode = new BinaryTreeNode();
    	pNode->m_nValue = value;
    	pNode->m_pLeft = NULL;
    	pNode->m_pRight = NULL;
    
    	return pNode;
    }
    
    void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
    {
    	if(pParent != NULL)
    	{
    		pParent->m_pLeft = pLeft;
    		pParent->m_pRight = pRight;
    	}
    }
    
    void PrintTreeNode(BinaryTreeNode* pNode)
    {
    	if(pNode != NULL)
    	{
    		printf("value of this node is: %d
    ", pNode->m_nValue);
    
    		if(pNode->m_pLeft != NULL)
    			printf("value of its left child is: %d.
    ", pNode->m_pLeft->m_nValue);
    		else
    			printf("left child is null.
    ");
    
    		if(pNode->m_pRight != NULL)
    			printf("value of its right child is: %d.
    ", pNode->m_pRight->m_nValue);
    		else
    			printf("right child is null.
    ");
    	}
    	else
    	{
    		printf("this node is null.
    ");
    	}
    
    	printf("
    ");
    }
    
    void PrintTree(BinaryTreeNode* pRoot)
    {
    	PrintTreeNode(pRoot);
    
    	if(pRoot != NULL)
    	{
    		if(pRoot->m_pLeft != NULL)
    			PrintTree(pRoot->m_pLeft);
    
    		if(pRoot->m_pRight != NULL)
    			PrintTree(pRoot->m_pRight);
    	}
    }
    
    void DestroyTree(BinaryTreeNode* pRoot)
    {
    	if(pRoot != NULL)
    	{
    		BinaryTreeNode* pLeft = pRoot->m_pLeft;
    		BinaryTreeNode* pRight = pRoot->m_pRight;
    
    		delete pRoot;
    		pRoot = NULL;
    
    		DestroyTree(pLeft);
    		DestroyTree(pRight);
    	}
    }
    


    文件 TraverseTree.cpp

    #include "BinaryTree.h"
    #include <iostream>
    #include <stack>
    using namespace std;
    
    /* 递归先序遍历 */
    void PreOrderTraverseTree(BinaryTreeNode *pRoot)
    {
    	if (pRoot != NULL)
    	{
    		cout << pRoot->m_nValue << " ";
    		PreOrderTraverseTree(pRoot->m_pLeft);
    		PreOrderTraverseTree(pRoot->m_pRight);
    	}
    }
    
    /* 非递归先序遍历 */
    void PreOrderTraverseTreeNoRecursive(BinaryTreeNode *pRoot)
    {
    	if (pRoot == NULL)
    	{
    		return;
    	}
    
    	stack<BinaryTreeNode *> nodes;
    	nodes.push(pRoot);
    	while (!nodes.empty())
    	{
    		BinaryTreeNode *pNode = nodes.top();
    		nodes.pop();
    		cout << pNode->m_nValue << " ";
    
    		if (pNode->m_pRight != NULL)
    		{
    			nodes.push(pNode->m_pRight);
    		}
    		if (pNode->m_pLeft != NULL)
    		{
    			nodes.push(pNode->m_pLeft);
    		}
    	}
    }
    
    /* 递归中序遍历 */
    void InOrderTraverseTree(BinaryTreeNode *pRoot)
    {
    	if (pRoot != NULL)
    	{
    		InOrderTraverseTree(pRoot->m_pLeft);
    		cout << pRoot->m_nValue << " ";
    		InOrderTraverseTree(pRoot->m_pRight);
    	}
    }
    
    /* 非递归中序遍历 */
    void InOrderTraverseTreeNoRecursive(BinaryTreeNode *pRoot)
    {
    	if (pRoot == NULL)
    	{
    		return;
    	}
    
    	stack<BinaryTreeNode *> nodes;
    	BinaryTreeNode *pCurrent = pRoot;
    	while (!nodes.empty() || pCurrent != NULL)
    	{
    		if (pCurrent != NULL)
    		{
    			nodes.push(pCurrent);
    			pCurrent = pCurrent->m_pLeft;
    		}
    		else
    		{
    			pCurrent = nodes.top();
    			nodes.pop();
    			cout << pCurrent->m_nValue << " ";
    			pCurrent = pCurrent->m_pRight;
    		}
    	}
    }
    
    /* 递归后序遍历 */
    void PostOrderTraverseTree(BinaryTreeNode *pRoot)
    {
    	if (pRoot != NULL)
    	{
    		PostOrderTraverseTree(pRoot->m_pLeft);
    		PostOrderTraverseTree(pRoot->m_pRight);
    		cout << pRoot->m_nValue << " ";
    	}
    }
    
    /* 非递归后序遍历 */
    void PostOrderTraverseTreeNoRecursive(BinaryTreeNode *pRoot)
    {
    	if (pRoot == NULL)
    	{
    		return;
    	}
    
    	stack<BinaryTreeNode *> traverseNodes;
    	stack<BinaryTreeNode *> nodes;
    	traverseNodes.push(pRoot);
    	while (!traverseNodes.empty())
    	{
    		BinaryTreeNode *pNode = traverseNodes.top();
    		traverseNodes.pop();
    		if (pNode->m_pLeft != NULL)
    		{
    			traverseNodes.push(pNode->m_pLeft);
    		}
    		if (pNode->m_pRight != NULL)
    		{
    			traverseNodes.push(pNode->m_pRight);
    		}
    		nodes.push(pNode);
    	}
    
    	while (!nodes.empty())
    	{
    		BinaryTreeNode *pNode = nodes.top();
    		nodes.pop();
    		cout << pNode->m_nValue << " ";
    	}
    }
    
    void Test(const char *testName, BinaryTreeNode *pRoot)
    {
    	if (testName != NULL)
    	{
    		cout << testName << " : " << endl;
    	}
    	cout << "递归先序遍历:  " ;
    	PreOrderTraverseTree(pRoot);
    	cout << endl;
    	cout << "非递归先序遍历:";
    	PreOrderTraverseTreeNoRecursive(pRoot);
    	cout << endl;
    	cout << "递归中序遍历:  ";
    	InOrderTraverseTree(pRoot);
    	cout << endl;
    	cout << "非递归中序遍历:";
    	InOrderTraverseTreeNoRecursive(pRoot);
    	cout << endl;
    	cout << "递归后序遍历:  ";
    	PostOrderTraverseTree(pRoot);
    	cout << endl;
    	cout << "非递归后序遍历:";
    	PostOrderTraverseTreeNoRecursive(pRoot);
    	cout << endl;
    }
    void Test1()
    {
    	BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
    	BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2);
    	BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3);
    	BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4);
    	BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5);
    	BinaryTreeNode *pNode6 = CreateBinaryTreeNode(6);
    
    	ConnectTreeNodes(pNode1, pNode2, pNode3);
    	ConnectTreeNodes(pNode2, pNode4, pNode5);
    	ConnectTreeNodes(pNode3, pNode6, NULL);
    
    	Test("Test1", pNode1);
    
    	DestroyTree(pNode1);
    }
    
    void Test2()
    {
    	BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
    	BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2);
    	BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3);
    	BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4);
    	BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5);
    	ConnectTreeNodes(pNode1, pNode2, NULL);
    	ConnectTreeNodes(pNode2, pNode3, NULL);
    	ConnectTreeNodes(pNode3, pNode4, NULL);
    	ConnectTreeNodes(pNode4, pNode5, NULL);
    
    	Test("Test2", pNode1);
    
    	DestroyTree(pNode1);
    }
    
    void Test3()
    {
    	BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
    	BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2);
    	BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3);
    	BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4);
    	BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5);
    	ConnectTreeNodes(pNode1, NULL, pNode2);
    	ConnectTreeNodes(pNode2, NULL, pNode3);
    	ConnectTreeNodes(pNode3, NULL, pNode4);
    	ConnectTreeNodes(pNode4, NULL, pNode5);
    
    	Test("Test3", pNode1);
    
    	DestroyTree(pNode1);
    }
    
    void Test4()
    {
    	BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
    	Test("Test4", pNode1);
    	DestroyTree(pNode1);
    }
    
    void Test5()
    {
    	Test("Test5", NULL);
    }
    
    int main()
    {
    	Test1();
    	Test2();
    	Test3();
    	Test4();
    	Test5();
    }


    测试结果:



  • 相关阅读:
    嵌入式开发之基于模型的设计思想
    tslib库的移植以及"selected device is not a touchscreen I understand"问题解决
    rocketMq broker.conf全部参数解释
    rocketMq 消息偏移量 Offset
    3、验证,数据绑定和类型转换
    2、springframe Resources
    1、Spring Framework 5.0简述
    POI导出Excel时下拉列表值超过255的问题(String literals in formulas can't be bigger than 255 characters ASCII)
    rocketMq console MQClientException异常
    25. Secure Object Implementations(安全对象实现)
  • 原文地址:https://www.cnblogs.com/riskyer/p/3275712.html
Copyright © 2020-2023  润新知