2.构造一个静态顺序表(数组),输入一个有序序列存放表中,使用二分查找法查找key是否在表中.
3.把序列[10,18,3,8,12,2,7,3]构造成一个二叉排序树;中序遍历输出序列得到一个非递减序列;输入一个关键字key,查找key是否存在于树中
4.给定一个关键字序列4,5,7,2,1,3,6,试生成一棵平衡二叉树
请会的人帮我看一看````谢谢~
第2个:
#include <stdio.h>
#include <conio.h>
#define N 10
static void CreateArray(int iarra[]);
static int SearchElem(int iarra[], int ikey, int ilow, int ihigh);
int main(void)
{
int iarra[N], ikey, ilow, ihigh, iindex = -1;
CreateArray(iarra);
puts("Enter found key: ");
scanf("%d", &ikey);
ilow = 0, ihigh = N - 1;
if ((iindex = SearchElem(iarra, ikey, ilow, ihigh)) != -1)
{
printf("found %d in iarra array.\n", iarra[iindex]);
}
else
{
printf("no found %d in iarra array.\n", ikey);
}
getch();
return 0;
}
static void CreateArray(int iarra[])
{
int i;
puts("Enter ten value: ");
for (i = 0; i < N; i++)
{
scanf("%d", &iarra[i]);
}
}
static int SearchElem(int iarra[], int ikey, int ilow, int ihigh)
{
int imiddle;
while (ilow <= ihigh)
{
imiddle = (ilow + ihigh) / 2;
if (iarra[imiddle] == ikey)
{
return imiddle;
}
else if (iarra[imiddle] > ikey)
{
ihigh = imiddle - 1;
}
else if (iarra[imiddle] < ikey)
{
ilow = imiddle + 1;
}
}
return -1;
}
第1个:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node *nextPtr;
}*LinkList, Lnode;
static void CreateList(LinkList *headPtr, LinkList *tailPtr);
static void SearchElem(LinkList headPtr);
int main(void)
{
LinkList headPtr = NULL, tailPtr = NULL;
CreateList(&headPtr, &tailPtr);
SearchElem(headPtr);
getch();
return 0;
}
static void CreateList(LinkList *headPtr, LinkList *tailPtr)
{
LinkList newPtr;
int ia;
while ((ia = getchar()) != EOF)
{
if ((newPtr = (LinkList)malloc(sizeof(Lnode))) == NULL)
{
exit(1);
}
newPtr -> data = ia;
newPtr -> nextPtr = NULL;
if (*headPtr == NULL)
{
newPtr -> nextPtr = *headPtr;
*headPtr = newPtr;
}
else
{
(*tailPtr) -> nextPtr = newPtr;
}
*tailPtr = newPtr;
}
}
static void SearchElem(LinkList headPtr)
{
char ca;
int tag = 0;
printf("Enter search node: ");
fflush(stdin);
ca = getchar();
for (; headPtr != NULL; headPtr = headPtr -> nextPtr)
{
if (headPtr -> data == ca)
{
printf("found %c in linklist.\n", ca);
tag = 1;
break;
}
}
if (tag == 0)
{
printf("no found %c in linklist.\n", ca);
}
}
第3个:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct node
{
int data;
struct node *lchild, *rchild;
}*Tree, Tnode;
static void CreateTree(Tree *T, int ikey);
static Tree SearchKey(Tree T, int ikey);
int main(void)
{
Tree T = NULL, p = NULL;
int ikey;
while (1)
{
scanf("%d", &ikey);
if (ikey == -1)
{
break;
}
CreateTree(&T, ikey);
}
printf("Enter search key: ");
scanf("%d", &ikey);
if ((p = SearchKey(T, ikey)) != NULL)
{
printf("found %d.\n", p -> data);
}
else
{
printf("no found.\n");
}
getch();
return 0;
}
static void CreateTree(Tree *T, int ikey)
{
if (*T == NULL)
{
if (((*T) = (Tree)malloc(sizeof(Tnode))) == NULL)
{
exit(1);
}
(*T) -> data = ikey;
(*T) -> lchild = (*T) -> rchild = NULL;
}
else
{
if ((*T) -> data > ikey)
{
CreateTree(&(*T) -> lchild, ikey);
}
else if ((*T) -> data < ikey)
{
CreateTree(&(*T) -> rchild, ikey);
}
}
}
static Tree SearchKey(Tree T, int ikey)
{
Tree p = NULL;
if (T == NULL)
{
return NULL;
}
if (T -> data == ikey)
{
return T;
}
if (T -> lchild != NULL)
{
p = SearchKey(T -> lchild, ikey);
if (p != NULL)
{
return p;
}
}
if (T -> rchild != NULL)
{
p = SearchKey(T -> rchild, ikey);
if (p != NULL)
{
return p;
}
}
return NULL;
}
第一个:
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int data;
struct node *next;
}*LinkList, ListNode;
void CreateList(LinkList *head, LinkList *tail);
void SearchElem(LinkList head);
void VisitList(LinkList head);
void DestroyList(LinkList *head);
int main(void)
{
LinkList newhead = NULL, newtail = NULL;
CreateList(&newhead, &newtail);
VisitList(newhead);
SearchElem(newhead);
DestroyList(&newhead);
return 0;
}
void CreateList(LinkList *head, LinkList *tail)
{
int data;
LinkList p;
printf("Enter one number:\n");
scanf("%d", &data);
while (data != 0)
{
if ((p = (LinkList)malloc(sizeof(ListNode))) == NULL)
{
exit(1);
}
p -> data = data;
p -> next = NULL;
if (*head == NULL)
{
p -> next = *head;
*head = p;
}
else
{
(*tail) -> next = p;
}
*tail = p;
printf("Enter one number:\n");
scanf("%d", &data);
}
}
void SearchElem(LinkList head)
{
int key;
LinkList p;
printf("Enter the key:\n");
scanf("%d", &key);
p = head;
while (p && p -> data != key)
{
p = p -> next;
}
if (p -> data == key)
printf("The key exist in the list!\n");
else
printf("The key is not exist in the list!\n");
}
void VisitList(LinkList head)
{
while(head)
{
printf("%d ", head -> data);
head = head -> next;
}
printf("\n");
}
void DestroyList(LinkList *head)
{
LinkList temp;
while (*head != NULL)
{
temp = *head;
*head = (*head) -> next;
free(temp);
}
*head = NULL;
}
第二个:
#include <stdio.h>
#define N 10
void CreateArray(int a[]);
int binary_search(int a[], int key, int left, int right);
int main(void)
{
int array[N], i, number;
CreateArray(array);
printf("Enter search number:\n");
scanf("%d", &number);
i = binary_search(array, number, 0, N - 1);
if (i != -1)
printf("Found key %d in the %d order in the array !", number, i);
else
printf("Not found key %d in the array!", number);
return 0;
}
void CreateArray(int a[])
{
int i;
for (i = 0; i < N; i++)
{
printf("Enter one number:\n");
scanf("%d", &a[i]);
}
}
int binary_search(int a[], int key, int left, int right)
{
int mid;
if (left <= right)
{
mid = (left + right) / 2;
if (key > a[mid])
binary_search(a, key, mid + 1, right);
else if (key < a[mid])
binary_search(a, key, left, mid - 1);
else
return mid;
}
else
return -1;
}