红黑平衡二叉树SDK编程参考手册

红黑树SDK和源码下载地址:   https://download.csdn.net/download/freeland008/12600993

1       概述.... 3

2       红黑平衡二叉树介绍.... 3

3       红黑树SDK技术特点.... 3

4       红黑树应用场景.... 3

5       定义说明.... 4

6       示例代码说明.... 5

7       红黑树SDK功能函数.... 5

7.1        RbtCreateTree () 5

7.2       RbtDeleteTree(). 7

7.3        RbtAddKey() 9

7.4        RbtRemoveKey() 11

7.5        RbtGetCount() 14

7.6        RbtRemoveAll() 15

7.7        RbtGetKeyPosition() 17

7.8        RbtGetKeyValue() 19

7.9        RbtGetHeadPosition() 21

7.10      RbtGetNextPosition() 23

7.11      RbtGetPrevPosition() 24

7.12      RbtGetTailPosition() 24

7.13      RbtRemoveHead() 25

7.14      RbtRemoveTail() 27

7.15      RbtGetHead() 29

7.16      RbtGetTail() 31

7.17      RbtGetNext() 32

7.18      RbtGetPrev() 35

7.19      RbtGetFirstPosGEKey() 37

7.20      RbtGetFirstPosGTKey() 38

7.21      RbtGetFirstPosSEKey() 40

7.22      RbtGetFirstPosSTKey() 41

7.23      RbtGetPosColor() 43

7.24      RbtGetPosKey() 43

7.25      RbtGetPosValue() 44

7.26      RbtSetPosValue() 44

7.27      RbtRemoveAt() 45

 

 

 

 

 

 

  1.  概述

本文档适用于C/c++语言开发人员,文档详细讲述红黑平衡二叉树SDK中每个函数的用法以及源码示例。

 

  1.  红黑平衡二叉树介绍

参考相关文档。

 

  1.  红黑树SDK技术特点

支持以下功能:

  1. 支持自定义键值比较函数
  2. 支持删除节点回调函数
  3. 支持插入节点
  4. 支持根据键值进行精确查询节点
  5. 支持根据键值进行精确删除节点
  6. 支持从头到尾(从尾到头)的方式遍历红黑树节点

7、支持从任意指定节点开始向后(或向前)遍历红黑树节点

8、支持范围查询(>、>=、<=、< )

9、支持删除头(尾)节点

10、支持获取头(尾)节点

11、支持获取节点数量

12、支持清空所有节点数量

 

  1.  红黑树应用场景
  2. 数据查询
  3. 数据排序
  4. 其它情况

 

  1.  定义说明

1、HANDLE

   指向一个红黑树的句柄(指针)或树节点的值,定义如下:

   typedef void* HANDLE;

 

2、POSITION

   POSITION定义为一个指向树节点的指针,定义如下:

    typedef void* POSITION;

 

3、ONRBTCOMPARE

    typedef int (* ONRBTCOMPARE)(void* pNodeKey, void* pOutKey);

    参数说明:

  1. void* pNodeKey

输入参数,为红黑树节点的键值指针

 

  2、void* pOutKey

   输入参数,为外部提供的键值

 

返回值:

> 0:说明此红黑树节点大于外部键值

= 0:说明此红黑树节点等于外部键值

< 0:说明此红黑树节点小于外部键值

 

4、Rbt节点内部定义如下:

    typedef struct _RBTNODE

{

        _RBTNODE * pParent; // 父节点

        _RBTNODE * pLeftChild;  // 左节点

        _RBTNODE * pRightChild;  // 右节点

        void* pKey;       // 节点键值  

        HANDLE hValue;    // 用户数据

        unsigned char nColor;  // 颜色

}RBTNODE;

typedef RBTNODE* LPRBTNODE;

 

6、ONRBTDELETE

ONRBTDELETE为回调函数指针,在执行RbtDeleteTree和RbtRemoveAll

函数释放树节点时,对每个被删除的节点都执行此回调函数,在回调函数中

处理与节点相关的操作。

 

定义如下:

typedef bool (* ONRBTDELETE)(POSITION pos, void* pInputPara);

参数:

1)POSITION pos

       被删除红黑树节点句柄(指针)

    2)void* pInputPara

输入参数指针,由RbtDeleteTree和RbtRemoveAll函数的参数中提供。

 

  1. 头节点

