二叉查找树实现排序

来源:互联网 发布: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;
}


原创粉丝点击