// 中序遍历.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;
}