码迷,mamicode.com
首页 > 编程语言 > 详细

二叉树排序树的的构造和查找

时间:2015-11-28 13:32:06      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

/*********************************************************

          二叉树排序树的的构造和查找

*********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <stack>
using namespace std;

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW   -2

#define EQ(a,b) ((a) == (b))
#define LT(a,b) ((a) <  (b))
#define LQ(a,b) ((a) <= (b))

#define InitDSTable Init
#define DestroyDSTable Destroy
#define TraverseDSTable InOrderTraverse

typedef int Status;
//typedef char TElemType[12];
typedef int KeyType;

struct ElemType //数据元素类型
{
    KeyType key;
    int others;
};

typedef struct BiTNode
{
    ElemType data;
    BiTNode* lchild, *rchild;
} BiTNode, *BitTree;

void print(ElemType c)
{
    printf("(%d,%d) ", c.key, c.others);
}

void Init(BitTree& T)
{
    T = NULL;
}

void Destroy(BitTree& T)
{
    if(T)
    {
        if(T->lchild)
        {
            Destroy(T->lchild);
        }
        if(T->rchild)
        {
            Destroy(T->rchild);
        }
        free(T);
        T = NULL;
    }
}

Status Empty(BitTree T)
{
    if(T) {
        return FALSE;
    }
    else {
        return TRUE;
    }
}

//中序遍历
void InOrderTraverse(BitTree T)
{
    if(T)
    {
        InOrderTraverse(T->lchild);
        printf("%d ", T->data.key);
        InOrderTraverse(T->rchild);
    }
}

Status SearchBST(BitTree& T, KeyType key, BitTree f, BitTree& p)
{
    //在根指针T所指排序二叉树中递归地查找某关键字等于key的数据元素,若查找成功,则指针p只想该数据元素节点,并返回TRUE;否则指针p指向查找路径上访问的最后一个节点并返回FALSE,指针f指向T的双亲,其初始调用值为NULL
    if(!T)
    {
        //查找不成功
        p = f;
        return FALSE;
    }
    else if EQ(key, T->data.key)
    {
        p = T;
        return TRUE;
    }
    else if LT(key, T->data.key)
    {
        return SearchBST(T->lchild, key, T, p);
    }
    else
    {
        return SearchBST(T->rchild, key, T, p);
    }
}

Status InsertBST(BitTree& T, ElemType e)
{
    //当排序二叉树T中不存在关键字等于e.key的元素时,插入e并返回TRUE,否则返回FALSE。
    BitTree p, s;
    if(!SearchBST(T, e.key, NULL, p))
    {
        s = (BitTree)malloc(sizeof(BiTNode));
        s->data = e;
        s->lchild = s->rchild = NULL;
        if(!p)
        {
            T = s; //被插节点s为新的根节点
        }
        else if LT(e.key, p->data.key)
        {
            p->lchild = s;
        }
        else
        {
            p->rchild = s;
        }
        return TRUE;
    }
    //树种已经有关键字相同的节点,不再插入
    else
    {
        return FALSE;
    }
}

void Delete(BitTree& p)
{
    //从排序二叉树中删除节点p,并重接它的左或右子树
    BitTree q, s;
    if(!p->rchild)
    {
        //p的右子树空,则只需要重接它的左字树
        q = p;
        p = p->lchild;
        free(q);
    }
    else if(!p->lchild)
    {
        //p的左子树为空,只需要重接它的右子树
        q = p;
        p = p->rchild;
        free(q);
    }
    else
    {
        //左右子树均不为空
        q = p;
        s = p->lchild;
        while(s->rchild)
        {
            //转左,然后向右到尽头(找待删除节点的前驱)
            q = s;
            s = s->rchild;
        }
        p->data = s->data; //s指向被删除节点的“前驱”(将被删除节点的前驱的值取代被删除节点的值)
        if(q != p)
        {
            q->rchild = s->lchild; //重接q的右子树
        }
        else
        {
            q->lchild = s->lchild; //重接q的左子树
        }
        free(s);
    }
}

Status DeleteBST(BitTree& T, KeyType key)
{
    //若排序二叉树T中存在关键字等于key的数据元素时,则删除该数据元素节点,并返回TRUE,否则返回FALSE
    if(!T)
    {
        return FALSE;
    }
    else
    {
        if EQ(key, T->data.key)
        {
            Delete(T);
        }
        else if LT(key, T->data.key)
        {
            DeleteBST(T->lchild, key);
        }
        else
        {
            DeleteBST(T->rchild, key);
        }
        return TRUE;
    }
}
int main()
{
    BitTree dt, p = NULL;
    int i, j;
    int nn;
    int n = 10;
    ElemType r[10] = {{4, 8}, {16, 22}, {7, 3}, {78, 21}, {37, 5}, {24, 6}, {100, 71}, {61, 12}, {90, 9}, {79, 11}};
    InitDSTable(dt);
    for(i = 0; i < n; i++)
    {
        InsertBST(dt, r[i]);
    }
    cout << "the Binary Tree is ";
    TraverseDSTable(dt);
    cout << endl;
    cin >> nn;
    cout << "delect node:" << nn << "\n";
    DeleteBST(dt, 24);
    cout << "the Binary Tree is ";
    TraverseDSTable(dt);
    return 0;
}

 

二叉树排序树的的构造和查找

标签:

原文地址:http://www.cnblogs.com/chenyang920/p/5002487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!