C++前中后序遍历树的方法

#include<iostream>
using namespace std;

//定义二叉树节点
class binarynode
{
public:
	char ch;			 //节点数据域
	binarynode* lchild;  //左孩子
	binarynode* rchild;  //右孩子
};

//递归遍历
//先序
void recursion_dlr(binarynode* root)
{
	if (root == NULL)
	{
		return;
	}
	//遍历根节点
	cout << root->ch << "\t";
	//遍历左子树
	recursion_dlr(root->lchild);
	//遍历右子树
	recursion_dlr(root->rchild);
}

//中序
void recursion_ldr(binarynode* root)
{
	if (root == NULL)
	{
		return;
	}
	//遍历左子树
	recursion_ldr(root->lchild);
	//遍历根节点
	cout << root->ch << "\t";
	//遍历右子树
	recursion_ldr(root->rchild);
}

//后序
void recursion_lrd(binarynode* root)
{
	if (root == NULL)
	{
		return;
	}
	//遍历左子树
	recursion_lrd(root->lchild);
	//遍历右子树
	recursion_lrd(root->rchild);
	//遍历根节点
	cout << root->ch << "\t";
}

//创建二叉树
void createtree()
{
	//创建节点
	binarynode node1 = { 'A',NULL,NULL };
	binarynode node2 = { 'B',NULL,NULL };
	binarynode node3 = { 'C',NULL,NULL };
	binarynode node4 = { 'D',NULL,NULL };
	binarynode node5 = { 'E',NULL,NULL };
	binarynode node6 = { 'F',NULL,NULL };
	binarynode node7 = { 'G',NULL,NULL };
	binarynode node8 = { 'H',NULL,NULL };
	//建立节点关系
	node1.lchild = &node2;
	node1.rchild = &node6;
	node2.rchild = &node3;
	node3.lchild = &node4;
	node3.rchild = &node5;
	node6.rchild = &node7;
	node7.lchild = &node8;
	cout << "先序遍历:" << endl;
	recursion_dlr(&node1);
	cout << endl;
	cout << "中序遍历:" << endl;
	recursion_ldr(&node1);
	cout << endl;
	cout << "后序遍历:" << endl;
	recursion_lrd(&node1);
	cout << endl;
}

int main()
{
	createtree();
	system("pause");
	return 0;
}
#include <iostream>
using namespace std;

typedef struct STreeNode* pSTreeNode;
typedef int TreeDataType;

struct STreeNode
{
    TreeDataType data;
    pSTreeNode pFirstChild;
    pSTreeNode pNextBrother;

    STreeNode(TreeDataType Value)
    {
        data = Value;
        pFirstChild = NULL;
        pNextBrother = NULL;
    }
};

class CTree
{
public:
    CTree();
    CTree(TreeDataType Value);
    ~CTree();

public:
    void Insert(TreeDataType parentValue, TreeDataType Value);    //  parentValue:该节点的父结点;Value:该节点的值
    void InsertBrother(pSTreeNode pParentNode, TreeDataType Value);

    pSTreeNode Search(pSTreeNode pNode, TreeDataType Value);

    void Preorder(pSTreeNode pNode);      //  前序遍历
    void Inorder(pSTreeNode pNode);       //  中序遍历
    void postorder(pSTreeNode pNode);     //  后续遍历

    void PrintNode(pSTreeNode pNode);
    void FreeMemory(pSTreeNode pNode);    //  释放内存

public:
    pSTreeNode pRoot;
};

CTree::CTree()
{
    pRoot = NULL;
}

CTree::CTree(TreeDataType Value)
{
    pRoot = new STreeNode(Value);
    if (pRoot == NULL)
        return;
}

CTree::~CTree()
{
    if (pRoot == NULL)
        return;

    FreeMemory(pRoot);
}

void CTree::FreeMemory(pSTreeNode pNode)
{
    if (pNode == NULL)
        return;

    if (pNode->pFirstChild != NULL)
        FreeMemory(pNode->pFirstChild);

    if (pNode->pNextBrother != NULL)
        FreeMemory(pNode->pNextBrother);

    delete pNode;
    pNode = NULL;
}

void CTree::Insert(TreeDataType parentValue, TreeDataType Value)
{
    if (pRoot == NULL)
        return;

    pSTreeNode pFindNode = Search(pRoot, parentValue);
    if (pFindNode == NULL)
        return;

    if (pFindNode->pFirstChild == NULL)
    {
        pFindNode->pFirstChild = new STreeNode(Value);
        return;
    }
    else
    {
        InsertBrother(pFindNode->pFirstChild, Value);
        return;
    }
}

void CTree::InsertBrother(pSTreeNode pBrotherNode, TreeDataType Value)
{
    if (pBrotherNode->pNextBrother != NULL)
        InsertBrother(pBrotherNode->pNextBrother, Value);
    else
    {
        pBrotherNode->pNextBrother = new STreeNode(Value);
        return;
    }
}

pSTreeNode CTree::Search(pSTreeNode pNode, TreeDataType Value)
{
    if (pNode == NULL)
        return NULL;

    if (pNode->data == Value)
        return pNode;

    if (pNode->pFirstChild == NULL && pNode->pNextBrother == NULL)
        return NULL;
    else
    {
        if (pNode->pFirstChild != NULL)
        {
            pSTreeNode pNodeTemp = Search(pNode->pFirstChild, Value);
            if (pNodeTemp != NULL)
                return pNodeTemp;
            else
            {
                return Search(pNode->pNextBrother, Value);
            }
        }
        else
            return Search(pNode->pNextBrother, Value);
    }
}

void CTree::Preorder(pSTreeNode pNode)
{
    if (pNode == NULL)
        return;
    cout << " " << pNode->data << " ";

    Preorder(pNode->pFirstChild);
    Preorder(pNode->pNextBrother);
}

void CTree::Inorder(pSTreeNode pNode)
{
    if (pNode == NULL)
        return;

    Inorder(pNode->pFirstChild);
    cout << " " << pNode->data << " ";
    Inorder(pNode->pNextBrother);
}

void CTree::postorder(pSTreeNode pNode)
{
    if (pNode == NULL)
        return;

    postorder(pNode->pFirstChild);
    postorder(pNode->pNextBrother);
    cout << " " << pNode->data << " ";
}

int main()
{
    CTree* pTree = new CTree(1);
    if (pTree == NULL)
        return 0;

    pTree->Insert(1, 2);
    pTree->Insert(1, 3);
    pTree->Insert(1, 4);
    pTree->Insert(1, 5);
    pTree->Insert(1, 6);
    pTree->Insert(1, 7);
    pTree->Insert(4, 8);
    pTree->Insert(5, 9);
    pTree->Insert(5, 10);
    pTree->Insert(6, 11);
    pTree->Insert(6, 12);
    pTree->Insert(6, 13);
    pTree->Insert(10, 14);
    pTree->Insert(10, 15);

    cout << "前序遍历:" << endl;
    pTree->Preorder(pTree->pRoot);
    cout << endl;

    cout << "中序遍历:" << endl;
    pTree->Inorder(pTree->pRoot);
    cout << endl;

    cout << "后序遍历:" << endl;
    pTree->postorder(pTree->pRoot);
    cout << endl;

    delete pTree;
    pTree = NULL;
    system("pause");
    return 1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值