Data Structure Tutorial Lesson 35th Experiment Seven Search

Source: Internet
Author: User
Tags printf sort

Teaching Purpose: practice sequential lookup, binary lookup and binary sort tree implementation

Teaching Focus:

Teaching Difficulties:

Teaching Content:

Sequential Lookup

Binary find

Sequential lookup and Binary lookup examples

#include <stdio.h> typedef int KEYTYPE;
  typedef struct{KEYTYPE Key;
  int maths;
int 中文版;
}elemtype; #define EQ (A,b) ((a) = = (b)) #define LT (A,b) ((a) < (b)) #define LQ (A,b) ((a) <= (b)) typedef struct {Elemtype *el
  Em
int length;

}sstable;
  int Search_seq (sstable st,keytype key) {int i;
  St.elem[0].key=key; for (i=st.length;!) EQ (St.elem[i].key,key);
  I.);
return i;
  int Search_bin (sstable st,keytype key) {int low,mid,high;
  Low=1;high=st.length;
    while (Low<=high) {mid= (Low+high)/2;
    If EQ (Key,st.elem[mid].key) return mid;
    else if LT (Key,st.elem[mid].key) high=mid-1;
  else Low=mid +1;
  } getdata (Sstable * t) {FILE *FP;
  int i=1;
  Fp=fopen ("Stu.txt", "R");
  FSCANF (FP, "%d",& (t->length)); while (i<=t->length) {fscanf (FP, "%d%d%d",& (T->elem[i].key), & (T->elem[i].maths),& (t
      ->elem[i].english));
    i++;
Fclose (FP);
  Main () {elemtype stu[50]; Sstable class;
  int i,j,k;
  Long time;


  Class.elem=stu;

  GetData (&class);
  printf ("This class has%d students.\n", class.length);
  printf ("Input Stuno you want search:\n");

  scanf ("%d", &k);
  I=search_seq (CLASS,K);
  J=search_bin (CLASS,K);
  printf ("Maths english\n");
  printf ("%d%d\n", class.elem[i].maths,class.elem[i].english);

  printf ("%d%d\n", class.elem[j].maths,class.elem[j].english);
      for (i=1;i<=4;i++) {j=stu[i].maths+stu[i].english;
    printf ("%d\n", j); }

}

Two-fork Sort tree

Example

#include <alloc.h> #define ERROR 0;
#define FALSE 0;
#define TRUE 1;


#define OK 1;
typedef int ELEMTYPE;
typedef int STATUS;

typedef int KEYTYPE;
  #define EQ (A,b) ((a) = = (b)) #define LT (A,b) ((a) < (b)) #define LQ (A,b) ((a) <= (b)) typedef struct BINARYTREE {
  Elemtype data;
  struct BinaryTree *l;
struct BinaryTree *r;

}*bitree,binode;

Binode * NEW () {return ((Binode *) malloc (sizeof (Binode)));} Createsubtree (bitree *t,elemtype *all,int i) {if (all[i]==0) | |
      I&GT;16) {*t=null;
    return OK;
  } *t=new ();
  if (*t==null) return ERROR;
  (*t)->data=all[i];
  Createsubtree (& (*t)->l), all,2*i);
Createsubtree (& (*t)->r), all,2*i+1);
  } createbitree (Bitree *t) {elemtype all[16]={0,1,2,3,0,0,4,5,0,0,0,0,6,0,0,0,};
Createsubtree (t,all,1);

} printelem (Elemtype d) {printf ("%d\n", d);} Preordertraverse (Bitree t,int (*visit) (Elemtype D)) {if (T) {if (Visit (T->data)) if (preordertraverse , Visit)) if (PreordErtraverse (t->r,visit)) return OK;
  return ERROR;
else return OK; } inordertraverse (Bitree t,int (*visit) (Elemtype D)) {if (T) {if (Inordertraverse (t->l,visit)) if (Visit
    Gt;data)) if (Inordertraverse (t->r,visit)) return OK;
  return ERROR;
}else return OK; Status Searchbst (bitree t,keytype key,bitree f,bitree *p) {if (!)
  T) {*p=f;return FALSE;}
  else if EQ (key,t->data) {*p=t;return TRUE;}
  else if LT (key,t->data) Searchbst (t->l,key,t,p);
else Searchbst (t->r,key,t,p);
  Status Insertbst (Bitree *t,elemtype e) {Bitree p;
  Bitree s; if (!
    Searchbst (*t,e,null,&p)) {s= (bitree) malloc (sizeof (Binode));
    s->data=e;s->l=s->r=null;
    if (!p) *t=s;
    else if (LT (e,p->data)) p->l=s;
    else p->r=s;
  return TRUE;
else return FALSE;
  } void Delete (Bitree *p) {Bitree q,s; if (!) (
    *P)->r) {q= (*p);
    (*p) = (*p)->l;
  Free (q); Or else if (!
    *P)->l) {q= (*p); (*p) = (*p)->r;
    Free (q);
    else {/* q= (*P);
    S= (*p)->l;
    while (s->r) {q=s; S=s->r}
    (*p)->data=s->data;
    if (q!= (*p)) q->r=s->l;
    else q->l=s->l;
    Free (s);
    * * q=s= (*P)->l;
    while (S->r) s=s->r;
    S->r= (*p)->r;
    Free (*p);

  (*p) =q; } Status Deletebst (Bitree *t,keytype key) {if (!) (
  *t)) {return FALSE;}
    else{if (EQ (key, (*t)->data) Delete (T);
    else if (LT (key, (*t)->data) Deletebst ((*t)->l), key);
    Else Deletebst (& ((*t)->r), key);
  return TRUE;
  } main () {Bitree root;
  Bitree Sroot=null;
  int i;
  int a[10]={45,23,12,3,33, 27,56,90,120,62};
  System ("CLS");
  Createbitree (&root);
  printf ("preordertraverse:\n");
  Preordertraverse (Root,printelem);
  printf ("inordertraverse:\n");
  Inordertraverse (Root,printelem);
  for (i=0;i<10;i++) Insertbst (&sroot,a[i]);
  printf ("inordertraverse:\n");
  Inordertraverse (Sroot,printelem); for (I=0;I&Lt;3;i++) Deletebst (&sroot,a[i]);
  printf ("Now Sroot has nodes:\n");
Inordertraverse (Sroot,printelem); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.