红黑树中键值最小的节点

 

  1. 尾节点

红黑树中键值最大的节点

 

  1. 下一节点

红黑树中键值比当前节点大的第一个节点

 

10、上一节点

红黑树中键值比当前节点小的第一个节点

 

  1.  示例代码说明
  1. 本文档的示例代码可以在windows的VC上直接编译运行
  2. 如果是采用linux的SDK开发包,需要对代码进行适当的调整才可以编译通过。

 

  1.  红黑树SDK功能函数
  2.  RbtCreateTree ()

 

1、功能说明

此函数用来生成一个红黑树,执行成功后返回红黑树句柄,之后对

此红黑树的操作都引用此句柄。

 

2、函数原型

HANDLE RbtCreateTree(ONRBTCOMPARE OnCompare);

 

3、参数说明

  1. ONRBTCOMPARE OnCompare

比较红黑树中节点和外部输入键值的函数指针,请参考上一章关于此函数指针的定义

 

4、返回值

返回红黑树的句柄

 

5、相关函数

   RbtDeleteTree()

 

示例1:

功能:生成一个红黑树,比较函数是对int数据类型进行比较。

 

#include “RbtAPI.h”

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    if (int(pNodeKey) > int(pOutKey))

    {

        return 1;

    }

    else if (int(pNodeKey) < int(pOutKey))

    {

        return -1;

    }

    else

    {

        return 0;

    }

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

    return 0;

}

 

示例2:

功能:生成一个红黑树,比较函数是对字符串进行比较。

 

#include “RbtAPI.h”

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

    return 0;

}

 

  1.  RbtDeleteTree()

1、功能说明

此函数用来删除红黑树以及树中的所有节点,释放红黑树和节点占用的所有内存空间,删除后的红黑树句柄不能再被引用。

 

2、函数原型

void RbtDeleteTree(HANDLE hTree, ONRBTDELETE OnDelete = NULL,

void* pInputPara = NULL);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

   2)ONRBTDELETE OnDelete = NULL

参考上一章关于此函数指针的说明,如果此参数设置为NULL,则在删除Rbt

树时不调用此回调函数。

 

3)void* pInputPara;

由开发人员设置自己的数据,一般为结构体或对象的指针,由回调函数

使用。

 

4、返回值

    无

 

5、相关函数

   RbtCreateTree()

 

示例1:

 

功能:删除红黑树,不调用回调函数

 

#include <stdlib.h>

#include “RbtAPI.h”

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    if (int(pNodeKey) > int(pOutKey))

    {

        return 1;

    }

    else if (int(pNodeKey) < int(pOutKey))

    {

        return -1;

    }

    else

    {

        return 0;

    }

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

int i;

for(i = 0; i < 10; i++)

{

    POSITION pos;

RbtAddKey(hTree, (void *)i, (void *)i, pos);

}

 

RbtDeleteTree(hTree);

 

    return 0;

}

 

示例2:

 

功能:删除红黑树,在删除时调用回调函数打印节点键值和用户数据,同时删除主键占用的内存。

 

#include <stdlib.h>

#include “RbtAPI.h”

 

bool OnDelete(POSITION pos, void* pInputPara)

{

    char* sKey;

sKey = (char*)(RbtGetPosKey(pos));// 获取节点键值

 

int nValue;

nValue = int(RbtGetPosValue(pos));// 获取节点用户数据

 

    printf(“Key: %s, Data: %d\r\n”, sKey, nValue);// 打印节点键值和用户数据

 

    delete[] sKey;  // 删除键值占用的空间

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

char* sKey;

 

int i;

for(i = 0; i < 10; i++)

{

sKey = new char[11];

itoa(i, sKey, 10);

POSITION pos;

RbtAddKey(hTree, sKey, (void*)i, pos);

}

 

RbtDeleteTree(hTree, OnDelete, “RbtDeleteTree”);

 

    return 0;

}

 

  1.  RbtAddKey()

1、功能说明

此函数用来在红黑树中增加节点。

 

2、函数原型

bool RbtAddKey(HANDLE hTree, void* pKey, HANDLE hValue,

POSITION& pos);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄,此句柄由RbtCreateTree函数生成。

 

2)void* pKey

pKey为键值,pKey可以本身是键值,也可以是指向某地址的指针,在

