释放值为key的子树

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

typedef struct _Node
{
    int data;
    struct _Node *left;
    struct _Node *right;
    
    _Node()
    {
        data = 0;
        left = NULL;
        right = NULL;
    }
}Node, *_PNode;

//创建二叉树利用先序创建
/*
                                             1
                                          /     \
                                         2       3
                                        / \      / 
                                       4   3    6
                                      / \   \  / \
                                     7   8  9  10 11
                                    /     \
                                   12      13

*/
void CreateBitree(_PNode &pNode, fstream &fin)
{
    int dat;
    fin>>dat;
    if(dat==0)
    {
        pNode = NULL;
    }
    else 
    {
        pNode = new Node();
        pNode->data=dat;      
        CreateBitree(pNode->left, fin);      
        CreateBitree(pNode->right, fin);
    }
}

//*************************************释放值为key的子树***************************************begin

void DeleteNode(_PNode pNode)
{
    if (NULL != pNode)
    {
        DeleteNode(pNode->left);
        DeleteNode(pNode->right);
        delete pNode;
    }
}

void DeleteSpecificValue(_PNode pRoot, int key)
{
    if (NULL == pRoot)
    {
        return;
    }
    if (pRoot->data == key)
    {
        DeleteNode(pRoot);
        return;
    }
    _PNode pNode = pRoot; 
    queue<_PNode> q;
    q.push(pNode);
    while (!q.empty())
    {
        pNode = q.front();
        q.pop();
        if (NULL != pNode->left)
        {
            if (pNode->left->data == key)
            {
                DeleteNode(pNode->left);
                pNode->left = NULL;
            }
            else
            {
                q.push(pNode->left);
            }
        }
        if (NULL != pNode->right)
        {
            if (pNode->right->data == key)
            {
                DeleteNode(pNode->right);
                pNode->right = NULL;
            }
            else
            {
                q.push(pNode->right);
            }
        }
    }
}

//*************************************释放值为key的子树***************************************end

//前序递归遍历
void PreRecurTraversal(_PNode pRoot)
{
    if (NULL != pRoot)
    {
        cout<<pRoot->data<<"  ";
        PreRecurTraversal(pRoot->left);
        PreRecurTraversal(pRoot->right);
    }
}

//中序递归遍历
void MidRecurTraversal(_PNode pRoot)
{
    if (NULL != pRoot)
    {
        MidRecurTraversal(pRoot->left);
        cout<<pRoot->data<<"  ";
        MidRecurTraversal(pRoot->right);
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    _PNode pRoot = NULL;

    fstream fin("tree.txt");
    CreateBitree(pRoot, fin);

    cout<<"********************前序递归遍历二叉树********************"<<endl<<endl;
    PreRecurTraversal(pRoot);
    cout<<endl<<endl<<"********************中序递归遍历二叉树********************"<<endl<<endl;
    MidRecurTraversal(pRoot);

    DeleteSpecificValue(pRoot, 3); //删除结点的值等于3的结点
    cout<<endl<<endl<<"********************前序递归遍历二叉树********************"<<endl<<endl;
    PreRecurTraversal(pRoot);
    cout<<endl<<endl<<"********************中序递归遍历二叉树********************"<<endl<<endl;
    MidRecurTraversal(pRoot);

    cout<<endl<<endl;
    return 0;
}

界面运行如下:

建造二叉树的tree.txt文件如下:

1 2 4 7 12 0 0 0 8 0 13 0 0 3 0 9 0 0 3 6 10 0 0 11 0 0 0
posted @ 2012-08-24 22:15  venow  阅读(301)  评论(0编辑  收藏  举报