声明
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LIST_INIT_SIZE 100
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
}SSTable;
typedef int TElemType;//二叉树
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;//左右孩子指针
}BiTNode, *BiTree;
int Search_Seq(SSTable ST, ElemType key);
int Search_Bin(SSTable ST, ElemType key);
bool SearchBST(BiTree T, TElemType Tkey, BiTree f, BiTree &p);
bool InsertBST(BiTree &T, TElemType Tkey);
bool InitList(SSTable &ST);
bool InitBiTree(BiTree &T);
bool InOrderTraverse(BiTree T);
主函数
int main()
{
printf("1.顺序查找+折半查找\n");
SSTable ST;
InitList(ST);
int n;
printf("请输入要查找序列的元素个数:\n");
scanf("%d", &n);
ST.length = n;
printf("请输入要查找序列中的元素:\n");
int i;
for(i = 1; i <= n; i ++)
scanf("%d", &ST.elem[i]);
ElemType key;
printf("请输入要查找的元素:\n");
scanf("%d", &key);
printf("使用顺序查找的获得的元素位置为:%d\n", Search_Seq(ST, key));
printf("使用折半查找的获得的元素位置为:%d\n", Search_Bin(ST, key));
printf("2.从空树开始,通过逐个插入关键字序列(45,24,53,12,37,93),建立一棵二叉排序树\n");
BiTree T;
InitBiTree(T);
TElemType Tkey;
printf("请输入要在二叉排序树中插入的元素个数:\n");
scanf("%d", &n);
for(i = 1; i <= n; i ++)
{
printf("请输入要在二叉排序树T中插入的第%d个关键字:\n", i);
scanf("%d", &Tkey);
InsertBST(T, Tkey);
}
printf("输出执行插入操作后的二叉排序树的中序遍历序列(即中序序列):\n");
InOrderTraverse(T);
printf("\n");
return 0;
}
顺序查找
int Search_Seq(SSTable ST, ElemType key)
{
/*在顺序表ST中顺序查找其关键字等于key的数据元素。若找到,其函数值为该元素在表中的位置,否则为0*/
int i;
ST.elem[i] = key;//"哨兵"
for(i = ST.length; ST.elem[i] != key; i --);//从后往前找
return i; //找不到时,i为0
}//Search_Seq
折半查找
int Search_Bin(SSTable ST, ElemType key)
{
/*在有序表ST中折半查找其关键字等于key的数据元素。若找到,则函数值为该元素在表中的位置,否则为0*/
int low = 1, high = ST.length;
int mid;
while(low <= high)
{
mid = (low + high) / 2;
if(ST.elem[mid] == key)
return mid;
else
if(ST.elem[mid] > key)
high = mid - 1;
else
low = mid + 1;
}
return 0;
}//Search_Bin
二叉排序树查找
bool SearchBST(BiTree T, TElemType Tkey, BiTree f, BiTree &p)
{
//在根指针T所指二叉排序树中递归的查找其关键字等于key的数据元素;
//若查找成功,则指针p指向该数据元素结点,并返回true;
//否则指针p指向查找路径上访问的最后一个结点并返回false;
//指针f指向T的双亲,其初始调用值为NULL
if(!T) //查找不成功
{
p = f;
return false;
}
else
if(Tkey == T->data) //查找成功
{
p = T;
return true;
}
else
if(Tkey < T->data) //在左子树中继续查找
return SearchBST(T->lchild, Tkey, T, p);
else //在右子树中继续查找
return SearchBST(T->rchild, Tkey, T, p);
}//SearchBST
bool InsertBST(BiTree &T, TElemType Tkey)
{
//当二叉排序树T中不存在关键字等于key的数据元素时,插入key并返回true,否则返回false
BiTree s;
BiTree p;
if(!SearchBST(T, Tkey, NULL, p))
{
s = (BiTree)malloc(sizeof(BiTNode));
s->data = Tkey; s->lchild = NULL; s->rchild = NULL;
if(!p)//原树为空
T = s;
else
if(Tkey < p->data)
p->lchild = s;
else
p->rchild = s;
return true;
}//if
else
return false;
}//InsertBST
附:顺序表及二叉树的基本操作
bool InitList(SSTable &ST)
{
ST.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!ST.elem)
return false;
ST.length = 0;
return true;
}//InitList
bool InitBiTree(BiTree &T)
{
T = NULL;//这时候要求是空树啊!
//如果是 if(T = NULL) return false;这时候肯定不是空!!!
return true;
}//InitBiTree
bool InOrderTraverse(BiTree T)
{
if(T == NULL)
return false;
InOrderTraverse(T->lchild);
printf("%d ", T->data);
InOrderTraverse(T->rchild);
return true;
}