二叉查找树实现排序
来源:互联网 发布:mac html5开发工具 编辑:程序博客网 时间:2024/05/17 09:07
//---------------------------------------------------------------------------------------------------------------------------------------------------------二叉查找树头文件。
#pragma once
#include <iostream>
using namespace std;
struct TreeNode
{
TreeNode(int iData)
{
m_iData = iData;
m_pParent = NULL;
m_pLeft = NULL;
m_pRight = NULL;
}
~TreeNode()
{
if (m_pLeft)
{
delete m_pLeft;
m_pLeft = NULL;
}
if (m_pRight)
{
delete m_pRight;
m_pRight = NULL;
}
}
int m_iData;
TreeNode* m_pParent;
TreeNode* m_pLeft;
TreeNode* m_pRight;
};
void MidOrderView(TreeNode* pRoot); //中序遍历
class BinaryFindTree
{
public:
BinaryFindTree(void);
~BinaryFindTree(void);
public:
void ConstructTree(int A[], int iLength);
void InsertNode(int iValue);
void InsertNode(TreeNode* pNode);
//插入和删除使用节点关键字有点相反的意思味道。
bool DeleteNode(int iValue);
bool DeleteNode(TreeNode* pNode);
TreeNode* GetRoot();
private:
TreeNode* m_pRoot;
};
//---------------------------------------------------------------------------------------------------------------------------------------------------------二叉查找树头文件。//---------------------------------------------------------------------------------------------------------------------------------------------------------二叉查找树实现文件。
#include "BinaryFindTree.h"
static void ViewTreeNode(TreeNode* pNode)
{
cout << pNode->m_iData << " , ";
}
void MidOrderView(TreeNode* pRoot)
{
if (pRoot == NULL)
{
return;
}
MidOrderView(pRoot->m_pLeft);
ViewTreeNode(pRoot);
MidOrderView(pRoot->m_pRight);
}
BinaryFindTree::BinaryFindTree(void)
{
m_pRoot = NULL;
}
BinaryFindTree::~BinaryFindTree(void)
{
delete m_pRoot;
m_pRoot = NULL;
}
TreeNode* BinaryFindTree::GetRoot()
{
return m_pRoot;
}
void BinaryFindTree::ConstructTree(int A[], int iLength)
{
for ( int i = 0; i < iLength; i ++)
{
TreeNode* pNode = new TreeNode(A[i]);
InsertNode(pNode);
}
}
void BinaryFindTree::InsertNode(int iValue)
{
TreeNode* pNode = new TreeNode[iValue];
InsertNode(pNode);
}
void BinaryFindTree::InsertNode(TreeNode* pNode)
{
TreeNode* pY = NULL;
TreeNode* pX = m_pRoot;
while (pX != NULL)
{
pY = pX;
if (pX->m_iData > pNode->m_iData)
{
pX = pX->m_pLeft;
}
else
{
pX = pX->m_pRight;
}
}
pNode->m_pParent = pY;
if (pY == NULL)
{
m_pRoot = pNode;
}
else
{
if (pNode->m_iData <pY->m_iData)
{
pY->m_pLeft = pNode;
}
else
{
pY->m_pRight = pNode;
}
}
}
bool BinaryFindTree::DeleteNode(int iValue)
{
TreeNode* p = m_pRoot;
TreeNode* pp = NULL;
while(p && p->m_iData != iValue)
{
pp = p;
if (p->m_iData > iValue)
{
p = p->m_pLeft;
}
else
{
p = p->m_pRight;
}
}
if (!p)
{
return false;//没有查找到、故不删除,直接返回
}
TreeNode* pC = NULL;
//删除的节点有左子节点、右子节点,让左子树上最大节点来取代它
if (p->m_pLeft && p->m_pRight)
{
TreeNode* pS = p->m_pLeft; //当前节点
TreeNode* pSP = p; //当前节点的父节点。
while(pS->m_pRight)
{
pSP = pS;
pS = pS->m_pRight;
}
p->m_iData = pS->m_iData;
p = pS;
pp = pSP;
}
//只有一个子节点
else if (p->m_pLeft)
{
pC = p->m_pLeft;
}
else if (p->m_pRight)
{
pC = p->m_pRight;
}
//删除p
if ( p == m_pRoot)
{
m_pRoot = pC;
}
else
{
if (p == pp->m_pLeft)
{
pp->m_pLeft = pC;
}
else
{
pp->m_pRight = pC;
}
}
delete p;
return true;
}
bool BinaryFindTree::DeleteNode(TreeNode* pNode)
{
int iValue = pNode->m_iData;
return DeleteNode(iValue);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------二叉查找树实现文件。
//---------------------------------------------------------------------------------------------------------------------------------------------------------建立二叉查找树、中序遍历,便实现了排序。
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "BinaryFindTree.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
srand(int(time(NULL)));
int A[20];
for ( int i = 0; i < 20; i++)
{
A[i] = ((int)rand())%1000;
cout << A[i] << ",";
}
cout << " After Binary_Find_Tree Sort :"<<endl;
BinaryFindTree binaryFindTree;
binaryFindTree.ConstructTree(A, 20);
MidOrderView(binaryFindTree.GetRoot());
return 0;
}
- 二叉查找树实现排序
- Java 实现二叉树排序和查找
- 二叉排序(查找)树
- 二叉排序(查找)树
- 二叉排序/查找树
- 二叉排序查找树
- 排序二叉树查找
- 二叉树排序查找
- java 二叉查找树(搜索树、排序树)实现
- 二叉查找树(排序树)与java实现
- C++模板实现的二叉排序(查找)树
- Java实现二叉排序(查找)树的操作
- 二叉查找树实现
- 二叉查找树实现
- 二叉查找树实现
- 二叉查找树实现
- 二叉查找(排序)树
- 二叉查找(排序)树
- 在 Windows 下安装配置 PHP + Memcache
- struts 2 locale 设置
- 使用ARM标准C库进行嵌入式应用程序开发
- DHCP自动获取IP配置
- Visual Studio 2010(.NET 4.0)中使用SQLite.NET
- 二叉查找树实现排序
- 动态PAT配置
- Linq中Group和Sum的应用
- SQLite使用手记
- Objective-C中委托和协议详解
- 计数排序的C++实现
- 静态NAT配置
- #if defined() 含义
- 装系统时出现蓝屏