RbtAddKey函数执行时直接把pKey值赋给Rbt节点中的pKey成员变量。

 

3)HANDLE hValue

用户数据句柄(指针)在,RbtAddKey函数执行时直接把hValue赋给Rbt

节点中的hValue成员变量。

 

4)POSITION& pos;

输出参数,返回新增节点的句柄,如果新增节点失败,pos值为NULL。

 

4、返回值

    1)true

添加节点成功。

   2)false

       添加节点失败,树中存在相同主键的节点

 

5、相关函数  

RbtRemoveKey()

 

示例1:

功能:在红黑树中新插入一个节点。

 

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCardID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, (void*)pPersonInfo, pos);

}

 

RbtDeleteTree(hTree, OnDelete, “RbtAddKey”);

 

    return 0;

}

  1.  RbtRemoveKey()

1、功能说明

此函数用来在红黑树中根据键值删除指定的节点。

 

2、函数原型

bool RbtRemoveKey(HANDLE hTree, void* pKey, HANDLE& hValue);

 

3、参数说明

   1)HANDLE hTree

      输入参数

      红黑树句柄,此句柄由RbtCreateTree函数生成。

 

2)void* pKey

 输入参数,删除节点的键值

 

3)HANDLE& hValue

 输出参数

 返回要删除节点的用户数据句柄(指针)

 

4、返回值

    1)true

 从红黑树中成功删除一个节点。

   2)false

      在Rbt中不存在指定键值的节点

 

5、相关函数

   RbtAddKey()

   RbtGetCount()

   RbtRemoveAll()

 

示例1:

功能:从红黑树中删除指定节点。

 

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCardID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, (void*)pPersonInfo, pos);

}

 

for(i = 0; i < 10; i++)

{

    char sCardID[32];

sprintf(sCardID, “12345678901234567%d”, i);

HANDLE hValue;

if(RbtRemoveKey(hTree, sCardID, hValue) == true)

{

    LPPERSONINFO pPersonInfo;

    pPersonInfo = LPPERSONINFO(hValue);

    printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

        pPersonInfo->nAge);

}

}

 

RbtDeleteTree(hTree);

 

    return 0;

}

 

  1.  RbtGetCount()

1、功能说明

此函数用来获取红黑树中节点的数量。

 

2、函数原型

int RbtGetCount(HANDLE hTree);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

4、返回值

    返回消息的数量

  

5、相关函数  

RbtAddKey()

RbtRemoveKey()

 

示例1:

功能:获取红黑树中的节点总数。

 

#include “RbtAPI.h”

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    if (int(pNodeKey) > int(pOutKey))

    {

        return 1;

    }

    else if (int(pNodeKey) < int(pOutKey))

    {

        return -1;

    }

    else

    {

        return 0;

    }

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

int i;

for(i = 0; i < 10; i++)

{

RbtAddKey(hTree, (void *)i, (void *)i);

}

 

int nCount;

nCount = RbtGetCount(hTree);

 

for(i = 0; i < 5; i++)

{

    POSITION pos;

RbtRemoveKey(hTree, (void *)i, pos);

}

 

nCount = RbtGetCount(hTree);

 

RbtRemoveAll(hTree);

 

nCount = RbtGetCount(hTree);

 

    return 0;

}

      

  1.  RbtRemoveAll()

1、功能说明

此函数用来清除红黑树中所有的节点。

 

2、函数原型

void RbtRemoveAll(HANDLE hTree, ONMULTIDELETE OnDelete = NULL,

 HANDLE hPara = NULL);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

4、返回值

    返回消息的数量

  

5、相关函数  

RbtAddKey()

RbtRemoveKey()

RbtRemoveAll()

 

示例1:

功能:清除红黑树中所有节点。

#include “RbtAPI.h”

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    if (int(pNodeKey) > int(pOutKey))

    {

        return 1;

    }

    else if (int(pNodeKey) < int(pOutKey))

    {

        return -1;

    }

    else

    {

        return 0;

    }

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

int i;

for(i = 0; i < 10; i++)

{

    POSITION pos;

RbtAddKey(hTree, (void *)i, (void *)i, pos);

}

 

nCount = RbtGetCount(hTree);

 

RbtRemoveAll(hTree);

 

nCount = RbtGetCount(hTree);

 

    return 0;

}

  1.  RbtGetKeyPosition()

