Recursive algorithm for binary lookup of sequential table

Source: Internet
Author: User
-Academic year-1th semester of the 17 academic year data structure experiment task Book

Professional name: Experimental Hours: 2

Course Name: Data structure Instructor: Shanhai

Experimental topic: Search algorithm Implementation and analysis

Lab Environment: Visual C + + 6.0

Experiment item Seven: Search algorithm implementation and analysis

Experiment Objective: 1. Grasp the method of searching the sequential table, especially the binary search method.

2. Grasp the establishment of binary sorting tree and find the process, understand the binary sorting tree search process and insertion and deletion algorithm.

Experimental content: 1. The recursive algorithm for binary searching of ordered table is programmed.

2. Create a two-fork sorting tree and find it, traverse it, and so on.

3. Select: Determine whether a binary tree is a two-fork sort tree.

#include <iostream> #include <cstdlib> using namespace std;
#define MAXSIZE #define OK 1;

typedef struct{INT key;//keyword field}elemtype;
	typedef struct{ELEMTYPE *r;
int length;

}sstable;
	int initlist_sstable (sstable &l)//Initialize {l.r= (Elemtype *) malloc (MAXSIZE *sizeof (elemtype)); if (!
		L.R) {cout<< "initialization error";
	return 0;
	} l.length=0;
return OK;
	} int insert_sstable (sstable &l) {int j=1;
		for (int i=1;i<maxsize;i++) {l.r[i].key=j;
		l.length++;
	j + +;
}//return 1; }/*int Search_bin (sstable st,int key) {//In the ordered table ST binary find the data element whose key is equal to key.							If found, the function value is//the position of the element in the table, otherwise 0 int low=1,high=st.length;
   Set search interval initial value int mid;
      while (Low<=high) {mid= (Low+high)/2; if (key==st.      		R[mid].key) return mid; Locate the unknown origin element else if (key<st.		R[mid].key) high = mid-1;                       			Continue to look in the previous sub-table for else low =mid +1;										Continue to look in the latter sub-table}//while return 0; The unknown origin element is not present in the table}//search_bin*/int Binsrch (sstableL,int k,int Low,int High)//Find the keyword K in an ordered table of n long, if the lookup succeeds, returns the location of K, and the lookup failure returns 0.
   {int Mid, if (Low<=high)//low and high are the lower and upper bounds of the ordered table, respectively {mid= (Low+high)/2;
   if (l.r[mid].key==k) return mid;
        else if (l.r[mid].key<k) return Binsrch (L,k,mid+1,high);
   else return Binsrch (l,k,low,mid-1);
} else return 0;//lookup failed.
	}//algorithm ends void show_end (int result,int testkey) {if (result==0) cout<< "not Found" <<testkey<<endl;
	else cout<< "Find" <<testkey<< "location is" <<result<<endl;
Return
	} int main () {sstable ST;
	Initlist_sstable (ST);
	Insert_sstable (ST);
	int testkey1=7,testkey2=200;
	int result;
	Result=binsrch (st,testkey1,1,99);
	Show_end (Result,testkey1);
	Result=binsrch (st,testkey2,1,99);
Show_end (Result,testkey2); }

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.