使用指针数组实现bst插入和查找操作
2014-05-21 15:00
253 查看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
node* PNode;
#define key int
#define Item int
struct node
{
int index;
Item data;
PNode left;
PNode right;
}__attribute__((aligned(sizeof(int))));
PNode a[100];//暂时定为100,可以先申请内存,然后在申请数组内容的内存,使用指针数组
int inc;//记录插入的个数
int N;//记录数组的大小
#define KEYITEM(A) A
#define Key(A) a[A]->data
#define KLEFT(A) a[A]->left
#define KRIGHT(A) a[A]->right
#define KEYDATA(A) a[A]
#define KNULL -1
#define KSUCCESS -2
#define KLESS -3
#define KMORE -4
static
inline PNode NEW(Item item,PNode l,PNode r,int
aIndex)
{
PNode tp = NULL;
do
{
tp =
malloc(sizeof(struct
node));
}
while (!tp);
tp->index = aIndex;
tp->data = item;
tp->left = l;
tp->right = r;
return tp;
}
void init(int maxN)
{
N = maxN;
for (int i =
0;i < maxN;++i)
{
a[i] = NEW(0,
0, 0,i);
}
}
int STcount()
{
return inc;
}
int searchR(aKey)
{
int count = 0;
for (int i =
0;i < inc;++i)
{
if (Key(i) == aKey)
{
return KSUCCESS;
}
count =
a[i]->index;
PNode temp = NULL;
if (Key(i) < aKey)
{
temp =
a[i]->right;
}
else if (Key(i) > aKey)
{
temp =
a[i]->left;
}
if (!temp)
{
return count;
}
for (;temp;)
{
count = temp->index;
if (temp->data < aKey)
{
temp = temp->left;
}
else if (temp->data > aKey)
{
temp = temp->right;
}
else if (temp->data == aKey)
{
return KSUCCESS;
}
}
if (!temp)
{
return count;
}
}
return count;
}
int STsearch(key aKey)
{
return searchR(aKey);
}
void STinsert(Item aItem)
{
int i = STsearch(KEYITEM(aItem));
if (i >= 0)
{
int j = 0;
while (Key(j)&&(j <
inc))
{
j +=
1;
}
Key(j) = KEYITEM(aItem);
if (Key(i) <
Key(j))
{
KRIGHT(i)= KEYDATA(j);
}
else if (Key(i) >
Key(j))
{
KLEFT(i) = KEYDATA(j);
}
++inc;
}
}
#include <stdlib.h>
#include <string.h>
typedef struct
node* PNode;
#define key int
#define Item int
struct node
{
int index;
Item data;
PNode left;
PNode right;
}__attribute__((aligned(sizeof(int))));
PNode a[100];//暂时定为100,可以先申请内存,然后在申请数组内容的内存,使用指针数组
int inc;//记录插入的个数
int N;//记录数组的大小
#define KEYITEM(A) A
#define Key(A) a[A]->data
#define KLEFT(A) a[A]->left
#define KRIGHT(A) a[A]->right
#define KEYDATA(A) a[A]
#define KNULL -1
#define KSUCCESS -2
#define KLESS -3
#define KMORE -4
static
inline PNode NEW(Item item,PNode l,PNode r,int
aIndex)
{
PNode tp = NULL;
do
{
tp =
malloc(sizeof(struct
node));
}
while (!tp);
tp->index = aIndex;
tp->data = item;
tp->left = l;
tp->right = r;
return tp;
}
void init(int maxN)
{
N = maxN;
for (int i =
0;i < maxN;++i)
{
a[i] = NEW(0,
0, 0,i);
}
}
int STcount()
{
return inc;
}
int searchR(aKey)
{
int count = 0;
for (int i =
0;i < inc;++i)
{
if (Key(i) == aKey)
{
return KSUCCESS;
}
count =
a[i]->index;
PNode temp = NULL;
if (Key(i) < aKey)
{
temp =
a[i]->right;
}
else if (Key(i) > aKey)
{
temp =
a[i]->left;
}
if (!temp)
{
return count;
}
for (;temp;)
{
count = temp->index;
if (temp->data < aKey)
{
temp = temp->left;
}
else if (temp->data > aKey)
{
temp = temp->right;
}
else if (temp->data == aKey)
{
return KSUCCESS;
}
}
if (!temp)
{
return count;
}
}
return count;
}
int STsearch(key aKey)
{
return searchR(aKey);
}
void STinsert(Item aItem)
{
int i = STsearch(KEYITEM(aItem));
if (i >= 0)
{
int j = 0;
while (Key(j)&&(j <
inc))
{
j +=
1;
}
Key(j) = KEYITEM(aItem);
if (Key(i) <
Key(j))
{
KRIGHT(i)= KEYDATA(j);
}
else if (Key(i) >
Key(j))
{
KLEFT(i) = KEYDATA(j);
}
++inc;
}
}
相关文章推荐
- 关于链表结构的基本操作 c 实现 (创建,插入删除,反转,合并链表,查找,是否有环,链表相交情况)
- objecttive-C语言使用二叉排序树实现查找、插入、删除、查看元素
- c语言:顺序表的实现(一) 创建,插入,删除,查找,输出等基本操作实现
- php使用PDO事务配合表格读取大量数据插入操作实现方法
- 如何使用c语言实现双向链表的插入删除操作
- Qt创建那个XML文档,并且对它实现查找,更新,插入等操作
- C语言实现双链表基本操作(创建、查找、插入、删除)
- C语言实现顺序表的插入、删除、查找、遍历等基本操作
- [Java Web]ibatis使用queryForMap实现数据查找等操作
- ViewPager使用FragmentPagerAdapter加载多个fragment,实现对已加载的list做插入、删除及排序操作
- 排序二叉树BST的基本操作(1)插入,查找,最大,最小
- (二)C#使用DsoFramer操作Excel实现查找、替换的功能
- 高级数组,实现特定值的插入、查找、删除等操作
- [数据结构]10.2实现binary search tree的查找和插入操作,用非递归的方法实现
- 数据结构基础5.2:二叉搜索树(BST)的基本操作(插入、查找、删除)
- C++链表的创建、插入、删除、查找、合并、排序、修改等操作的实现
- 链表(单双链表)用法与基本操作(构建、查找、插入、删除)实现
- 使用终端操作数据库文件,创建 删除 查找内容 插入内容
- C++实现二叉排序树BSTree --插入删除摧毁查找等操作
- 二叉查找树的查找、插入、删除、释放等基本操作的实现(C语言)