1、功能说明

此函数用来根据键值在红黑树中查询节点,返回节点的句柄。

 

2、函数原型

POSITION RbtGetKeyPosition(HANDLE hTree, void* pKey);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

2) 查询节点用的键值

 

4、返回值

    返回节点的句柄,如果返回值为NULL,则没有查询到节点。

  

5、相关函数  

RbtAddKey()

RbtRemoveKey()

RbtRemoveAll()

RbtGetKeyValue()

 

示例1:

功能:根据键值在红黑树中获取节点句柄

 

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCarID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

    char* sKey;

sKey = (char*)(RbtGetPosKey(pos));

 

    printf(“Key: %s\r\n”, sKey)

 

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

RbtAddKey(hTree, pPersonInfo->sCardID, (void*)pPersonInfo);

}

 

POSITION pos;

pos = RbtGetKeyPosition(hTree, “123456789012345678”);

if(pos != NULL)

{

    HANDLE hValue;

    hValue = RbtGetPosValue(pos);

 

        LPPERSONINFO pPersonInfo;

pPersonInfo = LPPERSONINFO(hValue);

printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

            pPersonInfo->nAge);

}

 

RbtDeleteTree(hTree, OnDelete, “RbtGetKeyPosition”);

 

    return 0;

}

 

  1.  RbtGetKeyValue()

1、功能说明

此函数用来根据键值在红黑树中查询节点,返回节点的用户数据。

 

2、函数原型

bool RbtGetKeyValue(HANDLE hTree, void* pKey, HANLDE& hValue);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

2) void* pKey

  查询节点用的键值

 

3) HANDLE& hValue

  输出参数,返回节点用户数据

 

4、返回值

    true:查询到该键值的节点

    false:没有此键值的节点

  

5、相关函数  

RbtAddKey()

RbtRemoveKey()

RbtRemoveAll()

RbtGetKeyPosition()

 

示例1:

功能:根据键值在红黑树中获取节点用户数据

 

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCarID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, pPersonInfo, pos);

}

 

HANDLE hValue;

if(RbtGetKeyPosition(hTree, “123456789012345678”, hValue) == true)

{

        LPPERSONINFO pPersonInfo;

pPersonInfo = LPPERSONINFO(hValue);

printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

            pPersonInfo->nAge);

}

 

RbtDeleteTree(hTree, OnDelete, “RbtGetKeyValue”);

 

    return 0;

}

 

  1.  RbtGetHeadPosition()

1、功能说明

此函数用来返回红黑树中的头节点即键值最小的节点。

 

2、函数原型

POSITION RbtGetHeadPosition(HANDLE hTree);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

4、返回值

    返回节点句柄,如果红黑树中没有节点返回值为NULL,否则返回非空值。

  

5、相关函数  

RbtGetNextPosition()

RbtGetPrevPosition()

RbtGetTailPosition()

 

示例1:

功能:采用升序、降序遍历红黑树中的节点

 

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCarID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, pPersonInfo, pos);

}

 

HANDLE hValue;

POSITION pos;

// 升序遍历

Pos = RbtGetHeadPosition(hTree);

While(pos != NULL)

{

    hValue = RbtGetPosValue(pos);

 

        LPPERSONINFO pPersonInfo;

pPersonInfo = LPPERSONINFO(hValue);

printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

            pPersonInfo->nAge);

 

    pos = RbtGetNextPosition(pos)

}

// 降序遍历

Pos = RbtGetTailPosition(hTree);

While(pos != NULL)

{

    hValue = RbtGetPosValue(pos);

 

        LPPERSONINFO pPersonInfo;

pPersonInfo = LPPERSONINFO(hValue);

printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

            pPersonInfo->nAge);

 

    pos = RbtGetPrevPosition(pos)

}

 

RbtDeleteTree(hTree, OnDelete, “RbtGetKeyValue”);

 

    return 0;

}

 

  1.  RbtGetNextPosition()

1、功能说明

此函数用来返回红黑树中指定节点的下一个节点,下一个节点的键值大于当前节点的键值,如果当前节点已经是最后一个节点,则返回NULL。

 

2、函数原型

POSITION RbtGetNextPosition(POSITION pos);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

    2)POSITION pos

       指定节点

 

4、返回值

    返回节点句柄,如果红黑树中没有节点返回值为NULL,否则返回非空值。

  

