二叉树的前序、中序、后序遍历实现

// 中序遍历.cpp : 定义控制台应用程序的入口点。
//

include “stdafx.h”

include < iostream>

include < stdlib.h>

include < stdio.h>

include < assert.h>

include < vector>

using namespace std;

/*
为了使次二叉树符合任意的随机数列,即随机数列中有“相同”的元素,在结构体中保存一个链表专门存储相同的值得节点pEqualValue!!!
*/

struct TreeNode
{
int value;
TreeNode* pLeft;
TreeNode* pRight;
TreeNode* pParent;

TreeNode* pEqualValue; //保存值相等的节点(使用顺序存储),全部存于左孩子上

TreeNode(int nValue)
{
    value = nValue;
    pLeft = NULL;
    pRight = NULL;
    pParent = NULL;
    pEqualValue = NULL;
}

void AddEV(TreeNode* node)
{
    if(node)
    {
        if(pEqualValue == NULL)
        {
            pEqualValue = node;
        }
        else
        {
            TreeNode* pTempNode = pEqualValue;
            while(pTempNode->pLeft)
            {
                pTempNode = pTempNode->pLeft;
            }

            pTempNode->pLeft = node;
            node->pParent = pTempNode;
        }

    }
}

~TreeNode()
{
    cout<<endl<<"释放内存: "<<value<<" ";
}

};

static TreeNode* m_rootTreeNode = NULL;

void AddTreeNode(int value)
{
if(m_rootTreeNode == NULL)
{
TreeNode* node = new TreeNode(value);
m_rootTreeNode = node;
return;
}

TreeNode* pParent = m_rootTreeNode;
TreeNode* newNode = new TreeNode(value);
while(pParent)
{
    if(pParent->value > value)
    {
        //放在左海子处
        if(pParent->pLeft == NULL)
        {
            pParent->pLeft = newNode;
            newNode->pParent = pParent;
            break;
        }
        else
        {
            pParent = pParent->pLeft;
        }
    }
    else if(pParent->value < value)
    {
        //放于右孩子处
        if(pParent->pRight == NULL)
        {
            pParent->pRight = newNode;
            newNode->pParent = pParent;
            break;
        }
        else
        {
            pParent = pParent->pRight;
        }
    }
    else
    {
        //值相等(其实可以直接放在右孩子节点的)
        pParent->AddEV(newNode);
        break;
    }
}

}

//创建BST树
void CreateBST(int* array, int size)
{
if((array == NULL) || (size <= 0))
{
assert((array == NULL) || (size <= 0));
return;
}

for(int i=0; i<size; i++)
{
    AddTreeNode(array[i]);
}

}

//中序遍历
void MiddleOrder(TreeNode* rootNode)
{
if(rootNode != NULL)
{
MiddleOrder(rootNode->pLeft);
cout<value<<” “;
/本结构特有: 显示相同值得节点/
if(rootNode->pEqualValue)
{
TreeNode* pTempEV = rootNode->pEqualValue;
while(pTempEV)
{
cout<value<<” “;
pTempEV = pTempEV->pLeft;
}
}
/本结构特有: 显示相同值得节点/
MiddleOrder(rootNode->pRight);
}
}

//前序遍历
void PriorOrder(TreeNode* rootNode)
{
if(rootNode != NULL)
{
cout<value<<” “;
/本结构特有: 显示相同值得节点/
if(rootNode->pEqualValue)
{
TreeNode* pTempEV = rootNode->pEqualValue;
while(pTempEV)
{
cout<value<<” “;
pTempEV = pTempEV->pLeft;
}
}
/本结构特有: 显示相同值得节点/
PriorOrder(rootNode->pLeft);
PriorOrder(rootNode->pRight);
}
}

//后序遍历 Postorder Traversal (LRD)
void LRD(TreeNode* rootNode)
{
if(rootNode != NULL)
{
LRD(rootNode->pLeft);
LRD(rootNode->pRight);
cout<value<<” “;
/本结构特有: 显示相同值得节点/
if(rootNode->pEqualValue)
{
TreeNode* pTempEV = rootNode->pEqualValue;
while(pTempEV)
{
cout<value<<” “;
pTempEV = pTempEV->pLeft;
}
}
/本结构特有: 显示相同值得节点/
}
}

//释放相同值得节点的内存
void DeleteEVMemory(TreeNode* pEVNode)
{
if(pEVNode)
{
DeleteEVMemory(pEVNode->pLeft);
delete pEVNode;
pEVNode = NULL;
}
}

//释放内存
void DeleteTreeMemory(TreeNode* rootNode)
{
if(rootNode)
{
DeleteTreeMemory(rootNode->pLeft);
DeleteTreeMemory(rootNode->pRight);
/本结构特有: 释放相同值得节点/
DeleteEVMemory(rootNode->pEqualValue);
/本结构特有: 释放相同值得节点/
delete rootNode;
rootNode = NULL;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
int array[13] = {0};
int size = sizeof(array)/sizeof(array[0]);
cout << “随机数列为: ” << endl;
for(int i=0; i < size; i++)
{
array[i] = rand()%20;
cout << array[i] << ” “;
}

CreateBST(array, size);

cout<< endl << "中序遍历: " << endl;
MiddleOrder(m_rootTreeNode);

cout<<endl<<"前序遍历: "<<endl;
PriorOrder(m_rootTreeNode);

cout<<endl<<"后序遍历: "<<endl;
LRD(m_rootTreeNode);

//释放内存
cout<<endl<<endl<<"利用后序遍历释放二叉树的内存!!!!!!";
DeleteTreeMemory(m_rootTreeNode);

cout<<endl;
system("pause");

return 0;

}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值