5、相关函数  

RbtGetHeadPosition()

RbtGetPrevPosition()

RbtGetTailPosition()

 

示例:参考RbtGetHeadPosition

 

  1.  RbtGetPrevPosition()

1、功能说明

此函数用来返回红黑树中指定节点的上一个节点,上一个节点的键值小于当前节点的键值,如果当前节点已经是头节点,则返回NULL。

 

2、函数原型

POSITION RbtGetPrevPosition(POSITION pos);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

    2)POSITION pos

       指定节点

 

4、返回值

    返回节点句柄,如果红黑树中没有节点返回值为NULL,否则返回非空值。

  

5、相关函数  

RbtGetHeadPosition()

RbtGetNextPosition()

RbtGetTailPosition()

 

示例:参考RbtGetHeadPosition

 

  1.  RbtGetTailPosition()

1、功能说明

此函数用来返回红黑树中的尾节点即键值最大的节点。

 

2、函数原型

POSITION RbtGetTailPosition(HANDLE hTree);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

4、返回值

    返回节点句柄,如果红黑树中没有节点返回值为NULL,否则返回非空值。

  

5、相关函数  

RbtGetHeadPosition()

RbtGetNextPosition()

RbtGetPrevPosition()

 

  1.  RbtRemoveHead()

1、功能说明

此函数删除红黑树中的头节点即键值最小的节点,同时返回被删除节点的句柄值。

 

2、函数原型

bool RbtRemoveHead(HANDLE hTree, HANDLE& hValue);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

2) HANDLE& hValue

  输出参数,返回被删除头节点的句柄值。

 

4、返回值

    true: 成功删除头结点

    false: 红黑树中无节点

  

5、相关函数  

RbtRemoveTail()

 

示例1:

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCardID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, (void*)pPersonInfo, pos);

}

 

HANDLE hValue;

RbtRemoveHead(hTree, hValue);

 

    LPPERSONINFO pPersonInfo;

pPersonInfo = LPPERSONINFO(hValue);

printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

            pPersonInfo->nAge);

 

RbtDeleteTree(hTree, OnDelete, “RbtGetKeyValue”);

 

    return 0;

}

 

  1.  RbtRemoveTail()

1、功能说明

此函数删除红黑树中的尾节点即键值最大的节点,同时返回被删除节点的句柄值。

 

2、函数原型

bool RbtRemoveTail(HANDLE hTree, HANDLE& hValue);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

2) HANDLE& hValue

  输出参数,返回被删除尾节点的句柄值

 

4、返回值

    true: 成功删除尾结点

    false: 红黑树中无节点

  

5、相关函数  

RbtRemoveHead()

 

示例1:

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCardID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, (void*)pPersonInfo, pos);

}

 

HANDLE hValue;

RbtRemoveTail(hTree, hValue);

 

    LPPERSONINFO pPersonInfo;

pPersonInfo = LPPERSONINFO(hValue);

printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

            pPersonInfo->nAge);

 

RbtDeleteTree(hTree, OnDelete, “RbtGetKeyValue”);

 

    return 0;

}

  1.  RbtGetHead()

1、功能说明

此函数返回红黑树中的头节点即键值最小的节点的句柄值。

 

2、函数原型

bool RbtGetHead(HANDLE hTree, HANDLE& hValue);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

2) HANDLE& hValue

  输出参数,返回头节点的句柄值。

 

4、返回值

    true: 执行成功

    false: 红黑树中无节点

  

5、相关函数  

RbtGetTail()

 

示例1:

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCardID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, (void*)pPersonInfo, pos);

}

 

HANDLE hValue;

RbtGetHead(hTree, hValue);

 

    LPPERSONINFO pPersonInfo;

pPersonInfo = LPPERSONINFO(hValue);

printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

            pPersonInfo->nAge);

 

RbtDeleteTree(hTree, OnDelete, “RbtGetKeyValue”);

 

    return 0;

}

  1.  RbtGetTail()

1、功能说明

此函数返回红黑树中的尾节点即键值最大的节点的句柄值。

 

2、函数原型

bool RbtGetTail(HANDLE hTree, HANDLE& hValue);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

2) HANDLE& hValue

  输出参数,返回尾节点的句柄值。

 

4、返回值

    true: 执行成功

    false: 红黑树中无节点

  

5、相关函数  

RbtGetHead()

 

示例1:

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCardID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, (void*)pPersonInfo, pos);

}

 

HANDLE hValue;

RbtGetTail(hTree, hValue);

 

    LPPERSONINFO pPersonInfo;

pPersonInfo = LPPERSONINFO(hValue);

printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

            pPersonInfo->nAge);

 

RbtDeleteTree(hTree, OnDelete, “RbtGetKeyValue”);

 

    return 0;

}

 

  1.  RbtGetNext()

1、功能说明

此函数用来返回红黑树中指定节点的用户数据句柄,同时返回指定节点的下一个节点,下一个节点的键值大于当前节点的键值,如果当前节点是最后一

个节点,则返回NULL。

 

2、函数原型

HANDLE RbtGetNext(HANDLE hTree, POSITION& pos);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

    2)POSITION& pos

 指定节点句柄,输入、输出参数,执行完RbtGetNext函数后返回下一个节

点。

 

4、返回值

    返回输入节点句柄的用户数据句柄。

  

5、相关函数  

RbtGetHeadPosition()

RbtGetPrev()

RbtGetTailPosition()

 

示例1:

功能:采用升序遍历红黑树中的节点

 

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCarID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, pPersonInfo, pos);

}

 

HANDLE hValue;

POSITION pos;

// 升序遍历

Pos = RbtGetHeadPosition(hTree);

While(pos != NULL)

{

    hValue = RbtGetNext(hTree, pos);

 

        LPPERSONINFO pPersonInfo;

pPersonInfo = LPPERSONINFO(hValue);

printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

            pPersonInfo->nAge);

}

 

RbtDeleteTree(hTree, OnDelete, “RbtGetNext”);

 

    return 0;

}

  1.  RbtGetPrev()

1、功能说明

此函数用来返回红黑树中指定节点的用户数据句柄,同时返回指定节点的上

一个节点,上一个节点的键值小于当前节点的键值,如果当前节点是头节点,

则返回NULL。

 

2、函数原型

HANDLE RbtGetPrev(HANDLE hTree, POSITION& pos);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

    2)POSITION& pos

 指定节点句柄,输入、输出参数,执行完RbtGetPrev函数后返回上一个节

点。

 

4、返回值

    返回输入节点句柄的用户数据句柄。

  

5、相关函数  

RbtGetHeadPosition()

RbtGetNext()

RbtGetTailPosition()

 

示例1:

功能:采用降序遍历红黑树中的节点

 

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCarID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, pPersonInfo, pos);

}

 

HANDLE hValue;

POSITION pos;

// 升序遍历

Pos = RbtGetTailPosition(hTree);

While(pos != NULL)

{

    hValue = RbtGetPrev(hTree, pos);

 

        LPPERSONINFO pPersonInfo;

pPersonInfo = LPPERSONINFO(hValue);

printf(“cardid: %s name: %s sex: %d age: %d\r\n”,

       pPersonInfo->sCardID, pPersonInfo->sName, pPersonInfo->nSex,

            pPersonInfo->nAge);

}

 

RbtDeleteTree(hTree, OnDelete, “RbtGetPrev”);

 

    return 0;

}

 

  1.  RbtGetFirstPosGEKey()

1、功能说明

此函数返回红黑树中的第一个键值大于等于指定键值的节点,如果没有满足条件的节点返回NULL。

 

2、函数原型

POSITION RbtGetFirstPosGEKey(HANDLE hTree);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

4、返回值

    返回节点句柄。

  

5、相关函数  

RbtGetFirstPosGTKey()

RbtGetFirstPosSEKey()

RbtGetFirstPosSTKey()

 

示例1:

 

#include “RbtAPI.h”

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    if (int(pNodeKey) > int(pOutKey))

    {

        return 1;

    }

    else if (int(pNodeKey) < int(pOutKey))

    {

        return -1;

    }

    else

    {

        return 0;

    }

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

int i;

for(i = 0; i < 10; i++)

{

    POSITION pos;

RbtAddKey(hTree, (void *)i, (void *)i, pos);

}

 

POSITION pos;

pos = RbtGetFirstPosGEKey(hTree, (void*)(-1)); // 返回键值为0的节点

pos = RbtGetFirstPosGEKey(hTree, (void*)0);    // 返回键值为0的节点

pos = RbtGetFirstPosGEKey(hTree, (void*)5);    // 返回键值为5的节点

pos = RbtGetFirstPosGEKey(hTree, (void*)9);    // 返回键值为9的节点

pos = RbtGetFirstPosGEKey(hTree, (void*)10);    // 返回NULL

 

RbtDeleteTree(hTree);

 

    return 0;

}

 

  1.  RbtGetFirstPosGTKey()

1、功能说明

此函数返回红黑树中的第一个键值大于指定键值的节点,如果没有满足条件的节点返回NULL。

 

2、函数原型

POSITION RbtGetFirstPosGTKey(HANDLE hTree);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

4、返回值

    返回节点句柄。

  

5、相关函数  

RbtGetFirstPosGEKey()

RbtGetFirstPosSEKey()

RbtGetFirstPosSTKey()

 

示例1:

 

#include “RbtAPI.h”

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    if (int(pNodeKey) > int(pOutKey))

    {

        return 1;

    }

    else if (int(pNodeKey) < int(pOutKey))

    {

        return -1;

    }

    else

    {

        return 0;

    }

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

int i;

for(i = 0; i < 10; i++)

{

    POSITION pos;

RbtAddKey(hTree, (void *)i, (void *)i, pos);

}

 

POSITION pos;

pos = RbtGetFirstPosGTKey(hTree, (void*)(-1)); // 返回键值为0的节点

pos = RbtGetFirstPosGTKey(hTree, (void*)0);    // 返回键值为1的节点

pos = RbtGetFirstPosGTKey(hTree, (void*)5);    // 返回键值为6的节点

pos = RbtGetFirstPosGTKey(hTree, (void*)8);    // 返回键值为9的节点

pos = RbtGetFirstPosGTKey(hTree, (void*)9);    // 返回NULL

pos = RbtGetFirstPosGTKey(hTree, (void*)10);   // 返回NULL

 

RbtDeleteTree(hTree);

 

    return 0;

}

 

  1.  RbtGetFirstPosSEKey()

1、功能说明

此函数返回红黑树中的第一个键值小于等于指定键值的节点,如果没有满足条件的节点返回NULL。

 

2、函数原型

POSITION RbtGetFirstPosSEKey(HANDLE hTree);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

4、返回值

    返回节点句柄。

  

5、相关函数  

RbtGetFirstPosGTKey()

RbtGetFirstPosGEKey()

RbtGetFirstPosSTKey()

 

示例1:

 

#include “RbtAPI.h”

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    if (int(pNodeKey) > int(pOutKey))

    {

        return 1;

    }

    else if (int(pNodeKey) < int(pOutKey))

    {

        return -1;

    }

    else

    {

        return 0;

    }

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

int i;

for(i = 0; i < 10; i++)

{

    POSITION pos;

RbtAddKey(hTree, (void *)i, (void *)i, pos);

}

 

POSITION pos;

pos = RbtGetFirstPosSEKey(hTree, (void*)(-1)); // 返回NULL

pos = RbtGetFirstPosSEKey(hTree, (void*)0);    // 返回键值为0的节点

pos = RbtGetFirstPosSEKey(hTree, (void*)5);    // 返回键值为5的节点

pos = RbtGetFirstPosSEKey(hTree, (void*)9);    // 返回键值为9的节点

pos = RbtGetFirstPosSEKey(hTree, (void*)10);    //返回键值为9的节点

 

RbtDeleteTree(hTree);

 

    return 0;

}

 

  1.  RbtGetFirstPosSTKey()

1、功能说明

此函数返回红黑树中的第一个键值小于指定键值的节点,如果没有满足条件的节点返回NULL。

 

2、函数原型

POSITION RbtGetFirstPosSEKey(HANDLE hTree);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

4、返回值

    返回节点句柄。

  

5、相关函数  

RbtGetFirstPosGTKey()

RbtGetFirstPosGEKey()

RbtGetFirstPosSEKey()

 

示例1:

 

#include “RbtAPI.h”

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    if (int(pNodeKey) > int(pOutKey))

    {

        return 1;

    }

    else if (int(pNodeKey) < int(pOutKey))

    {

        return -1;

    }

    else

    {

        return 0;

    }

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

int i;

for(i = 0; i < 10; i++)

{

    POSITION pos;

RbtAddKey(hTree, (void *)i, (void *)i, pos);

}

 

POSITION pos;

pos = RbtGetPosSTKey(hTree, (void*)(-1)); // 返回NULL

pos = RbtGetPosSTKey(hTree, (void*)0);    // 返回NULL

pos = RbtGetPosSTKey(hTree, (void*)5);    // 返回键值为4的节点

pos = RbtGetPosSTKey(hTree, (void*)9);    // 返回键值为8的节点

pos = RbtGetPosSTKey(hTree, (void*)10);    //返回键值为9的节点

 

RbtDeleteTree(hTree);

 

    return 0;

}

 

  1.  RbtGetPosColor()

1、功能说明

此函数用来获取指定节点的颜色。

 

2、函数原型

int RbtGetPosKey(POSITION pos);

 

3、参数说明

   1)POSITION pos

      节点句柄

 

4、返回值

    返回节点的颜色:0代表红色, 1代表黑色。

  

5、相关函数  

RbtGetPosValue()

RbtGetPosKey()

 

示例:无。

 

  1.  RbtGetPosKey()

1、功能说明

此函数用来获取指定节点的键值。

 

2、函数原型

void* RbtGetPosKey(POSITION pos);

 

3、参数说明

   1)POSITION pos

      节点句柄

 

4、返回值

    返回节点键值。

  

5、相关函数  

RbtGetPosValue()

 

示例:

参考函数RbtGetHeadPosition示例中的关于RbtGetPosKey用法。

  1.  RbtGetPosValue()

1、功能说明

此函数用来获取指定节点的用户数据句。

 

2、函数原型

HANDLE RbtGetPosValue(POSITION pos);

 

3、参数说明

   1)POSITION pos

      节点句柄。

 

4、返回值

    返回节点的句柄值。

  

5、相关函数  

RbtGetPosKey()

 

示例:

参考函数RbtGetHeadPosition示例中的关于RbtGetPosValue用法。

 

  1.  RbtSetPosValue()

1、功能说明

此函数用来设置指定节点的用户数据。

 

2、函数原型

void RbtSetPosValue(POSITION pos, HANDLE hValue);

 

3、参数说明

   1)HANDLE hTree

      红黑树句柄。

 

   2)HANDLE hValue

      设置的用户数据句柄值。

 

4、返回值

    无。

  

5、相关函数  

RbtGetPosValue()

 

  1.  RbtRemoveAt()

1、功能说明

根据节点句柄删除节点。

 

2、函数原型

void RbtRemoveAt(HANDLE hTree, POSITION pos);

 

3、参数说明

   1)HANDLE hTree

      红黑树的句柄,此句柄由RbtCreateTree函数生成。

 

2)POSITION pos

  要删除的节点句柄。

    

4、返回值

    无

 

5、相关函数

 

示例:

功能:根据节点句柄删除节点。

#include “RbtAPI.h”

 

typedef struct _PERSONINFO

{

    char sCardID[19];

    char sName[16];

    char nSex;

    short nAge;

}PERSONINFO;

typedef PERSONINFO* LPPERSONINFO;

 

bool OnDelete(POSITION pos, void* pInputPara)

{

LPPERSONINFO pPersonInfo;

pPersonInfo = (LPPERSONINFO)(RbtGetPosValue(pos))

 

    delete pPersonInfo;

    return true;

}

 

int OnCompare(void* pNodeKey, void* pOutKey)

{

    return(strcmp((char *)pNodeKey, (char *)pOutKey));

}

 

int main(int argc, char* argv[])

{

HANDLE hTree;

hTree = RbtCreateTree(OnCompare);

 

LPPERSONINFO pPersonInfo;

 

int i;

for(i = 0; i < 10; i++)

{

pPersonInfo = new PERSONINFO;

sprintf(pPersonInfo->sCardID, “12345678901234567%d”, i);

sprintf(pPersonInfo->sName, “zhang%d”, i);

pPersonInfo->nSex = 1;

pPersonInfo->nAge = 20 + i;

POSITION pos;

RbtAddKey(hTree, pPersonInfo->sCardID, (void*)pPersonInfo, pos);

}

 

POSITION pos;

pos = RbtGetHeadPosition(hTree);

 

RbtRemoveAt(hTree, pos);

 

RbtDeleteTree(hTree, OnDelete, “RbtGetKeyValue”);

 

    return 0;

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值